summaryrefslogtreecommitdiff
path: root/Exception.cs
blob: 5299522cd2112c440fc7982b31487a7adbb6f11f (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
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) {}
}