File tree 4 files changed +61
-45
lines changed
4 files changed +61
-45
lines changed Original file line number Diff line number Diff line change
1
+ using System . Diagnostics ;
2
+ using Microsoft . AspNetCore . Diagnostics ;
3
+ using Microsoft . AspNetCore . Mvc ;
4
+
5
+ namespace JixMinApi . Shared ;
6
+
7
+
8
+ public class GlobalExceptionHandler ( ILogger < GlobalExceptionHandler > logger ) : IExceptionHandler
9
+ {
10
+ private readonly ILogger < GlobalExceptionHandler > logger = logger ;
11
+
12
+ public async ValueTask < bool > TryHandleAsync (
13
+ HttpContext httpContext ,
14
+ Exception exception ,
15
+ CancellationToken cancellationToken )
16
+ {
17
+ var traceId = Activity . Current ? . Id ?? httpContext . TraceIdentifier ;
18
+
19
+ logger . LogError (
20
+ exception ,
21
+ "Could not process a request on machine {MachineName}. TraceId: {TraceId}" ,
22
+ Environment . MachineName ,
23
+ traceId
24
+ ) ;
25
+
26
+ var ( statusCode , title ) = MapException ( exception ) ;
27
+
28
+ await Results . Problem (
29
+ title : title ,
30
+ statusCode : statusCode ,
31
+ extensions : new Dictionary < string , object ? >
32
+ {
33
+ { "traceId" , traceId }
34
+ }
35
+ ) . ExecuteAsync ( httpContext ) ;
36
+
37
+ return true ;
38
+ }
39
+
40
+ private static ( int StatusCode , string Title ) MapException ( Exception exception )
41
+ {
42
+ return exception switch
43
+ {
44
+ ArgumentOutOfRangeException => ( StatusCodes . Status400BadRequest , exception . Message ) ,
45
+ _ => ( StatusCodes . Status500InternalServerError , "Server error" )
46
+ } ;
47
+ }
48
+ }
Original file line number Diff line number Diff line change 2
2
using JixMinApi . Features . Todo . Queries ;
3
3
using MediatR ;
4
4
using Microsoft . AspNetCore . Http . HttpResults ;
5
+ using Microsoft . AspNetCore . Mvc ;
5
6
using Microsoft . EntityFrameworkCore ;
6
7
using Microsoft . OpenApi . Models ;
7
8
using System . Net . Mime ;
@@ -40,12 +41,15 @@ public static void UseTodoEndpoints(this WebApplication app)
40
41
group . MapGet ( "/{id}" , GetTodoByIdAsync )
41
42
. Produces < TodoDto > ( StatusCodes . Status200OK )
42
43
. Produces < HttpValidationProblemDetails > ( StatusCodes . Status400BadRequest )
43
- . Produces ( StatusCodes . Status404NotFound ) ;
44
+ . Produces ( StatusCodes . Status404NotFound )
45
+ . Produces < ProblemDetails > ( StatusCodes . Status500InternalServerError ) ;
46
+
44
47
45
48
group . MapPost ( "/" , CreateTodoAsync )
46
49
. Accepts < CreateTodoDto > ( MediaTypeNames . Application . Json )
47
50
. Produces < TodoDto > ( StatusCodes . Status201Created )
48
- . Produces < HttpValidationProblemDetails > ( StatusCodes . Status400BadRequest ) ;
51
+ . Produces < HttpValidationProblemDetails > ( StatusCodes . Status400BadRequest )
52
+ . Produces < ProblemDetails > ( StatusCodes . Status500InternalServerError ) ;
49
53
50
54
//group.MapDelete("/{id}", GetAllTodosAsync)
51
55
// .Produces(StatusCodes.Status204NoContent)
Original file line number Diff line number Diff line change 48
48
builder . Services . AddHealthChecks ( ) ;
49
49
50
50
var app = builder . Build ( ) ;
51
+ app . UseHttpsRedirection ( ) ;
51
52
52
- // Configure the HTTP request pipeline.
53
- if ( app . Environment . IsDevelopment ( ) )
53
+ if ( ! app . Environment . IsDevelopment ( ) )
54
54
{
55
- app . UseSwagger ( ) ;
56
- app . UseSwaggerUI ( ) ;
55
+ app . UseStatusCodePages ( ) ;
56
+ app . UseExceptionHandler ( ) ;
57
57
}
58
58
59
- if ( ! app . Environment . IsDevelopment ( ) )
59
+ if ( app . Environment . IsDevelopment ( ) )
60
60
{
61
- app . UseHttpsRedirection ( ) ;
62
- app . UseExceptionHandler ( ) ;
61
+ app . UseSwagger ( ) ;
62
+ app . UseSwaggerUI ( ) ;
63
63
}
64
64
65
65
app . UseSerilogRequestLogging ( ) ;
Load Diff This file was deleted.
You can’t perform that action at this time.
0 commit comments