public ExceptionFilter(IServiceProvider serviceProvider) { exceptionProcessModule = serviceProvider.GetService <IExceptionProcessModule>(); if (exceptionProcessModule == null) { var options = serviceProvider.GetService <ExceptionOptions>(); if (options == null) { options = new ExceptionOptions(); } exceptionProcessModule = new BasicExceptionProcessModule(options); } exceptionLogModule = serviceProvider.GetService <IExceptionLogModule>(); exceptionResultModule = serviceProvider.GetService <IExceptionResultModule>(); }
/// <summary> /// /// </summary> /// <param name="services"></param> internal void BuildServices(IServiceCollection services) { if (processAction != null) { processAction.Invoke(services); } else { services.AddSingleton <IExceptionProcessModule, BasicExceptionProcessModule>(); } if (EnableLogger) { if (loggerAction != null) { loggerAction.Invoke(services); } else { services.AddSingleton <IExceptionLogModule, BasicExceptionLogModule>(); } } if (resultAction != null) { resultAction.Invoke(services); } else { services.AddSingleton <IExceptionResultModule, BasicExceptionResultModule>(); } var options = new ExceptionOptions() { UseOkStatus = UseOkStatus, ExceptionTypes = typeList }; services.AddSingleton(options); }
public BasicExceptionProcessModule(ExceptionOptions options) { defaultHttpStatus = options.UseOkStatus ? 200 : 400; useOkStatus = options.UseOkStatus; var baselist = new List <ExceptonTypeModel> { new ExceptonTypeModel(typeof(AuthorizeException), (exception) => { var typeEx = (AuthorizeException)exception; var model = new ExceptionModel() { Code = 40100, Message = typeEx.Message, LogType = LogType.LogMessage }; if (!useOkStatus) { model.HttpStatus = 401; } return(model); }), new ExceptonTypeModel(typeof(PermissionException), (exception) => { var typeEx = (PermissionException)exception; var model = new ExceptionModel() { Code = 40300, Message = typeEx.Message, LogType = LogType.LogMessage }; if (!useOkStatus) { model.HttpStatus = 403; } return(model); }), new ExceptonTypeModel(typeof(ParameterException), (exception) => { var typeEx = (ParameterException)exception; var model = new ExceptionModel() { Code = 40000, Message = typeEx.Message, LogType = LogType.NoLog, Errors = typeEx.Errors }; return(model); }), new ExceptonTypeModel(typeof(Exception), (exception) => { var model = new ExceptionModel() { Code = 50000, Message = exception.Message, LogType = LogType.LogFullException | LogType.LogPostData | LogType.LogTraceId }; if (exception is IMultiError multi) { model.Errors = multi.Errors; } return(model); }) }; typeModels = Combine(baselist, options.ExceptionTypes); } List <ExceptonTypeModel> Combine(List <ExceptonTypeModel> baselist, List <ExceptonTypeModel> list) { if (list == null) { return(baselist); } for (int i = 0; i < list.Count; i++) { var typeModel = list[i]; bool flag = false; for (int j = 0; j < baselist.Count; j++) { var typeModel1 = baselist[j]; if (typeModel.ExceptionType == typeModel1.ExceptionType) { baselist[j] = typeModel; flag = true; break; } else if (typeModel.ExceptionType.IsSubclassOf(typeModel1.ExceptionType)) { baselist.Insert(j, typeModel); flag = true; break; } } if (!flag) { baselist.Add(typeModel); } } return(baselist); }