feat:完成异常处理
This commit is contained in:
@@ -1,10 +1,12 @@
|
|||||||
using System.Runtime.Serialization;
|
using System.Runtime.Serialization;
|
||||||
|
using Furion.FriendlyException;
|
||||||
|
using Microsoft.AspNetCore.Http;
|
||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
using Yi.Framework.Infrastructure.Enums;
|
using Yi.Framework.Infrastructure.Enums;
|
||||||
|
|
||||||
namespace Yi.Framework.Infrastructure.Exceptions
|
namespace Yi.Framework.Infrastructure.Exceptions
|
||||||
{
|
{
|
||||||
public class AuthException : Exception,
|
public class AuthException : AppFriendlyException,
|
||||||
IHasErrorCode,
|
IHasErrorCode,
|
||||||
IHasErrorDetails,
|
IHasErrorDetails,
|
||||||
IHasLogLevel
|
IHasLogLevel
|
||||||
@@ -27,6 +29,12 @@ namespace Yi.Framework.Infrastructure.Exceptions
|
|||||||
Code = (int)code;
|
Code = (int)code;
|
||||||
Details = details;
|
Details = details;
|
||||||
LogLevel = logLevel;
|
LogLevel = logLevel;
|
||||||
|
|
||||||
|
|
||||||
|
base.ErrorCode = code;
|
||||||
|
base.StatusCode = StatusCodes.Status401Unauthorized;
|
||||||
|
base.ErrorMessage = $"{message}{(details is not null ? ":" + details : "")}";
|
||||||
|
base.ValidationException = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -38,11 +46,7 @@ namespace Yi.Framework.Infrastructure.Exceptions
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public AuthException WithData(string name, object value)
|
|
||||||
{
|
|
||||||
Data[name] = value;
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,10 +1,13 @@
|
|||||||
using System.Runtime.Serialization;
|
using System.ComponentModel.DataAnnotations;
|
||||||
|
using System.Runtime.Serialization;
|
||||||
|
using Furion.FriendlyException;
|
||||||
|
using Microsoft.AspNetCore.Http;
|
||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
using Yi.Framework.Infrastructure.Enums;
|
using Yi.Framework.Infrastructure.Enums;
|
||||||
|
|
||||||
namespace Yi.Framework.Infrastructure.Exceptions
|
namespace Yi.Framework.Infrastructure.Exceptions
|
||||||
{
|
{
|
||||||
public class BusinessException : Exception,
|
public class BusinessException : AppFriendlyException,
|
||||||
IHasErrorCode,
|
IHasErrorCode,
|
||||||
IHasErrorDetails,
|
IHasErrorDetails,
|
||||||
IHasLogLevel
|
IHasLogLevel
|
||||||
@@ -26,6 +29,10 @@ namespace Yi.Framework.Infrastructure.Exceptions
|
|||||||
Code = code;
|
Code = code;
|
||||||
Details = details;
|
Details = details;
|
||||||
LogLevel = logLevel;
|
LogLevel = logLevel;
|
||||||
|
base.ErrorCode= code;
|
||||||
|
base.StatusCode = StatusCodes.Status403Forbidden;
|
||||||
|
base.ErrorMessage = $"{message}{(details is not null? ":"+details:"")}";
|
||||||
|
base.ValidationException = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -37,11 +44,5 @@ namespace Yi.Framework.Infrastructure.Exceptions
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public BusinessException WithData(string name, object value)
|
|
||||||
{
|
|
||||||
Data[name] = value;
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,12 +0,0 @@
|
|||||||
//using Furion.DatabaseAccessor;
|
|
||||||
//using Microsoft.EntityFrameworkCore;
|
|
||||||
|
|
||||||
//namespace Yi.Furion.Rbac.EntityFramework.Core;
|
|
||||||
|
|
||||||
//[AppDbContext("Yi.Furion.Rbac", DbProvider.Sqlite)]
|
|
||||||
//public class DefaultDbContext : AppDbContext<DefaultDbContext>
|
|
||||||
//{
|
|
||||||
// public DefaultDbContext(DbContextOptions<DefaultDbContext> options) : base(options)
|
|
||||||
// {
|
|
||||||
// }
|
|
||||||
//}
|
|
||||||
@@ -16,7 +16,7 @@ public class Startup : AppStartup
|
|||||||
|
|
||||||
services.AddCorsAccessor();
|
services.AddCorsAccessor();
|
||||||
|
|
||||||
services.AddControllers().AddInject();
|
services.AddControllers().AddInjectWithUnifyResult();
|
||||||
|
|
||||||
services.AddEventBus();
|
services.AddEventBus();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,14 +0,0 @@
|
|||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Linq;
|
|
||||||
using System.Text;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
|
|
||||||
namespace Yi.Furion.Rbac.Web.Core
|
|
||||||
{
|
|
||||||
public class YiRESTfulResult<T>
|
|
||||||
{
|
|
||||||
|
|
||||||
public YiRESTfulResult() { }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,57 +0,0 @@
|
|||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Linq;
|
|
||||||
using System.Text;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
using Furion;
|
|
||||||
using Furion.DataValidation;
|
|
||||||
using Furion.FriendlyException;
|
|
||||||
using Furion.UnifyResult;
|
|
||||||
using Microsoft.AspNetCore.Http;
|
|
||||||
using Microsoft.AspNetCore.Mvc;
|
|
||||||
using Microsoft.AspNetCore.Mvc.Filters;
|
|
||||||
|
|
||||||
namespace Yi.Furion.Rbac.Web.Core
|
|
||||||
{
|
|
||||||
[UnifyModel(typeof(YiRESTfulResult<>))]
|
|
||||||
public class YiRESTfulResultProvider : IUnifyResultProvider
|
|
||||||
{
|
|
||||||
public IActionResult OnException(ExceptionContext context, ExceptionMetadata metadata)
|
|
||||||
{
|
|
||||||
return new JsonResult(new YiRESTfulResult<object>()
|
|
||||||
, UnifyContext.GetSerializerSettings(context)); // 当前行仅限 Furion 4.6.6+ 使用
|
|
||||||
}
|
|
||||||
|
|
||||||
public async Task OnResponseStatusCodes(HttpContext context, int statusCode, UnifyResultSettingsOptions unifyResultSettings = null)
|
|
||||||
{
|
|
||||||
UnifyContext.SetResponseStatusCodes(context, statusCode, unifyResultSettings);
|
|
||||||
|
|
||||||
switch (statusCode)
|
|
||||||
{
|
|
||||||
// 处理 401 状态码
|
|
||||||
case StatusCodes.Status401Unauthorized:
|
|
||||||
await context.Response.WriteAsJsonAsync(new YiRESTfulResult<object>()
|
|
||||||
, App.GetOptions<JsonOptions>()?.JsonSerializerOptions);
|
|
||||||
break;
|
|
||||||
// 处理 403 状态码
|
|
||||||
case StatusCodes.Status403Forbidden:
|
|
||||||
await context.Response.WriteAsJsonAsync(new YiRESTfulResult<object>()
|
|
||||||
, App.GetOptions<JsonOptions>()?.JsonSerializerOptions);
|
|
||||||
break;
|
|
||||||
default: break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public IActionResult OnSucceeded(ActionExecutedContext context, object data)
|
|
||||||
{
|
|
||||||
return new JsonResult(new YiRESTfulResult<object>()
|
|
||||||
, UnifyContext.GetSerializerSettings(context)); // 当前行仅限 Furion 4.6.6+ 使用
|
|
||||||
}
|
|
||||||
|
|
||||||
public IActionResult OnValidateFailed(ActionExecutingContext context, ValidationMetadata metadata)
|
|
||||||
{
|
|
||||||
return new JsonResult(new YiRESTfulResult<object>()
|
|
||||||
, UnifyContext.GetSerializerSettings(context)); // 当前行仅限 Furion 4.6.6+ 使用
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user