Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 101 additions & 0 deletions fundamentals/middleware/problem-details-service/Program.cs
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();
builder.Services.AddProblemDetails();

var app = builder.Build();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}

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());

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"
}
});
}
}
});

// endpoint for dividing numbers
app.MapGet("/divide", async (HttpContext context, double numerator, double denominator) =>
{
if (denominator == 0)
{
context.Features.Get<MathErrorFeature>()!.MathError = MathErrorType.DivisionByZeroError;
return Results.BadRequest();
}

var calculation = await Task.FromResult(numerator / denominator);
return Results.Ok(calculation);
});

// endpoint for obtaining the squareroot of a number
app.MapGet("/squareroot", async (HttpContext context, int radicand) =>
{
if (radicand < 0)
{
context.Features.Get<MathErrorFeature>()!.MathError = MathErrorType.NegativeRadicandError;
return Results.BadRequest();
}

var calculation = await Task.FromResult(Math.Sqrt(radicand));
return Results.Ok(calculation);
});

app.Run();

// Custom math errors
enum MathErrorType
{
DivisionByZeroError,
NegativeRadicandError
}

// Custom Http Request Feature
class MathErrorFeature
{
public MathErrorType MathError { get; set; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<RootNamespace>problem_detail_service</RootNamespace>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="7.0.0-preview.7.22376.6" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.2.3" />
</ItemGroup>

</Project>