Skip to content

Commit 4791b9b

Browse files
committed
refactor: condense exception handler configuration for conciseness
1 parent 8c8c501 commit 4791b9b

1 file changed

Lines changed: 21 additions & 24 deletions

File tree

src/ApiService/BookStore.ApiService/Program.cs

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -78,36 +78,33 @@ static async Task WaitForProjectionsAsync(IDocumentStore store, ILogger logger)
7878
// Configure the HTTP request pipeline
7979
if (app.Environment.IsDevelopment())
8080
{
81-
app.UseExceptionHandler(exceptionHandlerApp =>
81+
_ = app.UseExceptionHandler(exceptionHandlerApp => exceptionHandlerApp.Run(async context =>
8282
{
83-
exceptionHandlerApp.Run(async context =>
83+
var exceptionHandlerFeature = context.Features.Get<Microsoft.AspNetCore.Diagnostics.IExceptionHandlerFeature>();
84+
if (exceptionHandlerFeature is not null)
8485
{
85-
var exceptionHandlerFeature = context.Features.Get<Microsoft.AspNetCore.Diagnostics.IExceptionHandlerFeature>();
86-
if (exceptionHandlerFeature is not null)
86+
var logger = context.RequestServices.GetRequiredService<ILogger<Program>>();
87+
var exception = exceptionHandlerFeature.Error;
88+
89+
logger.LogError(exception, "Unhandled exception: {Message}", exception.Message);
90+
91+
context.Response.StatusCode = StatusCodes.Status500InternalServerError;
92+
context.Response.ContentType = "application/problem+json";
93+
94+
await context.Response.WriteAsJsonAsync(new
8795
{
88-
var logger = context.RequestServices.GetRequiredService<ILogger<Program>>();
89-
var exception = exceptionHandlerFeature.Error;
90-
91-
logger.LogError(exception, "Unhandled exception: {Message}", exception.Message);
92-
93-
context.Response.StatusCode = StatusCodes.Status500InternalServerError;
94-
context.Response.ContentType = "application/problem+json";
95-
96-
await context.Response.WriteAsJsonAsync(new
97-
{
98-
type = "https://tools.ietf.org/html/rfc9110#section-15.6.1",
99-
title = "An error occurred while processing your request.",
100-
status = StatusCodes.Status500InternalServerError,
101-
detail = exception.Message,
102-
stackTrace = exception.StackTrace
103-
});
104-
}
105-
});
106-
});
96+
type = "https://tools.ietf.org/html/rfc9110#section-15.6.1",
97+
title = "An error occurred while processing your request.",
98+
status = StatusCodes.Status500InternalServerError,
99+
detail = exception.Message,
100+
stackTrace = exception.StackTrace
101+
});
102+
}
103+
}));
107104
}
108105
else
109106
{
110-
app.UseExceptionHandler();
107+
_ = app.UseExceptionHandler();
111108
}
112109

113110
// Add request localization middleware

0 commit comments

Comments
 (0)