You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hello!
Is there some kind of exception handling and api response? For example, if there is a command validation error, an exception is traced to the controller method: throw new InvalidCommandException(errors.Select(x => x.ErrorMessage).ToList());
then the api method returns a 500 error with exception details, which is incorrect in a product environment. In this case, BadRequest with details of validation errors is more suitable. Any filter that is applied to a controller method that allows you to catch exceptions does not work at the controller level, I assume that this is due to the lifetime of the IMediator in the UserAccessModule for example:
public async Task<TResult> ExecuteQueryAsync<TResult>(IQuery<TResult> query) { using (var scope = UserAccessCompositionRoot.BeginLifetimeScope()) { var mediator = scope.Resolve<IMediator>(); return await mediator.Send(query); } }
My filter exception:
`
public ApiExceptionFilterAttribute()
{
_exceptionHandlers = new Dictionary<Type, Action<ExceptionContext>>
{
{ typeof(ValidationException), HandleValidationException },
{ typeof(NotFoundException), HandleNotFoundException },
{ typeof(UnauthorizedAccessException), HandleUnauthorizedAccessException },
{ typeof(ForbiddenAccessException), HandleForbiddenAccessException },
};
}
public override void OnException(ExceptionContext context)
{
HandleException(context);
base.OnException(context);
}
private void HandleException(ExceptionContext context)
{
Type type = context.Exception.GetType();
if (_exceptionHandlers.ContainsKey(type))
{
_exceptionHandlers[type].Invoke(context);
return;
}
if (!context.ModelState.IsValid)
{
HandleInvalidModelStateException(context);
return;
}
}
private void HandleValidationException(ExceptionContext context)
{
var exception = (ValidationException)context.Exception;
var details = new ValidationProblemDetails(exception.Errors)
{
Type = "https://tools.ietf.org/html/rfc7231#section-6.5.1"
};
context.Result = new BadRequestObjectResult(details);
context.ExceptionHandled = true;
}`
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Hello!
Is there some kind of exception handling and api response? For example, if there is a command validation error, an exception is traced to the controller method:
throw new InvalidCommandException(errors.Select(x => x.ErrorMessage).ToList());
then the api method returns a 500 error with exception details, which is incorrect in a product environment. In this case, BadRequest with details of validation errors is more suitable. Any filter that is applied to a controller method that allows you to catch exceptions does not work at the controller level, I assume that this is due to the lifetime of the IMediator in the UserAccessModule for example:
public async Task<TResult> ExecuteQueryAsync<TResult>(IQuery<TResult> query) { using (var scope = UserAccessCompositionRoot.BeginLifetimeScope()) { var mediator = scope.Resolve<IMediator>(); return await mediator.Send(query); } }
My filter exception:
`
Beta Was this translation helpful? Give feedback.
All reactions