Skip to content

Commit

Permalink
✨ 431 enhanced problem details (#443)
Browse files Browse the repository at this point in the history
  • Loading branch information
danielmackay authored Nov 18, 2024
1 parent 996dad5 commit 6233f2f
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 6 deletions.
2 changes: 1 addition & 1 deletion docs/adr/20241118-produce-useful-sql-server-exceptions.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Produce useful SQL Server exceptions

- Status: approved
- Status: accepted
- Deciders: Daniel Mackay
- Date: 2024-11-18
- Tags: ef-core
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Replace AutoMapper with manual mapping

- Status: Approved
- Status: accepted
- Deciders: Daniel Mackay, Matt Goldman
- Date: 2024-11-18
- Tags: mappers
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Use Vogen to simplify strongly typed IDs

- Status: approved
- Status: accepted
- Deciders: Daniel Mackay
- Date: 2024-11-19
- Tags: ddd, vogen, strongly-typed-ids
Expand Down
22 changes: 22 additions & 0 deletions src/WebApi/Extensions/CustomProblemDetailsExt.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using Microsoft.AspNetCore.Http.Features;

namespace SSW.CleanArchitecture.WebApi.Extensions;

public static class CustomProblemDetailsExt
{
public static void AddCustomProblemDetails(this IServiceCollection services)
{
services.AddProblemDetails(options =>
{
options.CustomizeProblemDetails = context =>
{
context.ProblemDetails.Instance = $"{context.HttpContext.Request.Method} {context.HttpContext.Request.Path}";

context.ProblemDetails.Extensions.TryAdd("requestId", context.HttpContext.TraceIdentifier);

var activity = context.HttpContext.Features.Get<IHttpActivityFeature>()?.Activity;
context.ProblemDetails.Extensions.TryAdd("traceId", activity?.Id);
};
});
}
}
11 changes: 11 additions & 0 deletions src/WebApi/Extensions/CustomScalarExt.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using Scalar.AspNetCore;

namespace SSW.CleanArchitecture.WebApi.Extensions;

public static class CustomScalarExt
{
public static void MapCustomScalarApiReference(this IEndpointRouteBuilder endpoints)
{
endpoints.MapScalarApiReference(options => options.WithDefaultHttpClient(ScalarTarget.CSharp, ScalarClient.HttpClient));
}
}
5 changes: 2 additions & 3 deletions src/WebApi/Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using Scalar.AspNetCore;
using SSW.CleanArchitecture.Application;
using SSW.CleanArchitecture.Infrastructure;
using SSW.CleanArchitecture.WebApi;
Expand All @@ -9,7 +8,7 @@
var builder = WebApplication.CreateBuilder(args);

builder.AddServiceDefaults();
builder.Services.AddProblemDetails();
builder.Services.AddCustomProblemDetails();

builder.Services.AddWebApi(builder.Configuration);
builder.Services.AddApplication();
Expand All @@ -29,7 +28,7 @@
}

app.MapOpenApi();
app.MapScalarApiReference(options => options.WithDefaultHttpClient(ScalarTarget.CSharp, ScalarClient.HttpClient));
app.MapCustomScalarApiReference();
app.UseHealthChecks();
app.UseHttpsRedirection();
app.UseStaticFiles();
Expand Down

0 comments on commit 6233f2f

Please sign in to comment.