< Summary

Information
Class: Livekit.Server.Sdk.Dotnet.TwirpException
Assembly: LivekitApi
File(s): /home/runner/work/livekit-server-sdk-dotnet/livekit-server-sdk-dotnet/LivekitApi/TwirpException.cs
Line coverage
100%
Covered lines: 51
Uncovered lines: 0
Coverable lines: 51
Total lines: 111
Line coverage: 100%
Branch coverage
88%
Covered branches: 16
Total branches: 18
Branch coverage: 88.8%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.cctor()100%11100%
.ctor(...)100%22100%
FromResponse(...)83.33%1212100%
ParseCode(...)100%44100%

File(s)

/home/runner/work/livekit-server-sdk-dotnet/livekit-server-sdk-dotnet/LivekitApi/TwirpException.cs

#LineLine coverage
 1using System;
 2using System.Collections.Generic;
 3using Newtonsoft.Json.Linq;
 4
 5namespace Livekit.Server.Sdk.Dotnet
 6{
 7    /// <summary>
 8    /// Error returned by the LiveKit server for a service call. It derives from the generated
 9    /// <c>Twirp.Exception</c>, so existing <c>catch</c> blocks keep working, and adds the HTTP status and
 10    /// the <c>meta</c> map of the Twirp error (for example <c>sip_status_code</c> for SIP calls). The Twirp
 11    /// error code is in the inherited <c>Type</c> property.
 12    /// </summary>
 13    public class TwirpException : global::Twirp.Exception
 14    {
 115        private static readonly IReadOnlyDictionary<string, string> NoMeta =
 116            new Dictionary<string, string>();
 17
 18        /// <summary>
 19        /// HTTP status of the response, 0 when unknown.
 20        /// </summary>
 21        public int Status { get; }
 22
 23        /// <summary>
 24        /// The <c>meta</c> map of the Twirp error, empty when the server sent none.
 25        /// </summary>
 26        public IReadOnlyDictionary<string, string> Meta { get; }
 27
 28        /// <summary>
 29        /// Creates a new error.
 30        /// </summary>
 31        /// <param name="code">The Twirp error code.</param>
 32        /// <param name="message">The error message.</param>
 33        /// <param name="status">The HTTP status of the response, 0 when unknown.</param>
 34        /// <param name="meta">The <c>meta</c> map of the Twirp error.</param>
 35        public TwirpException(
 36            global::Twirp.ErrorCode code,
 37            string message,
 38            int status = 0,
 39            IReadOnlyDictionary<string, string> meta = null
 40        )
 1741            : base(code, message)
 1742        {
 43            Status = status;
 1744            Meta = meta ?? NoMeta;
 1745        }
 46
 47        /// <summary>
 48        /// Builds the error from a non-2xx response body, which is a Twirp JSON error
 49        /// (<c>{"code":"...","msg":"...","meta":{...}}</c>) or, from proxies, anything else.
 50        /// The service clients call it for every failed request; it is public for code that sends Twirp
 51        /// requests itself.
 52        /// </summary>
 53        /// <param name="body">The response body.</param>
 54        /// <param name="status">The HTTP status of the response.</param>
 55        /// <returns>The exception describing the error.</returns>
 56        public static TwirpException FromResponse(string body, int status)
 1757        {
 1758            var code = global::Twirp.ErrorCode.Unknown;
 1759            var message = body ?? "";
 1760            Dictionary<string, string> meta = null;
 61            try
 1762            {
 1763                var json = JObject.Parse(body);
 1564                var codeValue = (string)json["code"];
 1565                if (codeValue != null)
 1566                {
 1567                    code = ParseCode(codeValue);
 1568                }
 1569                var msg = (string)json["msg"];
 1570                if (msg != null)
 1571                {
 1572                    message = msg;
 1573                }
 1574                if (json["meta"] is JObject metaObject)
 375                {
 376                    meta = new Dictionary<string, string>();
 1977                    foreach (var property in metaObject.Properties())
 578                    {
 579                        meta[property.Name] =
 580                            property.Value.Type == JTokenType.Null
 581                                ? null
 582                                : property.Value.ToString();
 583                    }
 384                }
 1585            }
 286            catch (Exception)
 287            {
 88                // Not a Twirp error (HTML error page from a proxy, empty body...): keep the raw body
 289            }
 1790            return new TwirpException(code, message, status, meta);
 1791        }
 92
 93        // Twirp codes are lower-case ("permission_denied"); the generated enum keeps the
 94        // underscores except for "dataloss" (Data_Loss), so compare without them.
 95        private static global::Twirp.ErrorCode ParseCode(string value)
 1596        {
 1597            var normalized = value.Replace("_", "");
 1598            foreach (
 22699                global::Twirp.ErrorCode candidate in Enum.GetValues(typeof(global::Twirp.ErrorCode))
 100            )
 105101            {
 105102                var name = candidate.ToString().Replace("_", "");
 105103                if (string.Equals(name, normalized, StringComparison.OrdinalIgnoreCase))
 14104                {
 14105                    return candidate;
 106                }
 91107            }
 1108            return global::Twirp.ErrorCode.Unknown;
 15109        }
 110    }
 111}