处理异常
This commit is contained in:
parent
f5d9c53c2c
commit
5f4af8583a
|
|
@ -4,6 +4,9 @@ using Microsoft.AspNetCore.Http;
|
|||
using Microsoft.Extensions.Logging;
|
||||
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Serialization;
|
||||
|
||||
using System;
|
||||
|
||||
|
||||
namespace HuanMeng.DotNetCore.MiddlewareExtend
|
||||
|
|
@ -30,6 +33,12 @@ namespace HuanMeng.DotNetCore.MiddlewareExtend
|
|||
}
|
||||
catch (ArgumentNullException ex)
|
||||
{
|
||||
var settings = new JsonSerializerSettings
|
||||
{
|
||||
ContractResolver = new CamelCasePropertyNamesContractResolver()
|
||||
};
|
||||
|
||||
|
||||
context.Response.StatusCode = StatusCodes.Status200OK;
|
||||
BaseResponse<object> baseResponse = new BaseResponse<object>(ResonseCode.ParamError, ex.ParamName ?? "参数错误", null)
|
||||
{
|
||||
|
|
@ -37,17 +46,21 @@ namespace HuanMeng.DotNetCore.MiddlewareExtend
|
|||
};
|
||||
context.Response.ContentType = "application/json; charset=utf-8";
|
||||
// 将异常信息写入 HTTP 响应
|
||||
await context.Response.WriteAsync(JsonConvert.SerializeObject(baseResponse));
|
||||
await context.Response.WriteAsync(JsonConvert.SerializeObject(baseResponse, settings));
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
var settings = new JsonSerializerSettings
|
||||
{
|
||||
ContractResolver = new CamelCasePropertyNamesContractResolver()
|
||||
};
|
||||
_logger.LogError(context.Request.Path.ToString(), ex, "异常记录");
|
||||
// 设置 HTTP 响应状态码为 500
|
||||
context.Response.ContentType = "application/json; charset=utf-8";
|
||||
context.Response.StatusCode = StatusCodes.Status200OK;
|
||||
BaseResponse<object> baseResponse = new BaseResponse<object>(ResonseCode.Error, ex.Message, null);
|
||||
// 将异常信息写入 HTTP 响应
|
||||
await context.Response.WriteAsync(JsonConvert.SerializeObject(baseResponse));
|
||||
await context.Response.WriteAsync(JsonConvert.SerializeObject(baseResponse, settings));
|
||||
}
|
||||
finally
|
||||
{
|
||||
|
|
|
|||
|
|
@ -90,7 +90,7 @@ namespace HuanMeng.MiaoYu.Code.Users
|
|||
}
|
||||
ResponseAccountLogIn responseAccountLogIn = new ResponseAccountLogIn()
|
||||
{
|
||||
token = accountInfo.Token,
|
||||
Token = accountInfo.Token,
|
||||
NickName = accountInfo.NickName,
|
||||
UserId = accountInfo.UserId,
|
||||
};
|
||||
|
|
|
|||
|
|
@ -14,7 +14,7 @@ namespace HuanMeng.MiaoYu.Model.Dto.Account
|
|||
/// <summary>
|
||||
/// token
|
||||
/// </summary>
|
||||
public string token { get; set; }
|
||||
public string Token { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 昵称
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user