From 9d90e21684eeaab3ac9f89a1d8013168a1d577d7 Mon Sep 17 00:00:00 2001 From: Jake Mannens Date: Thu, 30 Apr 2026 00:37:15 +1000 Subject: v0.13a --- Exception.cs | 86 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 86 insertions(+) create mode 100644 Exception.cs (limited to 'Exception.cs') diff --git a/Exception.cs b/Exception.cs new file mode 100644 index 0000000..5299522 --- /dev/null +++ b/Exception.cs @@ -0,0 +1,86 @@ +using System.Text.Json.Serialization; + +namespace HyperBooru.ApiModels; + +[JsonPolymorphic(TypeDiscriminatorPropertyName = "$type")] +[JsonDerivedType(typeof(MediaCreateException), typeDiscriminator: "mediaCreateException")] +[JsonDerivedType(typeof(MediaException), typeDiscriminator: "mediaException")] +[JsonDerivedType(typeof(ObjectNotFoundException), typeDiscriminator: "objectNotFoundException")] +[JsonDerivedType(typeof(TagDuplicateException), typeDiscriminator: "tagDuplicateException")] +[JsonDerivedType(typeof(TagException), typeDiscriminator: "tagException")] +[JsonDerivedType(typeof(ThumbnailException), typeDiscriminator: "thumbnailException")] +public class HBException : Exception { + public HBException() + : base() {} + [JsonConstructor] + public HBException(string message) + : base(message) {} + public HBException(string message, Exception inner) + : base(message, inner) {} +} + +[ExceptionStatusCode(404)] +public class ObjectNotFoundException : HBException { + public Guid Guid { get; } + + [JsonConstructor] + public ObjectNotFoundException(Guid guid) + : base($"Object not found: {guid}") { + + Guid = guid; + } +} + +public class TagException : HBException { + public Guid? TagDefinitionId { get; } + + public TagException(string message) : base(message) {} + [JsonConstructor] + public TagException(string message, Guid? tagDefinitionId) + : base(message) => + TagDefinitionId = tagDefinitionId; +} + +[ExceptionStatusCode(400)] +public class TagDuplicateException : TagException { + public bool NameExists { get; } + public bool AliasExists { get; } + + [JsonConstructor] + public TagDuplicateException(bool nameExists, bool aliasExists) + : base(GenerateMessage(nameExists, aliasExists)) { + + NameExists = nameExists; + AliasExists = aliasExists; + } + + private static string GenerateMessage(bool nameExists, bool aliasExists) { + if(nameExists && aliasExists) + return $"Both tag name and alias already exist!"; + else if(nameExists) + return $"Tag name already exists!"; + else + return $"Tag alias already exists"; + } +} + +public class MediaException : HBException { + public Guid? MediaId { get; } + + public MediaException(string message) : base(message) {} + [JsonConstructor] + public MediaException(string message, Guid? mediaId) : base(message) => + MediaId = mediaId; +} + +public class MediaCreateException : MediaException { + [JsonConstructor] + public MediaCreateException(string message) + : base(message) {} +} + +public class ThumbnailException : MediaException { + [JsonConstructor] + public ThumbnailException(string message, Guid mediaId) + : base(message, mediaId) {} +} -- cgit v1.3