| | | 1 | | using Microsoft.Extensions.Options; |
| | | 2 | | |
| | | 3 | | namespace ZhEaIsNsAaBn.Utilities.APIExceptionHandlers; |
| | | 4 | | |
| | | 5 | | /// <summary>Handles <see cref="JsonException"/> (bad request body) with an <c>InvalidJson</c> error, category <c>Valida |
| | | 6 | | public sealed class JsonExceptionHandler : BaseExceptionHandler<JsonExceptionHandler>, IExceptionHandler |
| | | 7 | | { |
| | | 8 | | /// <summary>Creates the handler.</summary> |
| | | 9 | | /// <param name="logger">The logger.</param> |
| | | 10 | | /// <param name="environment">The host environment.</param> |
| | | 11 | | /// <param name="settings">The status mapping.</param> |
| | | 12 | | public JsonExceptionHandler( ILogger<JsonExceptionHandler> logger, IWebHostEnvironment environment, IOptions<APIResp |
| | 192 | 13 | | : base( logger, environment, settings ) { } |
| | | 14 | | |
| | | 15 | | |
| | | 16 | | /// <inheritdoc /> |
| | | 17 | | public async ValueTask<bool> TryHandleAsync( HttpContext httpContext, Exception exception, CancellationToken cancell |
| | | 18 | | { |
| | 92 | 19 | | ArgumentNullException.ThrowIfNull( httpContext ); |
| | | 20 | | |
| | 176 | 21 | | if ( exception is not JsonException jsonException ) { return false; } |
| | | 22 | | |
| | | 23 | | |
| | 8 | 24 | | Log.JsonError( Logger, jsonException, httpContext.Request.Path, httpContext.Request.Method, httpContext.TraceIde |
| | | 25 | | |
| | | 26 | | |
| | 8 | 27 | | var error = new Error( "InvalidJson", "Validation", "صيغة JSON غير صالحة أو نوع بيانات غير مطابق في جسم الطلب.", |
| | 8 | 28 | | "Invalid JSON format or data type mismatch in request body.", Severity.Medium ); |
| | | 29 | | |
| | | 30 | | |
| | 8 | 31 | | await GenerateError( httpContext, error, cancellationToken ).ConfigureAwait( false ); |
| | | 32 | | |
| | 8 | 33 | | return true; |
| | 92 | 34 | | } |
| | | 35 | | } |