about summary refs log tree commit diff
path: root/LibMatrix/LibMatrixException.cs
blob: 5854826e124ff470a48dca8745bf9c4d9669948e (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
using System.Text.Json.Serialization;
using ArcaneLibs.Extensions;

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_UNSUPPORTED" => "The requested feature is not supported",
            _ => $"Unknown error: {GetAsObject().ToJson(ignoreNull: true)}"
        }}\nError: {Error}";

    public static class ErrorCodes {
        public const string M_NOT_FOUND = "M_NOT_FOUND";
        public const string M_UNSUPPORTED = "M_UNSUPPORTED";
    }
}