添加dto模式的demo测试
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using log4net.Core;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Newtonsoft.Json;
|
||||
@@ -7,6 +8,7 @@ using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Yi.Framework.Common.Enum;
|
||||
using Yi.Framework.Common.Exceptions;
|
||||
using Yi.Framework.Common.Models;
|
||||
|
||||
namespace Yi.Framework.WebCore.MiddlewareExtend
|
||||
@@ -17,11 +19,12 @@ namespace Yi.Framework.WebCore.MiddlewareExtend
|
||||
public class ErrorHandExtension
|
||||
{
|
||||
private readonly RequestDelegate next;
|
||||
private ILogger<ErrorHandExtension> _logger;
|
||||
public ErrorHandExtension(RequestDelegate next,ILogger<ErrorHandExtension> logger)
|
||||
//private readonly IErrorHandle _errorHandle;
|
||||
public ErrorHandExtension(RequestDelegate next/*, IErrorHandle errorHandle*/)
|
||||
{
|
||||
this.next = next;
|
||||
_logger = logger;
|
||||
//this._logger = loggerFactory.CreateLogger<ErrorHandExtension>();
|
||||
//_errorHandle = errorHandle;
|
||||
}
|
||||
|
||||
public async Task Invoke(HttpContext context)
|
||||
@@ -30,14 +33,19 @@ namespace Yi.Framework.WebCore.MiddlewareExtend
|
||||
{
|
||||
await next(context);
|
||||
}
|
||||
catch (BusinessException businessEx)
|
||||
{
|
||||
var statusCode = 200;
|
||||
await HandleExceptionAsync(context, statusCode, businessEx.Message, businessEx.Code);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
//await _errorHandle.Invoer(context, ex);
|
||||
var statusCode = context.Response.StatusCode;
|
||||
if (ex is ArgumentException)
|
||||
{
|
||||
statusCode = 200;
|
||||
}
|
||||
_logger.LogError($"中间件抓取错误\r\n错误信息:{ex.Message}\r\n堆栈信息“{ex.StackTrace}");
|
||||
await HandleExceptionAsync(context, statusCode, ex.Message);
|
||||
}
|
||||
finally
|
||||
@@ -45,9 +53,9 @@ namespace Yi.Framework.WebCore.MiddlewareExtend
|
||||
var statusCode = context.Response.StatusCode;
|
||||
var msg = "";
|
||||
|
||||
switch (statusCode)
|
||||
switch (statusCode)
|
||||
{
|
||||
case 401: msg = "未授权";break;
|
||||
case 401: msg = "未授权"; break;
|
||||
case 403: msg = "未授权"; break;
|
||||
case 404: msg = "未找到服务"; break;
|
||||
case 502: msg = "请求错误"; break;
|
||||
@@ -59,22 +67,20 @@ namespace Yi.Framework.WebCore.MiddlewareExtend
|
||||
}
|
||||
}
|
||||
//异常错误信息捕获,将错误信息用Json方式返回
|
||||
private static Task HandleExceptionAsync(HttpContext context, int statusCode, string msg)
|
||||
private static Task HandleExceptionAsync(HttpContext context, int statusCode, string msg, ResultCodeEnum code = ResultCodeEnum.NotSuccess)
|
||||
{
|
||||
Result resp;
|
||||
Result result;
|
||||
if (statusCode == 401)
|
||||
{
|
||||
resp = Result.UnAuthorize(msg);
|
||||
result = Result.UnAuthorize(msg);
|
||||
}
|
||||
else
|
||||
{
|
||||
resp = Result.Error(msg);
|
||||
result = Result.Error(msg).SetCode(code);
|
||||
}
|
||||
resp.SetCode((ResultCodeEnum)statusCode);
|
||||
var result = JsonConvert.SerializeObject(resp);
|
||||
|
||||
context.Response.ContentType = "application/json;charset=utf-8";
|
||||
|
||||
return context.Response.WriteAsync(result);
|
||||
return context.Response.WriteAsync(JsonConvert.SerializeObject(result));
|
||||
}
|
||||
}
|
||||
//扩展方法
|
||||
|
||||
Reference in New Issue
Block a user