about summary refs log tree commit diff
path: root/LibMatrix/LibMatrixNetworkException.cs
blob: 7be0f4e6ac24ef4953f03a1fdd312ccc08e35512 (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
using System.Diagnostics.CodeAnalysis;
using System.Text.Json.Serialization;
using ArcaneLibs.Extensions;
// ReSharper disable MemberCanBePrivate.Global

namespace LibMatrix;

public class LibMatrixNetworkException : Exception {
    public LibMatrixNetworkException() : base() { }
    public LibMatrixNetworkException(Exception httpRequestException) : base("A network error occurred", httpRequestException) { }

    [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 {
            ErrorCodes.RLM_NET_UNKNOWN_HOST => "The specified host could not be found.",
            ErrorCodes.RLM_NET_INVALID_REMOTE_CERTIFICATE => "The remote server's TLS certificate is invalid or could not be verified.",
            _ => $"Unknown error: {GetAsObject().ToJson(ignoreNull: true)}"
        }}\nError: {Error}";

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