From cbcfe6c2e5ed5f774b7bc13f305037b5e3de29e2 Mon Sep 17 00:00:00 2001 From: Jake Mannens Date: Fri, 29 May 2026 22:20:05 +1000 Subject: Added ServerException and ArgumentException types --- Exception.cs | 30 ++++++++++++++++++++++++------ 1 file changed, 24 insertions(+), 6 deletions(-) diff --git a/Exception.cs b/Exception.cs index 5299522..3f7f7ca 100644 --- a/Exception.cs +++ b/Exception.cs @@ -3,9 +3,11 @@ namespace HyperBooru.ApiModels; [JsonPolymorphic(TypeDiscriminatorPropertyName = "$type")] +[JsonDerivedType(typeof(ArgumentException), typeDiscriminator: "argumentException")] [JsonDerivedType(typeof(MediaCreateException), typeDiscriminator: "mediaCreateException")] [JsonDerivedType(typeof(MediaException), typeDiscriminator: "mediaException")] [JsonDerivedType(typeof(ObjectNotFoundException), typeDiscriminator: "objectNotFoundException")] +[JsonDerivedType(typeof(ServerException), typeDiscriminator: "serverException")] [JsonDerivedType(typeof(TagDuplicateException), typeDiscriminator: "tagDuplicateException")] [JsonDerivedType(typeof(TagException), typeDiscriminator: "tagException")] [JsonDerivedType(typeof(ThumbnailException), typeDiscriminator: "thumbnailException")] @@ -19,16 +21,32 @@ public class HBException : Exception { : base(message, inner) {} } +[ExceptionStatusCode(500)] +public class ServerException : HBException { + [JsonConstructor] + public ServerException() + : base("An unknown server error occurred") {} +} + +[ExceptionStatusCode(400)] +public class ArgumentException : HBException { + [JsonConstructor] + public ArgumentException(string message) : base(message) {} +} + [ExceptionStatusCode(404)] public class ObjectNotFoundException : HBException { - public Guid Guid { get; } + public Guid[] MissingObjects { get; } [JsonConstructor] - public ObjectNotFoundException(Guid guid) - : base($"Object not found: {guid}") { + public ObjectNotFoundException(IEnumerable missingObjects) + : base(GetMessage(missingObjects)) { - Guid = guid; + MissingObjects = missingObjects.Distinct().ToArray(); } + + private static string GetMessage(IEnumerable missingObjects) => + $"Unable to find object(s): {string.Join(", ", missingObjects.Order().Distinct())}"; } public class TagException : HBException { @@ -48,13 +66,13 @@ public class TagDuplicateException : TagException { [JsonConstructor] public TagDuplicateException(bool nameExists, bool aliasExists) - : base(GenerateMessage(nameExists, aliasExists)) { + : base(GetMessage(nameExists, aliasExists)) { NameExists = nameExists; AliasExists = aliasExists; } - private static string GenerateMessage(bool nameExists, bool aliasExists) { + private static string GetMessage(bool nameExists, bool aliasExists) { if(nameExists && aliasExists) return $"Both tag name and alias already exist!"; else if(nameExists) -- cgit v1.3