From a22c00fcefa10a99505c05393106fb3a655de243 Mon Sep 17 00:00:00 2001 From: Rory& Date: Sun, 1 Jun 2025 04:40:09 +0200 Subject: Add register with validation --- testFrontend/SafeNSound.Sdk/SafeNSoundException.cs | 87 ++++++++++++++++++++++ 1 file changed, 87 insertions(+) create mode 100644 testFrontend/SafeNSound.Sdk/SafeNSoundException.cs (limited to 'testFrontend/SafeNSound.Sdk/SafeNSoundException.cs') diff --git a/testFrontend/SafeNSound.Sdk/SafeNSoundException.cs b/testFrontend/SafeNSound.Sdk/SafeNSoundException.cs new file mode 100644 index 0000000..8915d19 --- /dev/null +++ b/testFrontend/SafeNSound.Sdk/SafeNSoundException.cs @@ -0,0 +1,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 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 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 { + } +} \ No newline at end of file -- cgit 1.5.1