summaryrefslogtreecommitdiff
path: root/Exception.cs
blob: 3f7f7ca79831b3e9c42276163b3c3d3d1b19ce07 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
using System.Text.Json.Serialization;

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")]
public class HBException : Exception {
    public HBException()
        : base() {}
    [JsonConstructor]
    public HBException(string message)
        : base(message) {}
    public HBException(string message, Exception inner)
        : 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[] MissingObjects { get; }

    [JsonConstructor]
    public ObjectNotFoundException(IEnumerable<Guid> missingObjects)
        : base(GetMessage(missingObjects)) {

        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 {
    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(GetMessage(nameExists, aliasExists)) {

        NameExists  = nameExists;
        AliasExists = aliasExists;
    }

    private static string GetMessage(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) {}
}