| | | 1 | | using System; |
| | | 2 | | using System.Collections.Generic; |
| | | 3 | | using Newtonsoft.Json.Linq; |
| | | 4 | | |
| | | 5 | | namespace 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 | | { |
| | 1 | 15 | | private static readonly IReadOnlyDictionary<string, string> NoMeta = |
| | 1 | 16 | | 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 | | ) |
| | 17 | 41 | | : base(code, message) |
| | 17 | 42 | | { |
| | | 43 | | Status = status; |
| | 17 | 44 | | Meta = meta ?? NoMeta; |
| | 17 | 45 | | } |
| | | 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) |
| | 17 | 57 | | { |
| | 17 | 58 | | var code = global::Twirp.ErrorCode.Unknown; |
| | 17 | 59 | | var message = body ?? ""; |
| | 17 | 60 | | Dictionary<string, string> meta = null; |
| | | 61 | | try |
| | 17 | 62 | | { |
| | 17 | 63 | | var json = JObject.Parse(body); |
| | 15 | 64 | | var codeValue = (string)json["code"]; |
| | 15 | 65 | | if (codeValue != null) |
| | 15 | 66 | | { |
| | 15 | 67 | | code = ParseCode(codeValue); |
| | 15 | 68 | | } |
| | 15 | 69 | | var msg = (string)json["msg"]; |
| | 15 | 70 | | if (msg != null) |
| | 15 | 71 | | { |
| | 15 | 72 | | message = msg; |
| | 15 | 73 | | } |
| | 15 | 74 | | if (json["meta"] is JObject metaObject) |
| | 3 | 75 | | { |
| | 3 | 76 | | meta = new Dictionary<string, string>(); |
| | 19 | 77 | | foreach (var property in metaObject.Properties()) |
| | 5 | 78 | | { |
| | 5 | 79 | | meta[property.Name] = |
| | 5 | 80 | | property.Value.Type == JTokenType.Null |
| | 5 | 81 | | ? null |
| | 5 | 82 | | : property.Value.ToString(); |
| | 5 | 83 | | } |
| | 3 | 84 | | } |
| | 15 | 85 | | } |
| | 2 | 86 | | catch (Exception) |
| | 2 | 87 | | { |
| | | 88 | | // Not a Twirp error (HTML error page from a proxy, empty body...): keep the raw body |
| | 2 | 89 | | } |
| | 17 | 90 | | return new TwirpException(code, message, status, meta); |
| | 17 | 91 | | } |
| | | 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) |
| | 15 | 96 | | { |
| | 15 | 97 | | var normalized = value.Replace("_", ""); |
| | 15 | 98 | | foreach ( |
| | 226 | 99 | | global::Twirp.ErrorCode candidate in Enum.GetValues(typeof(global::Twirp.ErrorCode)) |
| | | 100 | | ) |
| | 105 | 101 | | { |
| | 105 | 102 | | var name = candidate.ToString().Replace("_", ""); |
| | 105 | 103 | | if (string.Equals(name, normalized, StringComparison.OrdinalIgnoreCase)) |
| | 14 | 104 | | { |
| | 14 | 105 | | return candidate; |
| | | 106 | | } |
| | 91 | 107 | | } |
| | 1 | 108 | | return global::Twirp.ErrorCode.Unknown; |
| | 15 | 109 | | } |
| | | 110 | | } |
| | | 111 | | } |