| | 1 | | using Microsoft.AspNetCore.Http; |
| | 2 | | using Microsoft.AspNetCore.Mvc; |
| | 3 | |
|
| | 4 | | namespace Bookings.Infrastructure.RequestHandling |
| | 5 | | { |
| | 6 | | public abstract class RequestHandler |
| | 7 | | { |
| | 8 | | public static async Task<IResult> Execute(Func<IResult> handlerLogic, string operationName, string resourceIdent |
| 54 | 9 | | { |
| | 10 | | try |
| 54 | 11 | | { |
| 54 | 12 | | var result = handlerLogic(); |
| 42 | 13 | | return result ?? Results.Problem(new ProblemDetails |
| 42 | 14 | | { |
| 42 | 15 | | Title = $"{operationName} failed", |
| 42 | 16 | | Detail = $"Handler logic returned null for {operationName} of {resourceIdentifier}", |
| 42 | 17 | | Status = StatusCodes.Status500InternalServerError |
| 42 | 18 | | }); |
| | 19 | | } |
| 6 | 20 | | catch (ArgumentException ex) |
| 6 | 21 | | { |
| 6 | 22 | | return Results.Problem(new ProblemDetails |
| 6 | 23 | | { |
| 6 | 24 | | Title = "Invalid request", |
| 6 | 25 | | Detail = ex.Message, |
| 6 | 26 | | Status = StatusCodes.Status400BadRequest |
| 6 | 27 | | }); |
| | 28 | | } |
| 6 | 29 | | catch (Exception ex) |
| 6 | 30 | | { |
| 6 | 31 | | return Results.Problem(new ProblemDetails |
| 6 | 32 | | { |
| 6 | 33 | | Title = $"An error occurred during {operationName} of {resourceIdentifier}", |
| 6 | 34 | | Detail = ex.Message, |
| 6 | 35 | | Status = StatusCodes.Status500InternalServerError |
| 6 | 36 | | }); |
| | 37 | | } |
| 54 | 38 | | } |
| | 39 | | } |
| | 40 | | } |