feat: Add raw parameter to SwissQrCodeController endpoints
- Add 'raw' query parameter to both ExtractFromFile and ExtractFromBase64 methods - Returns raw QR text lines when raw=true, parsed Bill object when raw=false (default) - Remove obsolete 'references' parameter (not part of QR extraction logic) - Add XML documentation for raw parameter - Update ExceptionHandlingMiddleware to handle BadRequestException
This commit is contained in:
@@ -11,21 +11,16 @@ namespace DocumentOperator.API.Middleware;
|
||||
/// Central exception handling middleware
|
||||
/// Maps exceptions to HTTP status codes and RFC 7807 Problem Details
|
||||
/// </summary>
|
||||
public class ExceptionHandlingMiddleware
|
||||
/// <remarks>
|
||||
/// Initializes a new instance of the <see cref="ExceptionHandlingMiddleware"/> class.
|
||||
/// </remarks>
|
||||
/// <param name="Next">The next middleware in the pipeline.</param>
|
||||
public class ExceptionHandlingMiddleware(RequestDelegate Next)
|
||||
{
|
||||
private readonly RequestDelegate _next;
|
||||
private readonly ILogger<ExceptionHandlingMiddleware> _logger;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="ExceptionHandlingMiddleware"/> class.
|
||||
/// </summary>
|
||||
/// <param name="next">The next middleware in the pipeline.</param>
|
||||
/// <param name="logger">The logger instance for exception logging.</param>
|
||||
public ExceptionHandlingMiddleware(RequestDelegate next, ILogger<ExceptionHandlingMiddleware> logger)
|
||||
private static readonly JsonSerializerOptions ProbDetailsJsonOpt = new()
|
||||
{
|
||||
_next = next;
|
||||
_logger = logger;
|
||||
}
|
||||
PropertyNamingPolicy = JsonNamingPolicy.CamelCase
|
||||
};
|
||||
|
||||
/// <summary>
|
||||
/// Invokes the middleware to handle incoming HTTP requests and catch exceptions.
|
||||
@@ -35,11 +30,10 @@ public class ExceptionHandlingMiddleware
|
||||
{
|
||||
try
|
||||
{
|
||||
await _next(context);
|
||||
await Next(context);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Unhandled exception: {ExceptionMessage}", ex.Message);
|
||||
await HandleExceptionAsync(context, ex);
|
||||
}
|
||||
}
|
||||
@@ -51,12 +45,7 @@ public class ExceptionHandlingMiddleware
|
||||
context.Response.StatusCode = (int)statusCode;
|
||||
context.Response.ContentType = "application/problem+json";
|
||||
|
||||
var options = new JsonSerializerOptions
|
||||
{
|
||||
PropertyNamingPolicy = JsonNamingPolicy.CamelCase
|
||||
};
|
||||
|
||||
await context.Response.WriteAsync(JsonSerializer.Serialize(problemDetails, options));
|
||||
await context.Response.WriteAsync(JsonSerializer.Serialize(problemDetails, ProbDetailsJsonOpt));
|
||||
}
|
||||
|
||||
private static (HttpStatusCode StatusCode, ProblemDetails ProblemDetails) MapExceptionToProblemDetails(
|
||||
@@ -78,15 +67,15 @@ public class ExceptionHandlingMiddleware
|
||||
}
|
||||
),
|
||||
|
||||
// Domain Validation Exception (400 Bad Request)
|
||||
DomainValidationException domainEx => (
|
||||
// Not Found Exception (404 Not Found)
|
||||
BadRequestException badReqEx => (
|
||||
HttpStatusCode.BadRequest,
|
||||
new ProblemDetails
|
||||
{
|
||||
Type = "https://datatracker.ietf.org/doc/html/rfc7231#section-6.5.1",
|
||||
Title = "Domain Validation Error",
|
||||
Type = "https://datatracker.ietf.org/doc/html/rfc7231#section-6.5.4",
|
||||
Title = "Bad Request",
|
||||
Status = (int)HttpStatusCode.BadRequest,
|
||||
Detail = domainEx.Message,
|
||||
Detail = badReqEx.Message,
|
||||
Instance = context.Request.Path
|
||||
}
|
||||
),
|
||||
@@ -117,19 +106,6 @@ public class ExceptionHandlingMiddleware
|
||||
}
|
||||
),
|
||||
|
||||
// PDF Processing Exception (500 Internal Server Error)
|
||||
PdfProcessingException pdfEx => (
|
||||
HttpStatusCode.InternalServerError,
|
||||
new ProblemDetails
|
||||
{
|
||||
Type = "https://datatracker.ietf.org/doc/html/rfc7231#section-6.6.1",
|
||||
Title = "PDF Processing Error",
|
||||
Status = (int)HttpStatusCode.InternalServerError,
|
||||
Detail = pdfEx.Message,
|
||||
Instance = context.Request.Path
|
||||
}
|
||||
),
|
||||
|
||||
// Generic Exception (500 Internal Server Error)
|
||||
_ => (
|
||||
HttpStatusCode.InternalServerError,
|
||||
|
||||
Reference in New Issue
Block a user