summaryrefslogtreecommitdiff
path: root/Exception.cs
diff options
context:
space:
mode:
Diffstat (limited to 'Exception.cs')
-rw-r--r--Exception.cs30
1 files 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<Guid> missingObjects)
+ : base(GetMessage(missingObjects)) {
- Guid = guid;
+ MissingObjects = missingObjects.Distinct().ToArray();
}
+
+ private static string GetMessage(IEnumerable<Guid> 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)