about summary refs log tree commit diff
path: root/LibMatrix/LibMatrixException.cs
diff options
context:
space:
mode:
Diffstat (limited to 'LibMatrix/LibMatrixException.cs')
-rw-r--r--LibMatrix/LibMatrixException.cs27
1 files changed, 27 insertions, 0 deletions
diff --git a/LibMatrix/LibMatrixException.cs b/LibMatrix/LibMatrixException.cs
new file mode 100644
index 0000000..5854826
--- /dev/null
+++ b/LibMatrix/LibMatrixException.cs
@@ -0,0 +1,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";
+    }
+}
\ No newline at end of file