From ae3dc6e0d665abd913875aa1c35f8a29fa993ade Mon Sep 17 00:00:00 2001 From: Rick Anderson <3605364+Rick-Anderson@users.noreply.github.com> Date: Tue, 27 Sep 2022 12:05:38 -1000 Subject: [PATCH] Pd18 (#75) * fix snip * add new snippet * add new snippet --- .../Controllers/ValuesController.cs | 10 +++++----- .../problem-details-service/Program.cs | 19 +++++++++++++++---- 2 files changed, 20 insertions(+), 9 deletions(-) diff --git a/fundamentals/middleware/problem-details-service/Controllers/ValuesController.cs b/fundamentals/middleware/problem-details-service/Controllers/ValuesController.cs index b78f0a3..23556e5 100644 --- a/fundamentals/middleware/problem-details-service/Controllers/ValuesController.cs +++ b/fundamentals/middleware/problem-details-service/Controllers/ValuesController.cs @@ -15,7 +15,7 @@ public class Values2Controller : ControllerBase return BadRequest(); } - return Ok(Numerator/Denominator); + return Ok(Numerator / Denominator); } // /api/values2 /squareroot/4 @@ -51,7 +51,7 @@ public class ValuesController : ControllerBase return BadRequest(); } - return Ok(Numerator/Denominator); + return Ok(Numerator / Denominator); } // /api/values/squareroot/4 @@ -98,7 +98,7 @@ public class Values3Controller : ControllerBase ); } - return Ok(Numerator/Denominator); + return Ok(Numerator / Denominator); } // /api/values3/squareroot/4 @@ -131,7 +131,7 @@ public class Values3Controller : ControllerBase [ApiController] public class Values4Controller : ControllerBase { - // /api/values3/divide/1/2 + // /api/values4/divide/1/2 [HttpGet("{Numerator}/{Denominator}")] public IActionResult Divide(double Numerator, double Denominator) { @@ -142,6 +142,6 @@ public class Values4Controller : ControllerBase [HttpGet("{radicand}")] public IActionResult Squareroot(double radicand) { - return Ok(Math.Sqrt(radicand)); + return Ok(Math.Sqrt(radicand)); } } diff --git a/fundamentals/middleware/problem-details-service/Program.cs b/fundamentals/middleware/problem-details-service/Program.cs index 4a98ca5..1758600 100644 --- a/fundamentals/middleware/problem-details-service/Program.cs +++ b/fundamentals/middleware/problem-details-service/Program.cs @@ -279,22 +279,33 @@ else context.Response.ContentType = Text.Plain; + var title = "Bad Input"; + var detail = "Invalid input"; + var type = "https://errors.example.com/badInput"; + if (context.RequestServices.GetService() is { } problemDetailsService) { var exceptionHandlerFeature = context.Features.Get(); - // Examine exceptionHandlerFeature?.Error for more details. + var exceptionType = exceptionHandlerFeature?.Error; + if (exceptionType != null && + exceptionType.Message.Contains("infinity")) + { + title = "Arguement exception"; + detail = "Invalid input"; + type = "https://errors.example.com/arguementException"; + } await problemDetailsService.WriteAsync(new ProblemDetailsContext { HttpContext = context, ProblemDetails = { - Title = "Bad Input", - Detail = "Invalid input", - Type = "https://tools.ietf.org/html/rfc7231#section-6.6.1" + Title = title, + Detail = detail, + Type = type } }); }