We want to apply global error handling with a Request/Response pattern. Current code looks as below. Goal is to bring a custom Response object into the global error handling call. Is this possible? See custom response object below, and example of global error handling.
Current Code:
public async Task<ActionResult<GetAllProductResponse>> Get(ProductRequest productRequest)
{
try
{
var products = await ProductAppService.GetAllProducts();
var response = new GetAllProductResponse { Body = products };
return Ok(response);
}
catch (Exception ex)
{
logger.LogError(ex, ex.Message);
var response = new GetAllProductResponse { HasError = true, Error = ex.Message };
return StatusCode(StatusCodes.Status500InternalServerError, response);
}
}
public class GetAllDepartmentResponse : BaseRequestResponse<IEnumerable<ProductDto>>
{
}
public class BaseRequestResponse<T>
{
[Required, ValidateObject]
public T Body { get; set; }
public bool HasError { get; set; }
public string Error { get; set; }
}
}
**Goal code: How would I merge a Custom Response Object above with error Handling here? Is it possible? Want to pass as parameter object into global error handling, it can be Product, or Customer, Location, etc **
ASP.NET Core Web API exception handling
public class ErrorHandlingMiddleware
{
private readonly RequestDelegate next;
public ErrorHandlingMiddleware(RequestDelegate next)
{
this.next = next;
}
public async Task Invoke(HttpContext context /* other dependencies */)
{
try
{
await next(context);
}
catch (Exception ex)
{
await HandleExceptionAsync(context, ex);
}
}
private static Task HandleExceptionAsync(HttpContext context, Exception ex)
{
var code = HttpStatusCode.InternalServerError; // 500 if unexpected
if (ex is MyNotFoundException) code = HttpStatusCode.NotFound;
else if (ex is MyUnauthorizedException) code = HttpStatusCode.Unauthorized;
else if (ex is MyException) code = HttpStatusCode.BadRequest;
var result = JsonConvert.SerializeObject(new { error = ex.Message });
context.Response.ContentType = "application/json";
context.Response.StatusCode = (int)code;
return context.Response.WriteAsync(result);
}
}
Another resource: https://code-maze.com/global-error-handling-aspnetcore/