summary refs log tree commit diff
path: root/testFrontend/SafeNSound.Sdk/SafeNSoundException.cs
blob: 8915d19c605ac81fad14d8f7872887babac40b9a (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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
using System.Diagnostics.CodeAnalysis;
using System.Text.Json.Nodes;
using System.Text.Json.Serialization;
using ArcaneLibs.Extensions;

// ReSharper disable MemberCanBePrivate.Global

namespace SafeNSound.Sdk;

public class SafeNSoundExceptionContent {
    [JsonPropertyName("errCode")]
    public required string ErrorCode { get; set; }

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

    [JsonPropertyName("soft_logout")]
    [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
    public bool? SoftLogout { get; set; }

    [JsonPropertyName("retry_after_ms")]
    [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
    public int? RetryAfterMs { get; set; }

    [JsonExtensionData]
    public Dictionary<string, object> AdditionalData { get; set; } = new();
}

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

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

    [JsonPropertyName("soft_logout")]
    [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
    public bool? SoftLogout { get; set; }

    [JsonPropertyName("retry_after_ms")]
    [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
    public int? RetryAfterMs { get; set; }

    public string RawContent { get; set; }

    [JsonExtensionData]
    public Dictionary<string, object> AdditionalData { get; set; } = new();

    public object GetAsObject() => new SafeNSoundExceptionContent()
        { ErrorCode = ErrorCode, Error = Error, SoftLogout = SoftLogout, RetryAfterMs = RetryAfterMs, AdditionalData = AdditionalData };

    public string GetAsJson() => GetAsObject().ToJson(ignoreNull: true);

    public override string Message =>
        $"{ErrorCode}: " +
        (!string.IsNullOrWhiteSpace(Error)
            ? Error
            : ErrorCode switch {
                _ => $"Unknown error: {GetAsJson()}"
            });

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

public class SafeNSoundClientException : 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}";

    [SuppressMessage("ReSharper", "InconsistentNaming", Justification = "Follows spec naming")]
    public static class ErrorCodes {
    }
}