-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
152 lines (122 loc) · 3.23 KB
/
index.js
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
class HttpError extends Error {
#code = 500;
#message = "Something went wrong.";
#subcode = false;
constructor(code, message, subcode){
super(message);
this.#code = Number(code);
this.#message = JSON.stringify(message);
this.#subcode = subcode;
}
get code(){
return `${this.#code}`;
}
get message(){
return this.#message;
}
get subcode(){
return this.#subcode;
}
get fullcode(){
return this.subcode ? `${this.code}-${this.subcode}` : `${this.code}`
}
toJSON(){
return {
code: this.fullcode,
message: this.message
}
}
}
class BadRequest extends HttpError {
constructor(subcode = false, message = "Bad Request") {
super(400, message, subcode);
}
}
class Unauthorized extends HttpError {
constructor(subcode = false, message = "Unauthorized") {
super(401, message, subcode);
}
}
class SessionInvalid extends HttpError {
constructor(subcode = false, message = "Provided session is invalid") {
super(401, message, 1);
}
}
class SessionExpired extends HttpError {
constructor(subcode = false, message = "Provided session has expired") {
super(401, message, 2);
}
}
class PaymentRequired extends HttpError {
constructor(subcode = false, message = "Payment Required") {
super(402, message, subcode);
}
}
class Forbidden extends HttpError {
constructor(subcode = false, message = "Forbidden") {
super(402, message, subcode);
}
}
class InvalidPassword extends HttpError {
constructor(subcode = false, message = "Invalid Password") {
super(402, message, 2);
}
}
class InvalidUsername extends HttpError {
constructor(subcode = false, message = "Invalid Username") {
super(402, message, 3);
}
}
class Invalid2factor extends HttpError {
constructor(subcode = false, message = "Invalid 2FA Code") {
super(402, message, 4);
}
}
class InvalidAccountDetails extends HttpError {
constructor(subcode = false, message = "Invalid Account Details") {
super(402, message, 1);
}
}
class NotFound extends HttpError {
constructor(subcode = false, message = "Not Found") {
super(404, message, subcode);
}
}
class MethodNotAllowed extends HttpError {
constructor(subcode = false, message = "Method not allowed") {
super(405, message, subcode);
}
}
class Conflict extends HttpError {
constructor(subcode = false, message = "Conflict") {
super(409, message, subcode);
}
}
class SessionAlreadyStarted extends HttpError {
constructor(message = "Cannot start session because one has already been started.") {
super(409, message, 1);
}
}
class NotImplemented extends HttpError {
constructor(subcode = false, message = "Not Implemented") {
super(501, message, subcode);
}
}
module.exports = {
HttpError,
BadRequest,
Unauthorized,
SessionInvalid,
SessionExpired,
PaymentRequired,
Forbidden,
NotFound,
MethodNotAllowed,
Conflict,
SessionAlreadyStarted,
NotImplemented,
InvalidAccountDetails,
InvalidPassword,
InvalidUsername,
Invalid2factor
}