-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy patherrors.utils.ts
More file actions
84 lines (70 loc) · 2.19 KB
/
errors.utils.ts
File metadata and controls
84 lines (70 loc) · 2.19 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import { logger } from './logger.utils';
import { types } from 'node:util';
export function isError(error: unknown): error is Error {
return types.isNativeError(error);
}
export function isAlreadyExistsError(error: unknown): error is Error {
return (
(isError(error) && error.message.includes('already exists')) ||
(typeof error === 'object' && error !== null && 'status' in error && error.status === 409)
);
}
export class ErrorUtils {
static report(error: unknown, props: Record<string, unknown> = {}) {
if (isError(error)) {
logger.error(
`[REPORTED_ERROR]: ${error.message}\nProperties => ${JSON.stringify(props, null, 2)}\nStack => ${error.stack}`,
);
} else {
logger.error(`[REPORTED_ERROR]: ${JSON.stringify(error)}\nProperties => ${JSON.stringify(props, null, 2)}\n`);
}
}
}
export class ConflictError extends Error {
public statusCode = 409;
constructor(message: string) {
super(message);
this.name = 'ConflictError';
Object.setPrototypeOf(this, ConflictError.prototype);
}
}
export class NotFoundError extends Error {
public statusCode = 404;
constructor(message: string) {
super(message);
this.name = 'NotFoundError';
Object.setPrototypeOf(this, NotFoundError.prototype);
}
}
export class BadRequestError extends Error {
public statusCode = 400;
constructor(message: string) {
super(message);
this.name = 'BadRequestError';
Object.setPrototypeOf(this, BadRequestError.prototype);
}
}
export class UnsupportedMediaTypeError extends Error {
public statusCode = 415;
constructor(message: string) {
super(message);
this.name = 'UnsupportedMediaTypeError';
Object.setPrototypeOf(this, UnsupportedMediaTypeError.prototype);
}
}
export class MethodNotAllowed extends Error {
public statusCode = 405;
constructor(message: string) {
super(message);
this.name = 'MethodNotAllowed';
Object.setPrototypeOf(this, MethodNotAllowed.prototype);
}
}
export class NotImplementedError extends Error {
public statusCode = 501;
constructor(message: string) {
super(message);
this.name = 'NotImplementedError';
Object.setPrototypeOf(this, NotImplementedError.prototype);
}
}