From ccbbd542d4b847dbb46b8e51fb09eaa0bbe0972a Mon Sep 17 00:00:00 2001 From: Samson Amaugo Date: Fri, 26 Aug 2022 10:26:16 +0100 Subject: [PATCH 01/18] created project --- .../problem-detail-service/Program.cs | 46 +++++++++++++++++++ .../appsettings.Development.json | 8 ++++ .../problem-detail-service/appsettings.json | 9 ++++ .../problem-detail-service.csproj | 15 ++++++ 4 files changed, 78 insertions(+) create mode 100644 fundamentals/middleware/problem-detail-service/Program.cs create mode 100644 fundamentals/middleware/problem-detail-service/appsettings.Development.json create mode 100644 fundamentals/middleware/problem-detail-service/appsettings.json create mode 100644 fundamentals/middleware/problem-detail-service/problem-detail-service.csproj diff --git a/fundamentals/middleware/problem-detail-service/Program.cs b/fundamentals/middleware/problem-detail-service/Program.cs new file mode 100644 index 00000000..d736de09 --- /dev/null +++ b/fundamentals/middleware/problem-detail-service/Program.cs @@ -0,0 +1,46 @@ +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(); + +var app = builder.Build(); + +// Configure the HTTP request pipeline. +if (app.Environment.IsDevelopment()) +{ + app.UseSwagger(); + app.UseSwaggerUI(); +} + +app.UseHttpsRedirection(); + +var summaries = new[] +{ + "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching" +}; + +app.MapGet("/weatherforecast", () => +{ + var forecast = Enumerable.Range(1, 5).Select(index => + new WeatherForecast + ( + DateOnly.FromDateTime(DateTime.Now.AddDays(index)), + Random.Shared.Next(-20, 55), + summaries[Random.Shared.Next(summaries.Length)] + )) + .ToArray(); + return forecast; +}) +.WithName("GetWeatherForecast") +.WithOpenApi(); + +app.Run(); + +internal record WeatherForecast(DateOnly Date, int TemperatureC, string? Summary) +{ + public int TemperatureF => 32 + (int)(TemperatureC / 0.5556); +} diff --git a/fundamentals/middleware/problem-detail-service/appsettings.Development.json b/fundamentals/middleware/problem-detail-service/appsettings.Development.json new file mode 100644 index 00000000..0c208ae9 --- /dev/null +++ b/fundamentals/middleware/problem-detail-service/appsettings.Development.json @@ -0,0 +1,8 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + } +} diff --git a/fundamentals/middleware/problem-detail-service/appsettings.json b/fundamentals/middleware/problem-detail-service/appsettings.json new file mode 100644 index 00000000..10f68b8c --- /dev/null +++ b/fundamentals/middleware/problem-detail-service/appsettings.json @@ -0,0 +1,9 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + }, + "AllowedHosts": "*" +} diff --git a/fundamentals/middleware/problem-detail-service/problem-detail-service.csproj b/fundamentals/middleware/problem-detail-service/problem-detail-service.csproj new file mode 100644 index 00000000..e8597a0b --- /dev/null +++ b/fundamentals/middleware/problem-detail-service/problem-detail-service.csproj @@ -0,0 +1,15 @@ + + + + net7.0 + enable + enable + problem_detail_service + + + + + + + + From 93e60a26f7306afaa5c7e7d0dd360e51ada81bc6 Mon Sep 17 00:00:00 2001 From: Samson Amaugo Date: Fri, 26 Aug 2022 10:38:39 +0100 Subject: [PATCH 02/18] edited file --- .../problem-detail-service/Program.cs | 89 +++++++++++++++---- 1 file changed, 71 insertions(+), 18 deletions(-) diff --git a/fundamentals/middleware/problem-detail-service/Program.cs b/fundamentals/middleware/problem-detail-service/Program.cs index d736de09..bbd8622b 100644 --- a/fundamentals/middleware/problem-detail-service/Program.cs +++ b/fundamentals/middleware/problem-detail-service/Program.cs @@ -18,29 +18,82 @@ app.UseHttpsRedirection(); -var summaries = new[] +// 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() is { } problemDetailsService) + { + MathErrorType matherror = context.Features.Get()!.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) => { - "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching" -}; + if (denominator == 0) + { + context.Features.Get()!.MathError = MathErrorType.DivisionByZeroError; + return Results.BadRequest(); + } + + var calculation = await Task.FromResult(numerator / denominator); + return Results.Ok(calculation); +}); -app.MapGet("/weatherforecast", () => +// endpoint for obtaining the squareroot of a number +app.MapGet("/squareroot", async (HttpContext context, int radicand) => { - var forecast = Enumerable.Range(1, 5).Select(index => - new WeatherForecast - ( - DateOnly.FromDateTime(DateTime.Now.AddDays(index)), - Random.Shared.Next(-20, 55), - summaries[Random.Shared.Next(summaries.Length)] - )) - .ToArray(); - return forecast; -}) -.WithName("GetWeatherForecast") -.WithOpenApi(); + if (radicand < 0) + { + context.Features.Get()!.MathError = MathErrorType.NegativeRadicandError; + return Results.BadRequest(); + } + + var calculation = await Task.FromResult(Math.Sqrt(radicand)); + return Results.Ok(calculation); +}); app.Run(); -internal record WeatherForecast(DateOnly Date, int TemperatureC, string? Summary) +// Custom math errors +enum MathErrorType +{ + DivisionByZeroError, + NegativeRadicandError +} + +// Custom Http Request Feature +class MathErrorFeature { - public int TemperatureF => 32 + (int)(TemperatureC / 0.5556); + public MathErrorType MathError { get; set; } } From 4c7e056a256b2868fb3fd94748c181c49c7a7749 Mon Sep 17 00:00:00 2001 From: Samson Amaugo Date: Fri, 26 Aug 2022 10:43:07 +0100 Subject: [PATCH 03/18] edited file --- fundamentals/middleware/problem-detail-service/Program.cs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/fundamentals/middleware/problem-detail-service/Program.cs b/fundamentals/middleware/problem-detail-service/Program.cs index bbd8622b..e8190fbb 100644 --- a/fundamentals/middleware/problem-detail-service/Program.cs +++ b/fundamentals/middleware/problem-detail-service/Program.cs @@ -6,6 +6,7 @@ // 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(); @@ -19,7 +20,8 @@ app.UseHttpsRedirection(); // middleware to handle writing problem details to the response -app.Use(async(context, next)=>{ +app.Use(async (context, next) => +{ // added the MathErrorFeature to the request pipeline context.Features.Set(new MathErrorFeature()); From 213b7d905932738d951d458e129db358bb433aba Mon Sep 17 00:00:00 2001 From: Samson Amaugo Date: Fri, 26 Aug 2022 19:44:10 +0100 Subject: [PATCH 04/18] changed to project name --- ...oblem-detail-service.csproj => problem-details-service.csproj} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename fundamentals/middleware/problem-detail-service/{problem-detail-service.csproj => problem-details-service.csproj} (100%) diff --git a/fundamentals/middleware/problem-detail-service/problem-detail-service.csproj b/fundamentals/middleware/problem-detail-service/problem-details-service.csproj similarity index 100% rename from fundamentals/middleware/problem-detail-service/problem-detail-service.csproj rename to fundamentals/middleware/problem-detail-service/problem-details-service.csproj From fc9e707812eb0af32eb134bf55a1b8455d36afe4 Mon Sep 17 00:00:00 2001 From: Samson Amaugo Date: Fri, 26 Aug 2022 19:47:16 +0100 Subject: [PATCH 05/18] changed file name --- .../Program.cs | 0 .../appsettings.Development.json | 0 .../appsettings.json | 0 .../problem-details-service.csproj | 0 4 files changed, 0 insertions(+), 0 deletions(-) rename fundamentals/middleware/{problem-detail-service => problem-details-service}/Program.cs (100%) rename fundamentals/middleware/{problem-detail-service => problem-details-service}/appsettings.Development.json (100%) rename fundamentals/middleware/{problem-detail-service => problem-details-service}/appsettings.json (100%) rename fundamentals/middleware/{problem-detail-service => problem-details-service}/problem-details-service.csproj (100%) diff --git a/fundamentals/middleware/problem-detail-service/Program.cs b/fundamentals/middleware/problem-details-service/Program.cs similarity index 100% rename from fundamentals/middleware/problem-detail-service/Program.cs rename to fundamentals/middleware/problem-details-service/Program.cs diff --git a/fundamentals/middleware/problem-detail-service/appsettings.Development.json b/fundamentals/middleware/problem-details-service/appsettings.Development.json similarity index 100% rename from fundamentals/middleware/problem-detail-service/appsettings.Development.json rename to fundamentals/middleware/problem-details-service/appsettings.Development.json diff --git a/fundamentals/middleware/problem-detail-service/appsettings.json b/fundamentals/middleware/problem-details-service/appsettings.json similarity index 100% rename from fundamentals/middleware/problem-detail-service/appsettings.json rename to fundamentals/middleware/problem-details-service/appsettings.json diff --git a/fundamentals/middleware/problem-detail-service/problem-details-service.csproj b/fundamentals/middleware/problem-details-service/problem-details-service.csproj similarity index 100% rename from fundamentals/middleware/problem-detail-service/problem-details-service.csproj rename to fundamentals/middleware/problem-details-service/problem-details-service.csproj From f32307b8488a812c7ea15fa2fb010626998c40ba Mon Sep 17 00:00:00 2001 From: Samson Amaugo Date: Thu, 1 Sep 2022 07:27:01 +0100 Subject: [PATCH 06/18] created mathErrorFeature variable Co-authored-by: Bruno Oliveira --- fundamentals/middleware/problem-details-service/Program.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/fundamentals/middleware/problem-details-service/Program.cs b/fundamentals/middleware/problem-details-service/Program.cs index e8190fbb..38ad67f6 100644 --- a/fundamentals/middleware/problem-details-service/Program.cs +++ b/fundamentals/middleware/problem-details-service/Program.cs @@ -23,7 +23,8 @@ app.Use(async (context, next) => { // added the MathErrorFeature to the request pipeline - context.Features.Set(new MathErrorFeature()); + var mathErrorFeature = new MathErrorFeature(); + context.Features.Set(mathErrorFeature); await next(context); From 1b3968e4bd286ca434c03149f4f18f0f8a2f593f Mon Sep 17 00:00:00 2001 From: Samson Amaugo Date: Thu, 1 Sep 2022 07:31:28 +0100 Subject: [PATCH 07/18] use of switch expressions Co-authored-by: Bruno Oliveira --- .../problem-details-service/Program.cs | 28 +++++++------------ 1 file changed, 10 insertions(+), 18 deletions(-) diff --git a/fundamentals/middleware/problem-details-service/Program.cs b/fundamentals/middleware/problem-details-service/Program.cs index 38ad67f6..a244803f 100644 --- a/fundamentals/middleware/problem-details-service/Program.cs +++ b/fundamentals/middleware/problem-details-service/Program.cs @@ -28,32 +28,24 @@ await next(context); - if (context.RequestServices.GetService() is { } problemDetailsService) + if (context.Response.StatusCode > 399) { - MathErrorType matherror = context.Features.Get()!.MathError; - if (matherror == MathErrorType.DivisionByZeroError) + if (context.RequestServices.GetService() is { } problemDetailsService) { - await problemDetailsService.WriteAsync(new ProblemDetailsContext + (string Detail, string Type) details = mathErrorFeature.MathError switch { - 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) - { + MathErrorType.DivisionByZeroError => ("The number you inputed is zero", "https://en.wikipedia.org/wiki/Division_by_zero"), + _ => ("Negative or complex numbers are not handled", "https://en.wikipedia.org/wiki/Square_root") + }; + await problemDetailsService.WriteAsync(new ProblemDetailsContext { HttpContext = context, - ProblemDetails = new() + ProblemDetails = { Title = "Wrong Input", - Detail = "Negative or complex numbers are not handled", - Type = "https://en.wikipedia.org/wiki/Square_root" + Detail = details.Detail, + Type = details.Type } }); } From f01e2e087e9df1a692913b2465aa00c8fcad9a8c Mon Sep 17 00:00:00 2001 From: Samson Amaugo Date: Thu, 1 Sep 2022 07:54:13 +0100 Subject: [PATCH 08/18] use statuscodepages Co-authored-by: Bruno Oliveira --- .../problem-details-service/Program.cs | 20 ++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/fundamentals/middleware/problem-details-service/Program.cs b/fundamentals/middleware/problem-details-service/Program.cs index a244803f..ed9af6b6 100644 --- a/fundamentals/middleware/problem-details-service/Program.cs +++ b/fundamentals/middleware/problem-details-service/Program.cs @@ -6,7 +6,25 @@ // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle builder.Services.AddEndpointsApiExplorer(); builder.Services.AddSwaggerGen(); -builder.Services.AddProblemDetails(); +builder.Services.AddProblemDetails(options => + options.CustomizeProblemDetails = (context) => + { + var mathErrorFeature = context.HttpContext.Features.Get(); + + if (mathErrorFeature != null) + { + (string Detail, string Type) details = mathErrorFeature.MathError switch + { + MathErrorType.DivisionByZeroError => ("The number you inputed is zero", "https://en.wikipedia.org/wiki/Division_by_zero"), + _ => ("Negative or complex numbers are not handled", "https://en.wikipedia.org/wiki/Square_root") + }; + + context.ProblemDetails.Type = details.Type; + context.ProblemDetails.Title = "Wrong Input"; + context.ProblemDetails.Detail = details.Detail; + } + } +); var app = builder.Build(); From 9da3153def5df2464d170af175f46aac43330068 Mon Sep 17 00:00:00 2001 From: Samson Amaugo Date: Thu, 1 Sep 2022 08:11:11 +0100 Subject: [PATCH 09/18] switched to using the statuscodepages middleware --- .../problem-details-service/Program.cs | 24 ++----------------- 1 file changed, 2 insertions(+), 22 deletions(-) diff --git a/fundamentals/middleware/problem-details-service/Program.cs b/fundamentals/middleware/problem-details-service/Program.cs index ed9af6b6..fd93c709 100644 --- a/fundamentals/middleware/problem-details-service/Program.cs +++ b/fundamentals/middleware/problem-details-service/Program.cs @@ -46,29 +46,9 @@ await next(context); - if (context.Response.StatusCode > 399) - { - if (context.RequestServices.GetService() is { } problemDetailsService) - { - (string Detail, string Type) details = mathErrorFeature.MathError switch - { - MathErrorType.DivisionByZeroError => ("The number you inputed is zero", "https://en.wikipedia.org/wiki/Division_by_zero"), - _ => ("Negative or complex numbers are not handled", "https://en.wikipedia.org/wiki/Square_root") - }; + }); - await problemDetailsService.WriteAsync(new ProblemDetailsContext - { - HttpContext = context, - ProblemDetails = - { - Title = "Wrong Input", - Detail = details.Detail, - Type = details.Type - } - }); - } - } -}); +app.UseStatusCodePages(); // endpoint for dividing numbers app.MapGet("/divide", async (HttpContext context, double numerator, double denominator) => From aa7edba9af384e7de565fb917567fc7c6528d72e Mon Sep 17 00:00:00 2001 From: Samson Amaugo Date: Thu, 1 Sep 2022 21:55:16 +0100 Subject: [PATCH 10/18] change to sync Co-authored-by: Bruno Oliveira --- fundamentals/middleware/problem-details-service/Program.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/fundamentals/middleware/problem-details-service/Program.cs b/fundamentals/middleware/problem-details-service/Program.cs index fd93c709..112f0a10 100644 --- a/fundamentals/middleware/problem-details-service/Program.cs +++ b/fundamentals/middleware/problem-details-service/Program.cs @@ -51,7 +51,7 @@ app.UseStatusCodePages(); // endpoint for dividing numbers -app.MapGet("/divide", async (HttpContext context, double numerator, double denominator) => +app.MapGet("/divide", (HttpContext context, double numerator, double denominator) => { if (denominator == 0) { From 89126b32aeb07f49dd071a9e6c8e3da558f92e96 Mon Sep 17 00:00:00 2001 From: Samson Amaugo Date: Thu, 1 Sep 2022 21:55:38 +0100 Subject: [PATCH 11/18] change to sync Co-authored-by: Bruno Oliveira --- fundamentals/middleware/problem-details-service/Program.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/fundamentals/middleware/problem-details-service/Program.cs b/fundamentals/middleware/problem-details-service/Program.cs index 112f0a10..7415e090 100644 --- a/fundamentals/middleware/problem-details-service/Program.cs +++ b/fundamentals/middleware/problem-details-service/Program.cs @@ -64,7 +64,7 @@ }); // endpoint for obtaining the squareroot of a number -app.MapGet("/squareroot", async (HttpContext context, int radicand) => +app.MapGet("/squareroot", (HttpContext context, int radicand) => { if (radicand < 0) { From 047450e42b38b6c3777d58756b531945e15b991f Mon Sep 17 00:00:00 2001 From: Samson Amaugo Date: Thu, 1 Sep 2022 21:56:16 +0100 Subject: [PATCH 12/18] change to sync Co-authored-by: Bruno Oliveira --- fundamentals/middleware/problem-details-service/Program.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/fundamentals/middleware/problem-details-service/Program.cs b/fundamentals/middleware/problem-details-service/Program.cs index 7415e090..2df42117 100644 --- a/fundamentals/middleware/problem-details-service/Program.cs +++ b/fundamentals/middleware/problem-details-service/Program.cs @@ -72,7 +72,7 @@ return Results.BadRequest(); } - var calculation = await Task.FromResult(Math.Sqrt(radicand)); + var calculation = Math.Sqrt(radicand); return Results.Ok(calculation); }); From 29d1468c89a098019d384d6ae8bf0c429609be92 Mon Sep 17 00:00:00 2001 From: Samson Amaugo Date: Thu, 1 Sep 2022 21:56:28 +0100 Subject: [PATCH 13/18] change to sync Co-authored-by: Bruno Oliveira --- fundamentals/middleware/problem-details-service/Program.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/fundamentals/middleware/problem-details-service/Program.cs b/fundamentals/middleware/problem-details-service/Program.cs index 2df42117..16ce62b4 100644 --- a/fundamentals/middleware/problem-details-service/Program.cs +++ b/fundamentals/middleware/problem-details-service/Program.cs @@ -59,7 +59,7 @@ return Results.BadRequest(); } - var calculation = await Task.FromResult(numerator / denominator); + var calculation = numerator / denominator; return Results.Ok(calculation); }); From f0fb0a1168e0d04b579f0d39edba26ff2a08492f Mon Sep 17 00:00:00 2001 From: Samson Amaugo Date: Thu, 1 Sep 2022 22:43:23 +0100 Subject: [PATCH 14/18] added features extension --- fundamentals/middleware/problem-details-service/Program.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/fundamentals/middleware/problem-details-service/Program.cs b/fundamentals/middleware/problem-details-service/Program.cs index 16ce62b4..cc132ba2 100644 --- a/fundamentals/middleware/problem-details-service/Program.cs +++ b/fundamentals/middleware/problem-details-service/Program.cs @@ -1,4 +1,4 @@ -using Microsoft.AspNetCore.OpenApi; +using Microsoft.AspNetCore.Http.Features; var builder = WebApplication.CreateBuilder(args); @@ -55,7 +55,7 @@ { if (denominator == 0) { - context.Features.Get()!.MathError = MathErrorType.DivisionByZeroError; + context.Features.GetRequiredFeature().MathError = MathErrorType.DivisionByZeroError; return Results.BadRequest(); } @@ -68,7 +68,7 @@ { if (radicand < 0) { - context.Features.Get()!.MathError = MathErrorType.NegativeRadicandError; + context.Features.GetRequiredFeature().MathError = MathErrorType.NegativeRadicandError; return Results.BadRequest(); } From e8e1496df924a35daea5bc1870291740ddd91d42 Mon Sep 17 00:00:00 2001 From: Samson Amaugo Date: Thu, 1 Sep 2022 22:45:27 +0100 Subject: [PATCH 15/18] dotnet format --- .../middleware/problem-details-service/Program.cs | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/fundamentals/middleware/problem-details-service/Program.cs b/fundamentals/middleware/problem-details-service/Program.cs index cc132ba2..41564d85 100644 --- a/fundamentals/middleware/problem-details-service/Program.cs +++ b/fundamentals/middleware/problem-details-service/Program.cs @@ -1,4 +1,4 @@ -using Microsoft.AspNetCore.Http.Features; +using Microsoft.AspNetCore.Http.Features; var builder = WebApplication.CreateBuilder(args); @@ -6,8 +6,8 @@ // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle builder.Services.AddEndpointsApiExplorer(); builder.Services.AddSwaggerGen(); -builder.Services.AddProblemDetails(options => - options.CustomizeProblemDetails = (context) => +builder.Services.AddProblemDetails(options => + options.CustomizeProblemDetails = (context) => { var mathErrorFeature = context.HttpContext.Features.Get(); @@ -43,10 +43,8 @@ // added the MathErrorFeature to the request pipeline var mathErrorFeature = new MathErrorFeature(); context.Features.Set(mathErrorFeature); - await next(context); - - }); +}); app.UseStatusCodePages(); From a148b475730fb371b9a5b047e6b5752a3338904a Mon Sep 17 00:00:00 2001 From: Samson Amaugo Date: Thu, 1 Sep 2022 22:50:50 +0100 Subject: [PATCH 16/18] dotnet format --- .../problem-details-service/Program.cs | 25 ++++++++----------- 1 file changed, 10 insertions(+), 15 deletions(-) diff --git a/fundamentals/middleware/problem-details-service/Program.cs b/fundamentals/middleware/problem-details-service/Program.cs index 41564d85..c9ba79d3 100644 --- a/fundamentals/middleware/problem-details-service/Program.cs +++ b/fundamentals/middleware/problem-details-service/Program.cs @@ -9,22 +9,17 @@ builder.Services.AddProblemDetails(options => options.CustomizeProblemDetails = (context) => { - var mathErrorFeature = context.HttpContext.Features.Get(); - - if (mathErrorFeature != null) + var mathErrorFeature = context.HttpContext.Features.GetRequiredFeature(); + (string Detail, string Type) details = mathErrorFeature.MathError switch { - (string Detail, string Type) details = mathErrorFeature.MathError switch - { - MathErrorType.DivisionByZeroError => ("The number you inputed is zero", "https://en.wikipedia.org/wiki/Division_by_zero"), - _ => ("Negative or complex numbers are not handled", "https://en.wikipedia.org/wiki/Square_root") - }; - - context.ProblemDetails.Type = details.Type; - context.ProblemDetails.Title = "Wrong Input"; - context.ProblemDetails.Detail = details.Detail; - } - } -); + MathErrorType.DivisionByZeroError => ("The number you inputed is zero", "https://en.wikipedia.org/wiki/Division_by_zero"), + _ => ("Negative or complex numbers are not handled", "https://en.wikipedia.org/wiki/Square_root") + }; + + context.ProblemDetails.Type = details.Type; + context.ProblemDetails.Title = "Wrong Input"; + context.ProblemDetails.Detail = details.Detail; + }); var app = builder.Build(); From 8fd83b63f9ba7f07807c5b33c9a101c921a445ff Mon Sep 17 00:00:00 2001 From: Samson Amaugo Date: Thu, 1 Sep 2022 23:19:11 +0100 Subject: [PATCH 17/18] changed casing in proj Co-authored-by: Bruno Oliveira --- .../problem-details-service/problem-details-service.csproj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/fundamentals/middleware/problem-details-service/problem-details-service.csproj b/fundamentals/middleware/problem-details-service/problem-details-service.csproj index e8597a0b..f4dc5bdd 100644 --- a/fundamentals/middleware/problem-details-service/problem-details-service.csproj +++ b/fundamentals/middleware/problem-details-service/problem-details-service.csproj @@ -4,7 +4,7 @@ net7.0 enable enable - problem_detail_service + ProblemDetailsServiceSample From 6d6fa789e3187d9524c21ff1e49bf5d093efbd9a Mon Sep 17 00:00:00 2001 From: Samson Amaugo Date: Thu, 1 Sep 2022 23:23:05 +0100 Subject: [PATCH 18/18] changed casing of csproj --- ...problem-details-service.csproj => ProblemDetailService.csproj} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename fundamentals/middleware/problem-details-service/{problem-details-service.csproj => ProblemDetailService.csproj} (100%) diff --git a/fundamentals/middleware/problem-details-service/problem-details-service.csproj b/fundamentals/middleware/problem-details-service/ProblemDetailService.csproj similarity index 100% rename from fundamentals/middleware/problem-details-service/problem-details-service.csproj rename to fundamentals/middleware/problem-details-service/ProblemDetailService.csproj