-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrors.js
More file actions
67 lines (57 loc) · 1.66 KB
/
errors.js
File metadata and controls
67 lines (57 loc) · 1.66 KB
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
// Custom error objects
// No "AvatarError" necessary as null is a valid response
import logger from "./logger.cjs";
import { config } from './config.js';
const ERROR_LIST = config.ERROR_LIST;
export class AppError extends Error {
constructor(message, cause) {
super(message);
this.cause = cause;
this.name = "AppError";
}
}
export class Mistake extends Error {
constructor(message) {
super(message);
this.name = this.constructor.name;
}
}
export class DomainError extends Mistake {
constructor(domain) {
super(`Domain "${domain}" not found or missing address records`);
this.code = 404;
}
}
export class AssetError extends Mistake {
constructor(asset) {
super(`Asset "${asset}" is not supported`);
this.code = 400;
}
}
export class ArgError extends Mistake {
constructor() {
super("Expected parameter not found");
this.code = 400;
}
}
export class UpstreamError extends Mistake {
constructor() {
super("Upstream API returned an unexpected response");
this.code = 502;
}
}
export function throwProperly(e) {
if (ERROR_LIST.includes(e.name)) {
throw new AppError(e.name, e);
} else throw e;
}
// Custom error handler for Express
export function show(e, req, res, next) {
logger.error(e);
if (e instanceof AppError) {
const name = e.cause.name;
const msg = e.cause.message;
const code = e.cause.code;
return res.status(code).send(`${name}: ${msg}`);
} else return res.status(500).send(`Unexpected API Error: ${e}`);
}