|
1 | 1 | using JsonApiToolkit.Models.Errors; |
2 | 2 | using Microsoft.AspNetCore.Mvc; |
3 | 3 | using Microsoft.AspNetCore.Mvc.Filters; |
4 | | -using Microsoft.Extensions.Hosting; |
5 | 4 | using Microsoft.Extensions.Logging; |
6 | 5 |
|
7 | 6 | namespace JsonApiToolkit.Filters; |
8 | 7 |
|
9 | 8 | /// <summary> |
10 | | -/// Exception filter that transforms unhandled exceptions into JSON:API compliant error responses. |
| 9 | +/// Exception filter that transforms known and unknown exceptions into JSON:API compliant error responses. |
11 | 10 | /// </summary> |
12 | | -/// <remarks> |
13 | | -/// <para> |
14 | | -/// This filter ensures that all unhandled exceptions in JSON:API controllers result in properly formatted |
15 | | -/// JSON:API error responses rather than the default ASP.NET Core error format. |
16 | | -/// </para> |
17 | | -/// <para> |
18 | | -/// In development environments, the filter includes detailed exception information in the response. |
19 | | -/// In production environments, it provides a generic error message to avoid exposing sensitive details. |
20 | | -/// </para> |
21 | | -/// <para> |
22 | | -/// The filter automatically logs all exceptions using the provided ILogger instance. |
23 | | -/// </para> |
24 | | -/// </remarks> |
25 | | -/// <param name="logger">Logger for recording exception details</param> |
26 | | -/// <param name="environment">Host environment to determine the level of error detail</param> |
27 | | -public class JsonApiExceptionFilter( |
28 | | - ILogger<JsonApiExceptionFilter> logger, |
29 | | - IHostEnvironment environment |
30 | | -) : IExceptionFilter |
| 11 | +public class JsonApiExceptionFilter(ILogger<JsonApiExceptionFilter> logger) : IExceptionFilter |
31 | 12 | { |
32 | 13 | private readonly ILogger<JsonApiExceptionFilter> _logger = logger; |
33 | | - private readonly IHostEnvironment _environment = environment; |
34 | 14 |
|
35 | 15 | /// <summary> |
36 | | - /// Transforms an unhandled exception into a standardized JSON:API error response. |
| 16 | + /// Handles exceptions thrown during the execution of a controller action. |
37 | 17 | /// </summary> |
38 | | - /// <param name="context">The exception context containing the exception and controller context</param> |
| 18 | + /// <param name="context">The context of the exception.</param> |
39 | 19 | /// <remarks> |
40 | | - /// This method: |
41 | | - /// <list type="number"> |
42 | | - /// <item> |
43 | | - /// <description>Logs the exception using the configured logger</description> |
44 | | - /// </item> |
45 | | - /// <item> |
46 | | - /// <description>Creates a JSON:API error object with a 500 status code</description> |
47 | | - /// </item> |
48 | | - /// <item> |
49 | | - /// <description>Sets appropriate error detail based on the environment (detailed in development, generic in production)</description> |
50 | | - /// </item> |
51 | | - /// <item> |
52 | | - /// <description>Returns the error response and marks the exception as handled</description> |
53 | | - /// </item> |
54 | | - /// </list> |
55 | 20 | /// <para> |
56 | | - /// The resulting error response follows the JSON:API specification for error objects. |
| 21 | + /// This method inspects the exception and determines the appropriate HTTP status code |
| 22 | + /// and error message to return in the JSON:API error response. |
| 23 | + /// </para> |
| 24 | + /// <para> |
| 25 | + /// It handles known exceptions (e.g., JsonApiBadRequestException, JsonApiNotFoundException) |
| 26 | + /// and logs unexpected exceptions (500 Internal Server Error). |
57 | 27 | /// </para> |
58 | 28 | /// </remarks> |
59 | 29 | public void OnException(ExceptionContext context) |
60 | 30 | { |
61 | | - _logger.LogError(context.Exception, "An unhandled exception occurred"); |
| 31 | + var (status, title) = context.Exception switch |
| 32 | + { |
| 33 | + JsonApiBadRequestException => (400, "Bad Request"), |
| 34 | + JsonApiNotFoundException => (404, "Not Found"), |
| 35 | + JsonApiConflictException => (409, "Conflict"), |
| 36 | + JsonApiUnauthorizedException => (401, "Unauthorized"), |
| 37 | + JsonApiForbiddenException => (403, "Forbidden"), |
| 38 | + _ => (500, "Internal Server Error"), |
| 39 | + }; |
| 40 | + |
| 41 | + if (status == 500) |
| 42 | + { |
| 43 | + // Log full stack trace for unexpected errors |
| 44 | + _logger.LogError(context.Exception, "An unhandled exception occurred"); |
| 45 | + } |
| 46 | + else |
| 47 | + { |
| 48 | + // Log only the message for handled exceptions |
| 49 | + _logger.LogInformation( |
| 50 | + "Handled JSON:API exception: {Type} - {Message}", |
| 51 | + context.Exception.GetType().Name, |
| 52 | + context.Exception.Message |
| 53 | + ); |
| 54 | + } |
62 | 55 |
|
63 | 56 | var error = new JsonApiError |
64 | 57 | { |
65 | | - Status = "500", |
66 | | - Title = "Internal Server Error", |
67 | | - Detail = _environment.IsDevelopment() |
68 | | - ? context.Exception.Message |
69 | | - : "An error occurred while processing your request.", |
| 58 | + Status = status.ToString(), |
| 59 | + Title = title, |
| 60 | + Detail = |
| 61 | + status != 500 |
| 62 | + ? context.Exception.Message |
| 63 | + : "An error occurred while processing your request.", |
70 | 64 | }; |
71 | 65 |
|
72 | 66 | var response = new JsonApiErrorResponse { Errors = [error] }; |
73 | 67 |
|
74 | | - context.Result = new ObjectResult(response) { StatusCode = 500 }; |
| 68 | + context.Result = new ObjectResult(response) { StatusCode = status }; |
75 | 69 | context.ExceptionHandled = true; |
76 | 70 | } |
77 | 71 | } |
0 commit comments