From 1bd99457aae593ccd3a53e4c8cfbe5eace20b16e Mon Sep 17 00:00:00 2001 From: Samson Amaugo Date: Sat, 13 May 2023 11:20:08 +0100 Subject: [PATCH] added sampleproblemdetailswriter sample Signed-off-by: Samson Amaugo --- .../problem-details-service/Program.cs | 84 +++++++++++++++++++ 1 file changed, 84 insertions(+) diff --git a/fundamentals/middleware/problem-details-service/Program.cs b/fundamentals/middleware/problem-details-service/Program.cs index b2aa895..32a64a9 100644 --- a/fundamentals/middleware/problem-details-service/Program.cs +++ b/fundamentals/middleware/problem-details-service/Program.cs @@ -315,4 +315,88 @@ else app.MapControllers(); app.Run(); // + + +#elif SampleProblemDetailsWriter +// +var builder = WebApplication.CreateBuilder(args); + +// Add services to the container. +// addd SampleProblemDetailsWriter to dependency injection +builder.Services.AddTransient(); + +var app = builder.Build(); + + + +// Middleware to handle writing problem details to the response. +app.Use(async (context, next) => +{ + await next(context); + var mathErrorFeature = context.Features.Get(); + if (mathErrorFeature is not null) + { + if (context.RequestServices.GetService() is + { } problemDetailsService) + { + + if (problemDetailsService.CanWrite(new ProblemDetailsContext() { HttpContext = context })) + { + (string Detail, string Type) details = mathErrorFeature.MathError switch + { + MathErrorType.DivisionByZeroError => ("Divison by zero is not defined.", + "https://en.wikipedia.org/wiki/Division_by_zero"), + _ => ("Negative or complex numbers are not valid input.", + "https://en.wikipedia.org/wiki/Square_root") + }; + + await problemDetailsService.WriteAsync(new ProblemDetailsContext + { + HttpContext = context, + ProblemDetails = + { + Title = "Bad Input", + Detail = details.Detail, + Type = details.Type + } + }); + } + } + } +}); + +// /divide?numerator=2&denominator=4 +app.MapGet("/divide", (HttpContext context, double numerator, double denominator) => +{ + if (denominator == 0) + { + var errorType = new MathErrorFeature + { + MathError = MathErrorType.DivisionByZeroError + }; + context.Features.Set(errorType); + return Results.BadRequest(); + } + + return Results.Ok(numerator / denominator); +}); + +// /squareroot?radicand=16 +app.MapGet("/squareroot", (HttpContext context, double radicand) => +{ + if (radicand < 0) + { + var errorType = new MathErrorFeature + { + MathError = MathErrorType.NegativeRadicandError + }; + context.Features.Set(errorType); + return Results.BadRequest(); + } + + return Results.Ok(Math.Sqrt(radicand)); +}); + +app.Run(); +// #endif