using System.Text.Json; using System.Text.Json.Nodes; namespace Spacebar.Models.Api; public class SpacebarApiException : Exception { public int Code { get; set; } public string ErrorMessage { get; set; } public string? Request { get; set; } // Spacebar extension // public Dictionary Errors { get; set; } public JsonObject? Errors { get; set; } public JsonObject?[]? AjvErrors { get; set; } public JsonObject? OriginalErrorData { get; init; } public class FieldErrorList { // public } public SpacebarApiException(string? message) : base(message) { } // TODO: abstract out to HTTP layer public static SpacebarApiException FromJson(JsonObject resp) { var msg = resp["code"]!.GetValue() + " " + resp["message"]!.GetValue(); if (resp.ContainsKey("_ajvErrors") && resp["_ajvErrors"]!.AsArray().Any()) { msg = msg + " " + resp["_ajvErrors"]!.ToJsonString(new() { WriteIndented = true }); } else if (resp.ContainsKey("errors") && resp["errors"]!.AsObject().Any()) { msg = msg + " " + resp["errors"]!.ToJsonString(new() { WriteIndented = true }); } var ex = new SpacebarApiException(msg) { OriginalErrorData = resp, Code = resp["code"]!.GetValue(), ErrorMessage = resp["message"]!.GetValue(), Request = resp["request"]?.GetValue(), Errors = resp["errors"]?.AsObject(), AjvErrors = resp["_ajvErrors"]?.Deserialize(), }; return ex; } public JsonObject AsJsonObject() => OriginalErrorData?.DeepClone().AsObject() ?? new() { { "message", Message }, { "code", Code }, { "request", Request }, { "errors", Errors } }; }