about summary refs log tree commit diff
path: root/LibMatrix/LibMatrixException.cs
blob: e066d6cf595900b0df38c14dc6c5e2b58c94b607 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
using System.Diagnostics.CodeAnalysis;
using System.Text.Json.Serialization;
using ArcaneLibs.Extensions;

// ReSharper disable MemberCanBePrivate.Global

namespace LibMatrix;

public class LibMatrixException : Exception {
    [JsonPropertyName("errcode")]
    public required string ErrorCode { get; set; }

    [JsonPropertyName("error")]
    public required string Error { get; set; }

    public object GetAsObject() => new { errcode = ErrorCode, error = Error };
    public string GetAsJson() => GetAsObject().ToJson(ignoreNull: true);

    public override string Message =>
        $"{ErrorCode}: {ErrorCode switch {
            "M_NOT_FOUND" => "The specified entity could not be found",
            "M_UNSUPPORTED" => "The requested feature is not supported",
            "RLM_NO_CLIENT_URL" => "Could not resolve client URL",
            "RLM_NO_SERVER_URL" => "Could not resolve server URL",
            _ => $"Unknown error: {GetAsObject().ToJson(ignoreNull: true)}"
        }}\nError: {Error}";

    [SuppressMessage("ReSharper", "InconsistentNaming", Justification = "Follows spec naming")]
    public static class ErrorCodes {
        public const string M_NOT_FOUND = "M_NOT_FOUND";
        public const string M_UNSUPPORTED = "M_UNSUPPORTED";
        public const string RLM_NO_CLIENT_URL = "RLM_NO_CLIENT_URL";
        public const string RLM_NO_SERVER_URL = "RLM_NO_SERVER_URL";
    }
}