|
3 | 3 |
|
4 | 4 | using EasyExtensions.Abstractions; |
5 | 5 | using EasyExtensions.Models; |
6 | | -using Microsoft.AspNetCore.Mvc; |
| 6 | +using Microsoft.AspNetCore.WebUtilities; |
7 | 7 | using System; |
8 | 8 | using System.Collections.Generic; |
9 | 9 | using System.Diagnostics; |
@@ -53,28 +53,33 @@ public class WebApiException( |
53 | 53 | /// <returns> Error model. </returns> |
54 | 54 | public ErrorModel GetErrorModel() |
55 | 55 | { |
56 | | - ProblemDetails details = new ProblemDetails() |
57 | | - { |
58 | | - Status = (int)StatusCode, |
59 | | - Type = "https://tools.ietf.org/html/rfc7231#section-6.5.1", |
60 | | - Title = "Web API actions errors occurred.", |
61 | | - }; |
62 | | - string? traceId = Activity.Current?.Id ?? "-"; |
63 | | - if (!string.IsNullOrWhiteSpace(traceId)) |
64 | | - { |
65 | | - details.Extensions["traceId"] = traceId; |
66 | | - } |
| 56 | + int statusCode = (int)StatusCode; |
67 | 57 | return new() |
68 | 58 | { |
69 | | - Status = (int)StatusCode, |
70 | | - Type = "https://tools.ietf.org/html/rfc7231#section-6.5.1", |
71 | | - Title = "Web API actions errors occurred.", |
72 | | - TraceId = Activity.Current?.Id ?? "-", |
| 59 | + Status = statusCode, |
| 60 | + Type = GetRfcType(StatusCode), |
| 61 | + Title = ReasonPhrases.GetReasonPhrase(statusCode), |
| 62 | + TraceId = Activity.Current?.Id ?? string.Empty, |
73 | 63 | Errors = new Dictionary<string, string> |
74 | 64 | { |
75 | 65 | { ObjectName, Message } |
76 | 66 | } |
77 | 67 | }; |
78 | 68 | } |
| 69 | + |
| 70 | + private static string GetRfcType(HttpStatusCode statusCode) |
| 71 | + => statusCode switch |
| 72 | + { |
| 73 | + HttpStatusCode.BadRequest => "https://tools.ietf.org/html/rfc7231#section-6.5.1", |
| 74 | + HttpStatusCode.Unauthorized => "https://tools.ietf.org/html/rfc7235#section-3.1", |
| 75 | + HttpStatusCode.Forbidden => "https://tools.ietf.org/html/rfc7231#section-6.5.3", |
| 76 | + HttpStatusCode.NotFound => "https://tools.ietf.org/html/rfc7231#section-6.5.4", |
| 77 | + HttpStatusCode.Conflict => "https://tools.ietf.org/html/rfc7231#section-6.5.8", |
| 78 | + HttpStatusCode.UnprocessableEntity => "https://tools.ietf.org/html/rfc4918#section-11.2", |
| 79 | + HttpStatusCode.TooManyRequests => "https://tools.ietf.org/html/rfc6585#section-4", |
| 80 | + HttpStatusCode.InternalServerError => "https://tools.ietf.org/html/rfc7231#section-6.6.1", |
| 81 | + HttpStatusCode.ServiceUnavailable => "https://tools.ietf.org/html/rfc7231#section-6.6.4", |
| 82 | + _ => "https://tools.ietf.org/html/rfc7231" |
| 83 | + }; |
79 | 84 | } |
80 | 85 | } |
0 commit comments