| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="ProblemDetailsParser.cs" company="Stéphane ANDRE"> |
| | | 3 | | // Copyright (c) Stéphane ANDRE. All rights reserved. |
| | | 4 | | // </copyright> |
| | | 5 | | // ----------------------------------------------------------------------- |
| | | 6 | | |
| | | 7 | | using System; |
| | | 8 | | using System.Linq; |
| | | 9 | | using System.Text.Json; |
| | | 10 | | |
| | | 11 | | namespace MyNet.Http.Exceptions; |
| | | 12 | | |
| | | 13 | | /// <summary> |
| | | 14 | | /// Parses RFC 7807 problem details payloads into HTTP exceptions. |
| | | 15 | | /// </summary> |
| | | 16 | | public static class ProblemDetailsParser |
| | | 17 | | { |
| | | 18 | | /// <summary> |
| | | 19 | | /// Attempts to parse a problem details JSON payload into an exception. |
| | | 20 | | /// </summary> |
| | | 21 | | /// <param name="json">Problem details JSON payload.</param> |
| | | 22 | | /// <returns>A parsed exception, or <c>null</c> when the payload is not problem details.</returns> |
| | | 23 | | public static Exception? TryParseException(string? json) |
| | | 24 | | { |
| | 12 | 25 | | if (string.IsNullOrWhiteSpace(json)) |
| | | 26 | | { |
| | 0 | 27 | | return null; |
| | | 28 | | } |
| | | 29 | | |
| | | 30 | | try |
| | | 31 | | { |
| | 12 | 32 | | using var document = JsonDocument.Parse(json); |
| | 6 | 33 | | var root = document.RootElement; |
| | | 34 | | |
| | 6 | 35 | | if (root.ValueKind != JsonValueKind.Object) |
| | | 36 | | { |
| | 0 | 37 | | return null; |
| | | 38 | | } |
| | | 39 | | |
| | 6 | 40 | | var title = root.TryGetProperty("title", out var titleElement) && titleElement.ValueKind == JsonValueKind.St |
| | 6 | 41 | | ? titleElement.GetString() ?? string.Empty |
| | 6 | 42 | | : string.Empty; |
| | | 43 | | |
| | 6 | 44 | | if (!root.TryGetProperty("errors", out var errorsElement) || errorsElement.ValueKind != JsonValueKind.Object |
| | | 45 | | { |
| | 0 | 46 | | return string.IsNullOrWhiteSpace(title) ? null : new HttpException(title); |
| | | 47 | | } |
| | | 48 | | |
| | 6 | 49 | | var errors = (from property in errorsElement.EnumerateObject() |
| | 6 | 50 | | let message = property.Value.ValueKind switch |
| | 6 | 51 | | { |
| | 6 | 52 | | JsonValueKind.Array when property.Value.GetArrayLength() > 0 => property.Value[0].GetString(), |
| | 6 | 53 | | JsonValueKind.String => property.Value.GetString(), |
| | 6 | 54 | | _ => property.Value.ToString() |
| | 6 | 55 | | } |
| | 6 | 56 | | where !string.IsNullOrWhiteSpace(message) |
| | 6 | 57 | | select new HttpError(property.Name, message)).ToList(); |
| | | 58 | | |
| | 6 | 59 | | return errors.Count == 0 |
| | 6 | 60 | | ? (string.IsNullOrWhiteSpace(title) ? null : new HttpException(title)) |
| | 6 | 61 | | : new MultipleHttpException(title, errors); |
| | | 62 | | } |
| | 6 | 63 | | catch (JsonException) |
| | | 64 | | { |
| | 6 | 65 | | return null; |
| | | 66 | | } |
| | 12 | 67 | | } |
| | | 68 | | } |
| | | 69 | | |