-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
109 lines (93 loc) · 2.39 KB
/
index.js
File metadata and controls
109 lines (93 loc) · 2.39 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
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
const _ = require('underscore');
const defaultMessage = 'Error has occurred';
const defaultUserMessage = 'Произошла ошибка';
class BaseError extends Error {
constructor({message, userMessage, ...params} = {}) {
super(
_(message).isFunction() ?
message(params) :
message || defaultMessage
);
this.userMessage = _(userMessage).isFunction() ?
userMessage(params) :
userMessage || defaultUserMessage;
Object.defineProperty(this, 'name', {
value: this.constructor.name,
configurable: true,
writable: true
});
Error.captureStackTrace(this, this.constructor);
_(this).extend(params);
}
}
class ServerError extends BaseError {
constructor(params) {
super({
status: 500,
message: 'Internal server error',
userMessage: 'Внутренняя ошибка сервера',
..._(params).isString() ? {message: params} : params
});
}
}
class UnauthorizedError extends BaseError {
constructor(params) {
super({
status: 401,
message: 'Authentication is required',
userMessage: 'Для доступа к запрашиваемому ресурсу требуется аутентификация',
...params
});
}
}
class ForbiddenError extends BaseError {
constructor(params) {
super({
status: 403,
message: 'Access denied',
userMessage: 'Доступ запрещён',
...params
});
}
}
class NotFoundError extends BaseError {
constructor(params) {
super({
status: 404,
message: 'Entity is not found',
userMessage: 'Сущность не найдена',
...params
});
}
}
class UrlNotFoundError extends NotFoundError {
constructor(params) {
super({
message: ({url}) => `Url${url ? ` "${url}" ` : ' '}is not found`,
userMessage: 'Страница не найдена',
...params
});
}
}
exports.BaseError = BaseError;
exports.ServerError = ServerError;
exports.UnauthorizedError = UnauthorizedError;
exports.ForbiddenError = ForbiddenError;
exports.NotFoundError = NotFoundError;
exports.UrlNotFoundError = UrlNotFoundError;
exports.create = ({Parent = ServerError, ...params}) => class extends Parent {
constructor(constructorParams) {
super({
...params,
...constructorParams
});
}
};
exports.errorsCreator = (obj) => ({name, ...params}) => {
obj[name] = exports.create(params);
Object.defineProperty(obj[name], 'name', {
value: name,
configurable: true,
writable: true
});
};