< Summary

Information
Class: MyNet.Http.Exceptions.ProblemDetailsParser
Assembly: MyNet.Http
File(s): https://raw.githubusercontent.com/sandre58/MyNet/5aaaab311aa14541478b9e402357d98b657a23e4/src/MyNet.Http/Exceptions/ProblemDetailsParser.cs
Tag: 342_31701152804
Line coverage
88%
Covered lines: 23
Uncovered lines: 3
Coverable lines: 26
Total lines: 69
Line coverage: 88.4%
Branch coverage
40%
Covered branches: 8
Total branches: 20
Branch coverage: 40%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
TryParseException(...)40%212088.46%

File(s)

https://raw.githubusercontent.com/sandre58/MyNet/5aaaab311aa14541478b9e402357d98b657a23e4/src/MyNet.Http/Exceptions/ProblemDetailsParser.cs

#LineLine coverage
 1// -----------------------------------------------------------------------
 2// <copyright file="ProblemDetailsParser.cs" company="Stéphane ANDRE">
 3// Copyright (c) Stéphane ANDRE. All rights reserved.
 4// </copyright>
 5// -----------------------------------------------------------------------
 6
 7using System;
 8using System.Linq;
 9using System.Text.Json;
 10
 11namespace MyNet.Http.Exceptions;
 12
 13/// <summary>
 14/// Parses RFC 7807 problem details payloads into HTTP exceptions.
 15/// </summary>
 16public 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    {
 1225        if (string.IsNullOrWhiteSpace(json))
 26        {
 027            return null;
 28        }
 29
 30        try
 31        {
 1232            using var document = JsonDocument.Parse(json);
 633            var root = document.RootElement;
 34
 635            if (root.ValueKind != JsonValueKind.Object)
 36            {
 037                return null;
 38            }
 39
 640            var title = root.TryGetProperty("title", out var titleElement) && titleElement.ValueKind == JsonValueKind.St
 641                ? titleElement.GetString() ?? string.Empty
 642                : string.Empty;
 43
 644            if (!root.TryGetProperty("errors", out var errorsElement) || errorsElement.ValueKind != JsonValueKind.Object
 45            {
 046                return string.IsNullOrWhiteSpace(title) ? null : new HttpException(title);
 47            }
 48
 649            var errors = (from property in errorsElement.EnumerateObject()
 650                let message = property.Value.ValueKind switch
 651                {
 652                    JsonValueKind.Array when property.Value.GetArrayLength() > 0 => property.Value[0].GetString(),
 653                    JsonValueKind.String => property.Value.GetString(),
 654                    _ => property.Value.ToString()
 655                }
 656                where !string.IsNullOrWhiteSpace(message)
 657                select new HttpError(property.Name, message)).ToList();
 58
 659            return errors.Count == 0
 660                ? (string.IsNullOrWhiteSpace(title) ? null : new HttpException(title))
 661                : new MultipleHttpException(title, errors);
 62        }
 663        catch (JsonException)
 64        {
 665            return null;
 66        }
 1267    }
 68}
 69