Skip to content

Commit 2025980

Browse files
committed
update global exception handler
1 parent 6f08374 commit 2025980

File tree

4 files changed

+61
-45
lines changed

4 files changed

+61
-45
lines changed
+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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+
}

JixMinApi/Features/Todo/TodoEndpoints.cs

+6-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using JixMinApi.Features.Todo.Queries;
33
using MediatR;
44
using Microsoft.AspNetCore.Http.HttpResults;
5+
using Microsoft.AspNetCore.Mvc;
56
using Microsoft.EntityFrameworkCore;
67
using Microsoft.OpenApi.Models;
78
using System.Net.Mime;
@@ -40,12 +41,15 @@ public static void UseTodoEndpoints(this WebApplication app)
4041
group.MapGet("/{id}", GetTodoByIdAsync)
4142
.Produces<TodoDto>(StatusCodes.Status200OK)
4243
.Produces<HttpValidationProblemDetails>(StatusCodes.Status400BadRequest)
43-
.Produces(StatusCodes.Status404NotFound);
44+
.Produces(StatusCodes.Status404NotFound)
45+
.Produces<ProblemDetails>(StatusCodes.Status500InternalServerError);
46+
4447

4548
group.MapPost("/", CreateTodoAsync)
4649
.Accepts<CreateTodoDto>(MediaTypeNames.Application.Json)
4750
.Produces<TodoDto>(StatusCodes.Status201Created)
48-
.Produces<HttpValidationProblemDetails>(StatusCodes.Status400BadRequest);
51+
.Produces<HttpValidationProblemDetails>(StatusCodes.Status400BadRequest)
52+
.Produces<ProblemDetails>(StatusCodes.Status500InternalServerError);
4953

5054
//group.MapDelete("/{id}", GetAllTodosAsync)
5155
// .Produces(StatusCodes.Status204NoContent)

JixMinApi/Program.cs

+7-7
Original file line numberDiff line numberDiff line change
@@ -48,18 +48,18 @@
4848
builder.Services.AddHealthChecks();
4949

5050
var app = builder.Build();
51+
app.UseHttpsRedirection();
5152

52-
// Configure the HTTP request pipeline.
53-
if (app.Environment.IsDevelopment())
53+
if (!app.Environment.IsDevelopment())
5454
{
55-
app.UseSwagger();
56-
app.UseSwaggerUI();
55+
app.UseStatusCodePages();
56+
app.UseExceptionHandler();
5757
}
5858

59-
if (!app.Environment.IsDevelopment())
59+
if (app.Environment.IsDevelopment())
6060
{
61-
app.UseHttpsRedirection();
62-
app.UseExceptionHandler();
61+
app.UseSwagger();
62+
app.UseSwaggerUI();
6363
}
6464

6565
app.UseSerilogRequestLogging();

JixMinApi/Shared/GlobalExceptionHandler.cs

-36
This file was deleted.

0 commit comments

Comments
 (0)