diff options
Diffstat (limited to 'Exception.cs')
| -rw-r--r-- | Exception.cs | 86 |
1 files changed, 86 insertions, 0 deletions
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) {} +} |
