From 79f15e0439cc94d63a6144104865d27f4430188e Mon Sep 17 00:00:00 2001 From: uwaa Date: Wed, 25 Dec 2024 23:08:58 +0000 Subject: [PATCH] pleroma: unify exceptions --- Pleroma/Models/PleromaException.cs | 17 +++++++++-------- Pleroma/Pleroma.cs | 8 +++++--- 2 files changed, 14 insertions(+), 11 deletions(-) diff --git a/Pleroma/Models/PleromaException.cs b/Pleroma/Models/PleromaException.cs index e5d5ada..b891fee 100644 --- a/Pleroma/Models/PleromaException.cs +++ b/Pleroma/Models/PleromaException.cs @@ -1,31 +1,32 @@ namespace Uwaa.Pleroma; -public class PleromaException : Exception +public abstract class PleromaException : Exception +{ + public override string ToString() => Message; +} + +public class PleromaSimpleException : PleromaException { [JsonPropertyName("error")] public string Text { get; set; } = null!; public override string Message => Text; - - public override string ToString() => Message; } -public class PleromaAggregateException : Exception +public class PleromaAggregateException : PleromaException { [JsonPropertyName("errors")] public PleromaInnerException[] Text { get; set; } = null!; public override string Message => string.Join("\n", (object?[])Text); - public override string ToString() => Message; + public override Exception GetBaseException() => Text == null ? this : Text[0]; } -public class PleromaInnerException : Exception +public class PleromaInnerException : PleromaException { [JsonPropertyName("detail")] public string Text { get; set; } = null!; public override string Message => Text; - - public override string ToString() => Message; } \ No newline at end of file diff --git a/Pleroma/Pleroma.cs b/Pleroma/Pleroma.cs index daafea4..7afca23 100644 --- a/Pleroma/Pleroma.cs +++ b/Pleroma/Pleroma.cs @@ -69,7 +69,7 @@ public class Pleroma { try { - PleromaException? err = JsonSerializer.Deserialize(text, SerializerOptions); + PleromaSimpleException? err = JsonSerializer.Deserialize(text, SerializerOptions); if (err != null && err.Text != null) { if (err.Text == "Throttled") @@ -85,7 +85,7 @@ public class Pleroma } catch (JsonException) { - //Not an error + //Not a simple error } try @@ -96,8 +96,10 @@ public class Pleroma } catch (JsonException) { - //Not an error + //Not an aggregate error } + + throw new HttpRequestException(text); } if (res.StatusCode is >= (HttpStatusCode)200 and < (HttpStatusCode)300)