-
Notifications
You must be signed in to change notification settings - Fork 161
Problem detail service middleware sample code #25
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Rick-Anderson
merged 20 commits into
dotnet:main
from
sammychinedu2ky:problem-detail-service-middleware
Sep 2, 2022
Merged
Changes from 5 commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
ccbbd54
created project
sammychinedu2ky 93e60a2
edited file
sammychinedu2ky 4c7e056
edited file
sammychinedu2ky 213b7d9
changed to project name
sammychinedu2ky fc9e707
changed file name
sammychinedu2ky f32307b
created mathErrorFeature variable
sammychinedu2ky 1b3968e
use of switch expressions
sammychinedu2ky f01e2e0
use statuscodepages
sammychinedu2ky f2b27b4
Merge branch 'main' into problem-detail-service-middleware
sammychinedu2ky 4a7e592
Merge branch 'problem-detail-service-middleware' of https://github.co…
sammychinedu2ky 9da3153
switched to using the statuscodepages middleware
sammychinedu2ky aa7edba
change to sync
sammychinedu2ky 89126b3
change to sync
sammychinedu2ky 047450e
change to sync
sammychinedu2ky 29d1468
change to sync
sammychinedu2ky f0fb0a1
added features extension
sammychinedu2ky e8e1496
dotnet format
sammychinedu2ky a148b47
dotnet format
sammychinedu2ky 8fd83b6
changed casing in proj
sammychinedu2ky 6d6fa78
changed casing of csproj
sammychinedu2ky File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
101 changes: 101 additions & 0 deletions
101
fundamentals/middleware/problem-details-service/Program.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
using Microsoft.AspNetCore.OpenApi; | ||
|
||
var builder = WebApplication.CreateBuilder(args); | ||
|
||
// Add services to the container. | ||
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle | ||
builder.Services.AddEndpointsApiExplorer(); | ||
builder.Services.AddSwaggerGen(); | ||
sammychinedu2ky marked this conversation as resolved.
Show resolved
Hide resolved
|
||
builder.Services.AddProblemDetails(); | ||
sammychinedu2ky marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
||
var app = builder.Build(); | ||
|
||
// Configure the HTTP request pipeline. | ||
if (app.Environment.IsDevelopment()) | ||
{ | ||
app.UseSwagger(); | ||
app.UseSwaggerUI(); | ||
} | ||
sammychinedu2ky marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
app.UseHttpsRedirection(); | ||
|
||
// middleware to handle writing problem details to the response | ||
app.Use(async (context, next) => | ||
{ | ||
// added the MathErrorFeature to the request pipeline | ||
context.Features.Set(new MathErrorFeature()); | ||
sammychinedu2ky marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
||
await next(context); | ||
|
||
if (context.RequestServices.GetService<IProblemDetailsService>() is { } problemDetailsService) | ||
{ | ||
MathErrorType matherror = context.Features.Get<MathErrorFeature>()!.MathError; | ||
if (matherror == MathErrorType.DivisionByZeroError) | ||
{ | ||
await problemDetailsService.WriteAsync(new ProblemDetailsContext | ||
{ | ||
HttpContext = context, | ||
ProblemDetails = new() | ||
{ | ||
Title = "Wrong Input", | ||
Detail = "The number you inputed is zero", | ||
Type = "https://en.wikipedia.org/wiki/Division_by_zero" | ||
} | ||
}); | ||
} | ||
if (matherror == MathErrorType.NegativeRadicandError) | ||
{ | ||
await problemDetailsService.WriteAsync(new ProblemDetailsContext | ||
{ | ||
HttpContext = context, | ||
ProblemDetails = new() | ||
{ | ||
Title = "Wrong Input", | ||
Detail = "Negative or complex numbers are not handled", | ||
Type = "https://en.wikipedia.org/wiki/Square_root" | ||
} | ||
}); | ||
} | ||
} | ||
sammychinedu2ky marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
}); | ||
|
||
// endpoint for dividing numbers | ||
app.MapGet("/divide", async (HttpContext context, double numerator, double denominator) => | ||
sammychinedu2ky marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
{ | ||
if (denominator == 0) | ||
{ | ||
context.Features.Get<MathErrorFeature>()!.MathError = MathErrorType.DivisionByZeroError; | ||
sammychinedu2ky marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
return Results.BadRequest(); | ||
} | ||
|
||
var calculation = await Task.FromResult(numerator / denominator); | ||
sammychinedu2ky marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
return Results.Ok(calculation); | ||
}); | ||
|
||
// endpoint for obtaining the squareroot of a number | ||
app.MapGet("/squareroot", async (HttpContext context, int radicand) => | ||
sammychinedu2ky marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
{ | ||
if (radicand < 0) | ||
{ | ||
context.Features.Get<MathErrorFeature>()!.MathError = MathErrorType.NegativeRadicandError; | ||
sammychinedu2ky marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
return Results.BadRequest(); | ||
} | ||
|
||
var calculation = await Task.FromResult(Math.Sqrt(radicand)); | ||
sammychinedu2ky marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
return Results.Ok(calculation); | ||
}); | ||
|
||
app.Run(); | ||
|
||
// Custom math errors | ||
enum MathErrorType | ||
{ | ||
DivisionByZeroError, | ||
NegativeRadicandError | ||
} | ||
|
||
// Custom Http Request Feature | ||
class MathErrorFeature | ||
{ | ||
public MathErrorType MathError { get; set; } | ||
} |
8 changes: 8 additions & 0 deletions
8
fundamentals/middleware/problem-details-service/appsettings.Development.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"Logging": { | ||
"LogLevel": { | ||
"Default": "Information", | ||
"Microsoft.AspNetCore": "Warning" | ||
} | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
fundamentals/middleware/problem-details-service/appsettings.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"Logging": { | ||
"LogLevel": { | ||
"Default": "Information", | ||
"Microsoft.AspNetCore": "Warning" | ||
} | ||
}, | ||
"AllowedHosts": "*" | ||
} |
15 changes: 15 additions & 0 deletions
15
fundamentals/middleware/problem-details-service/problem-details-service.csproj
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<Project Sdk="Microsoft.NET.Sdk.Web"> | ||
sammychinedu2ky marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
<PropertyGroup> | ||
<TargetFramework>net7.0</TargetFramework> | ||
<Nullable>enable</Nullable> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<RootNamespace>problem_detail_service</RootNamespace> | ||
sammychinedu2ky marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="7.0.0-preview.7.22376.6" /> | ||
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.2.3" /> | ||
</ItemGroup> | ||
sammychinedu2ky marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
</Project> |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.