@carecard/common-util 1.0.1 → 2.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.js +5 -0
- package/lib/appErrorHandlers.js +297 -0
- package/{dist/esm → lib}/responseStatus.js +18 -7
- package/lib/untilityFunctions.js +18 -0
- package/package.json +13 -45
- package/readme.md +6 -2
- package/dist/cjs/appErrorHandlers.cjs +0 -179
- package/dist/cjs/appErrorHandlers.d.ts +0 -128
- package/dist/cjs/index.cjs +0 -19
- package/dist/cjs/index.d.ts +0 -3
- package/dist/cjs/responseStatus.cjs +0 -71
- package/dist/cjs/responseStatus.d.ts +0 -65
- package/dist/cjs/utilityFunctions.cjs +0 -30
- package/dist/cjs/utilityFunctions.d.ts +0 -9
- package/dist/esm/appErrorHandlers.d.ts +0 -128
- package/dist/esm/appErrorHandlers.js +0 -156
- package/dist/esm/index.d.ts +0 -3
- package/dist/esm/index.js +0 -3
- package/dist/esm/responseStatus.d.ts +0 -65
- package/dist/esm/utilityFunctions.d.ts +0 -9
- package/dist/esm/utilityFunctions.js +0 -27
package/index.js
ADDED
|
@@ -0,0 +1,297 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
/**
|
|
3
|
+
* These are application level error handlers. All error responses should be send using these functions.
|
|
4
|
+
* These should be use in app.js file, not in controller files.
|
|
5
|
+
*/
|
|
6
|
+
module.exports = {
|
|
7
|
+
throwAccountSuspendedError,
|
|
8
|
+
throwAccountBlockedError,
|
|
9
|
+
throwAccountInactiveError,
|
|
10
|
+
notFound404,
|
|
11
|
+
appErrorHandler,
|
|
12
|
+
throwValidationFailureError,
|
|
13
|
+
throwRecordExistError,
|
|
14
|
+
throwWrongCredentialsError,
|
|
15
|
+
throwLoginRequiredError,
|
|
16
|
+
throwRecordNotFoundError,
|
|
17
|
+
throwRecordNotSavedError,
|
|
18
|
+
throwUpdateFailedError,
|
|
19
|
+
throwTransactionFailedError,
|
|
20
|
+
throwUsedTokenError,
|
|
21
|
+
throwBadVisitorTokenError: throwVisitorTokenError,
|
|
22
|
+
throwFileFormatNotSupportedError,
|
|
23
|
+
throwNotAuthorizedError,
|
|
24
|
+
throwBadInputError,
|
|
25
|
+
throwInputNotUuidError,
|
|
26
|
+
throwFileTooLargeError,
|
|
27
|
+
throwInvalidTimeValueError
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
function notFound404(req, res, next) {
|
|
32
|
+
res.status(404);
|
|
33
|
+
const response = {
|
|
34
|
+
error:
|
|
35
|
+
{
|
|
36
|
+
code: 'NOT_FOUND',
|
|
37
|
+
message: 'Not found',
|
|
38
|
+
details: null
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
res.send(response)
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function appErrorHandler(err, req, res, next) {
|
|
46
|
+
const response =
|
|
47
|
+
{
|
|
48
|
+
error:
|
|
49
|
+
{
|
|
50
|
+
code: err.code ?? null,
|
|
51
|
+
message: err.userMessage ?? null,
|
|
52
|
+
details: err.details ?? null
|
|
53
|
+
}
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
switch (err?.message) {
|
|
57
|
+
|
|
58
|
+
case "Account_Suspended":
|
|
59
|
+
case "Account_Blocked":
|
|
60
|
+
case "Account_Inactive":
|
|
61
|
+
res.status(403);
|
|
62
|
+
res.send(response)
|
|
63
|
+
break;
|
|
64
|
+
|
|
65
|
+
case "Validation_Failure":
|
|
66
|
+
res.status(401);
|
|
67
|
+
res.send(response)
|
|
68
|
+
break;
|
|
69
|
+
|
|
70
|
+
case "Used_Token":
|
|
71
|
+
case "Wrong_Credentials":
|
|
72
|
+
res.status(401);
|
|
73
|
+
res.send(response)
|
|
74
|
+
break;
|
|
75
|
+
|
|
76
|
+
case "Bad_Visitor_Token":
|
|
77
|
+
res.status(401);
|
|
78
|
+
res.send(response)
|
|
79
|
+
break;
|
|
80
|
+
|
|
81
|
+
case "Record_NotSaved":
|
|
82
|
+
res.status(400);
|
|
83
|
+
res.send(response)
|
|
84
|
+
break;
|
|
85
|
+
|
|
86
|
+
case "Record_Exist":
|
|
87
|
+
res.status(409);
|
|
88
|
+
res.send(response)
|
|
89
|
+
break;
|
|
90
|
+
|
|
91
|
+
case "Record_NotFound":
|
|
92
|
+
res.status(404);
|
|
93
|
+
res.send(response)
|
|
94
|
+
break;
|
|
95
|
+
|
|
96
|
+
case "Update_Failed":
|
|
97
|
+
res.status(400);
|
|
98
|
+
res.send(response)
|
|
99
|
+
break;
|
|
100
|
+
|
|
101
|
+
case "Login_Required":
|
|
102
|
+
res.status(401);
|
|
103
|
+
res.send(response)
|
|
104
|
+
break;
|
|
105
|
+
|
|
106
|
+
case "Transaction_Failed":
|
|
107
|
+
res.status(400);
|
|
108
|
+
res.send(response)
|
|
109
|
+
break;
|
|
110
|
+
|
|
111
|
+
case "File_Format_Not_Supported":
|
|
112
|
+
res.status(415);
|
|
113
|
+
res.send(response)
|
|
114
|
+
break;
|
|
115
|
+
|
|
116
|
+
case "Not_Authorized":
|
|
117
|
+
res.status(401);
|
|
118
|
+
res.send(response)
|
|
119
|
+
break;
|
|
120
|
+
|
|
121
|
+
case "Bad_Input":
|
|
122
|
+
res.status(400);
|
|
123
|
+
res.send(response);
|
|
124
|
+
break;
|
|
125
|
+
|
|
126
|
+
case "Input_Not_Uuid" :
|
|
127
|
+
res.status(400);
|
|
128
|
+
res.send(response)
|
|
129
|
+
break;
|
|
130
|
+
|
|
131
|
+
case "File too large":
|
|
132
|
+
res.status(413);
|
|
133
|
+
res.send(response)
|
|
134
|
+
break;
|
|
135
|
+
|
|
136
|
+
case "Invalid time value":
|
|
137
|
+
res.status(403);
|
|
138
|
+
res.send(response)
|
|
139
|
+
break;
|
|
140
|
+
|
|
141
|
+
default:
|
|
142
|
+
res.status(500);
|
|
143
|
+
res.send(response)
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
function throwAccountSuspendedError(params = {}) {
|
|
148
|
+
const error = new Error("Account_Suspended");
|
|
149
|
+
error.code = "ACCOUNT_SUSPENDED";
|
|
150
|
+
error.userMessage = params.userMessage ?? null;
|
|
151
|
+
error.details = params.details ?? null;
|
|
152
|
+
throw error;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
function throwAccountBlockedError(params = {}) {
|
|
156
|
+
const error = new Error("Account_Blocked");
|
|
157
|
+
error.code = "ACCOUNT_BLOCKED";
|
|
158
|
+
error.userMessage = params.userMessage ?? null;
|
|
159
|
+
error.details = params.details ?? null;
|
|
160
|
+
throw error;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
function throwAccountInactiveError(params = {}) {
|
|
164
|
+
const error = new Error("Account_Inactive");
|
|
165
|
+
error.code = "ACCOUNT_INACTIVE";
|
|
166
|
+
error.userMessage = params.userMessage ?? null;
|
|
167
|
+
error.details = params.details ?? null;
|
|
168
|
+
throw error;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
function throwValidationFailureError(params = {}) {
|
|
172
|
+
const error = new Error("Validation_Failure");
|
|
173
|
+
error.code = "VALIDATION_FAILURE";
|
|
174
|
+
error.userMessage = params.userMessage ?? null;
|
|
175
|
+
error.details = params.details ?? null;
|
|
176
|
+
throw error;
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
function throwWrongCredentialsError(params = {}) {
|
|
180
|
+
const error = new Error("Wrong_Credentials");
|
|
181
|
+
error.code = "WRONG_CREDENTIALS";
|
|
182
|
+
error.userMessage = params.userMessage ?? null;
|
|
183
|
+
error.details = params.details ?? null;
|
|
184
|
+
throw error;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
function throwRecordExistError(params = {}) {
|
|
188
|
+
const error = new Error("Record_Exist");
|
|
189
|
+
error.code = "RECORD_EXIST";
|
|
190
|
+
error.userMessage = params.userMessage ?? null;
|
|
191
|
+
error.details = params.details ?? null;
|
|
192
|
+
throw error;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
function throwRecordNotFoundError(params = {}) {
|
|
196
|
+
const error = new Error("Record_NotFound");
|
|
197
|
+
error.code = "RECORD_NOT_FOUND";
|
|
198
|
+
error.userMessage = params.userMessage ?? null;
|
|
199
|
+
error.details = params.details ?? null;
|
|
200
|
+
throw error;
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
function throwRecordNotSavedError(params = {}) {
|
|
204
|
+
const error = new Error("Record_NotSaved");
|
|
205
|
+
error.code = "RECORD_NOT_SAVED";
|
|
206
|
+
error.userMessage = params.userMessage ?? null;
|
|
207
|
+
error.details = params.details ?? null;
|
|
208
|
+
throw error;
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
function throwLoginRequiredError(params = {}) {
|
|
212
|
+
const error = new Error("Login_Required");
|
|
213
|
+
error.code = "LOGIN_REQUIRED";
|
|
214
|
+
error.userMessage = params.userMessage ?? null;
|
|
215
|
+
error.details = params.details ?? null;
|
|
216
|
+
throw error;
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
function throwUpdateFailedError(params = {}) {
|
|
220
|
+
const error = new Error("Update_Failed");
|
|
221
|
+
error.code = "UPDATE_FAILED";
|
|
222
|
+
error.userMessage = params.userMessage ?? null;
|
|
223
|
+
error.details = params.details ?? null;
|
|
224
|
+
throw error;
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
function throwTransactionFailedError(params = {}) {
|
|
228
|
+
const error = new Error("Transaction_Failed");
|
|
229
|
+
error.code = "TRANSACTION_FAILED";
|
|
230
|
+
error.userMessage = params.userMessage ?? null;
|
|
231
|
+
error.details = params.details ?? null;
|
|
232
|
+
throw error;
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
function throwUsedTokenError(params = {}) {
|
|
236
|
+
const error = new Error("Used_Token");
|
|
237
|
+
error.code = "USED_TOKEN";
|
|
238
|
+
error.userMessage = params.userMessage ?? null;
|
|
239
|
+
error.details = params.details ?? null;
|
|
240
|
+
throw error;
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
function throwVisitorTokenError(params = {}) {
|
|
244
|
+
const error = new Error("Bad_Visitor_Token");
|
|
245
|
+
error.code = "BAD_VISITOR_TOKEN";
|
|
246
|
+
error.userMessage = params.userMessage ?? null;
|
|
247
|
+
error.details = params.details ?? null;
|
|
248
|
+
throw error;
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
function throwFileFormatNotSupportedError(params = {}) {
|
|
252
|
+
const error = new Error("File_Format_Not_Supported");
|
|
253
|
+
error.code = "FILE_FORMAT_NOT_SUPPORTED";
|
|
254
|
+
error.userMessage = params.userMessage ?? null;
|
|
255
|
+
error.details = params.details ?? null;
|
|
256
|
+
throw error;
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
function throwNotAuthorizedError(params = {}) {
|
|
260
|
+
const error = new Error("Not_Authorized");
|
|
261
|
+
error.code = "NOT_AUTHORIZED";
|
|
262
|
+
error.userMessage = params.userMessage ?? null;
|
|
263
|
+
error.details = params.details ?? null;
|
|
264
|
+
throw error;
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
function throwBadInputError(params = {}) {
|
|
268
|
+
const error = new Error("Bad_Input");
|
|
269
|
+
error.code = "BAD_INPUT";
|
|
270
|
+
error.userMessage = params.userMessage ?? null;
|
|
271
|
+
error.details = params.details ?? null;
|
|
272
|
+
throw error;
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
function throwInputNotUuidError(params = {}) {
|
|
276
|
+
const error = new Error("Input_Not_Uuid");
|
|
277
|
+
error.code = "INPUT_NOT_UUID";
|
|
278
|
+
error.userMessage = params.userMessage ?? null;
|
|
279
|
+
error.details = params.details ?? null;
|
|
280
|
+
throw error;
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
function throwFileTooLargeError(params = {}) {
|
|
284
|
+
const error = new Error("File too large");
|
|
285
|
+
error.code = "FILE_TOO_LARGE";
|
|
286
|
+
error.userMessage = params.userMessage ?? null;
|
|
287
|
+
error.details = params.details ?? null;
|
|
288
|
+
throw error;
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
function throwInvalidTimeValueError(params = {}) {
|
|
292
|
+
const error = new Error("Invalid time value");
|
|
293
|
+
error.code = "INVALID_TIME_VALUE";
|
|
294
|
+
error.userMessage = params.userMessage ?? null;
|
|
295
|
+
error.details = params.details ?? null;
|
|
296
|
+
throw error;
|
|
297
|
+
}
|
|
@@ -26,11 +26,14 @@
|
|
|
26
26
|
* @param ETag
|
|
27
27
|
* @returns {*}
|
|
28
28
|
*/
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
res.
|
|
29
|
+
function setOk200( res, ETag ) {
|
|
30
|
+
|
|
31
|
+
res.set( { ETag: ETag } );
|
|
32
|
+
res.status( 200 );
|
|
33
|
+
|
|
32
34
|
return res;
|
|
33
35
|
}
|
|
36
|
+
|
|
34
37
|
/**
|
|
35
38
|
* The 201 (Created) status code indicates that the request has been
|
|
36
39
|
* fulfilled and has resulted in one or more new resources being
|
|
@@ -47,10 +50,11 @@ export function setOk200(res, ETag) {
|
|
|
47
50
|
* @param res
|
|
48
51
|
* @returns {*}
|
|
49
52
|
*/
|
|
50
|
-
|
|
51
|
-
res.status(201);
|
|
53
|
+
function setCreated201( res ) {
|
|
54
|
+
res.status( 201 );
|
|
52
55
|
return res;
|
|
53
56
|
}
|
|
57
|
+
|
|
54
58
|
/**
|
|
55
59
|
* The 400 (Bad Request) status code indicates that the server cannot or
|
|
56
60
|
* will not process the request due to something that is perceived to be
|
|
@@ -60,7 +64,14 @@ export function setCreated201(res) {
|
|
|
60
64
|
* @param res
|
|
61
65
|
* @returns {*}
|
|
62
66
|
*/
|
|
63
|
-
|
|
64
|
-
res.status(400);
|
|
67
|
+
function setBadRequest400ClientError( res ) {
|
|
68
|
+
res.status( 400 );
|
|
65
69
|
return res;
|
|
66
70
|
}
|
|
71
|
+
|
|
72
|
+
module.exports = {
|
|
73
|
+
setOk200,
|
|
74
|
+
setCreated201,
|
|
75
|
+
setBadRequest400ClientError
|
|
76
|
+
};
|
|
77
|
+
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
// Create new object only with properties given in array.
|
|
2
|
+
function extractObjectWithProperties( obj, arrayOfProperties ) {
|
|
3
|
+
let returnObj = {};
|
|
4
|
+
|
|
5
|
+
arrayOfProperties.forEach( nameOfProperty => {
|
|
6
|
+
if ( obj?.[ nameOfProperty ] ) {
|
|
7
|
+
returnObj[ nameOfProperty ] = obj?.[ nameOfProperty ];
|
|
8
|
+
}
|
|
9
|
+
} );
|
|
10
|
+
|
|
11
|
+
return returnObj;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
module.exports = {
|
|
17
|
+
extractObjectWithProperties
|
|
18
|
+
};
|
package/package.json
CHANGED
|
@@ -1,55 +1,23 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@carecard/common-util",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
3
|
+
"version": "2.0.2",
|
|
4
|
+
"repository": "https://github.com/CareCard-ca/pkg-common-util",
|
|
5
5
|
"description": "Common utility for MVC framework",
|
|
6
|
-
"
|
|
7
|
-
"author": "PK Singh",
|
|
8
|
-
"repository": {
|
|
9
|
-
"type": "git",
|
|
10
|
-
"url": "git+https:github.com/CareCard-ca/pkg-common-util.git"
|
|
11
|
-
},
|
|
12
|
-
"type": "module",
|
|
13
|
-
"publishConfig": {
|
|
14
|
-
"access": "public"
|
|
15
|
-
},
|
|
16
|
-
"main": "./dist/cjs/index.cjs",
|
|
17
|
-
"module": "./dist/esm/index.js",
|
|
18
|
-
"types": "./dist/esm/index.d.ts",
|
|
19
|
-
"exports": {
|
|
20
|
-
".": {
|
|
21
|
-
"import": "./dist/esm/index.js",
|
|
22
|
-
"require": "./dist/cjs/index.cjs",
|
|
23
|
-
"types": "./dist/esm/index.d.ts"
|
|
24
|
-
}
|
|
25
|
-
},
|
|
26
|
-
"files": [
|
|
27
|
-
"dist"
|
|
28
|
-
],
|
|
6
|
+
"main": "index.js",
|
|
29
7
|
"scripts": {
|
|
30
|
-
"
|
|
31
|
-
"build:esm": "tsc -p tsconfig.esm.json",
|
|
32
|
-
"build:cjs": "tsc -p tsconfig.cjs.json && node ./scripts/rename-cjs.js",
|
|
33
|
-
"test": "NODE_NO_WARNINGS=1 jest --coverage",
|
|
34
|
-
"format": "prettier --write .",
|
|
35
|
-
"format:check": "prettier --check .",
|
|
36
|
-
"lint": "eslint",
|
|
37
|
-
"prepare": "husky"
|
|
8
|
+
"test": "export NODE_ENV=test && mocha --watch --recursive"
|
|
38
9
|
},
|
|
10
|
+
"keywords": [
|
|
11
|
+
"validate",
|
|
12
|
+
"data"
|
|
13
|
+
],
|
|
14
|
+
"author": "CareCard team",
|
|
15
|
+
"license": "ISC",
|
|
39
16
|
"devDependencies": {
|
|
40
|
-
"
|
|
41
|
-
"
|
|
42
|
-
"@types/supertest": "^6.0.3",
|
|
43
|
-
"eslint": "9.39.2",
|
|
44
|
-
"husky": "9.1.7",
|
|
45
|
-
"jest": "30.2.0",
|
|
46
|
-
"prettier": "3.7.4",
|
|
47
|
-
"supertest": "7.1.4",
|
|
48
|
-
"ts-jest": "29.4.6",
|
|
49
|
-
"typescript": "5.9.3",
|
|
50
|
-
"typescript-eslint": "8.50.1"
|
|
17
|
+
"mocha": "11.7.5",
|
|
18
|
+
"supertest": "7.1.4"
|
|
51
19
|
},
|
|
52
20
|
"dependencies": {
|
|
53
|
-
"express": "5.
|
|
21
|
+
"express": "5.1.0"
|
|
54
22
|
}
|
|
55
23
|
}
|
package/readme.md
CHANGED
|
@@ -1,179 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
/* appErrors.ts */
|
|
3
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
-
exports.throwInvalidTimeValueError = exports.throwFileTooLargeError = exports.throwInputNotUuidError = exports.throwBadInputError = exports.throwNotAuthorizedError = exports.throwFileFormatNotSupportedError = exports.throwBadVisitorTokenError = exports.throwUsedTokenError = exports.throwTransactionFailedError = exports.throwUpdateFailedError = exports.throwLoginRequiredError = exports.throwRecordNotSavedError = exports.throwRecordNotFoundError = exports.throwRecordExistError = exports.throwWrongCredentialsError = exports.throwValidationFailureError = exports.ERROR_TYPES = exports.AppError = void 0;
|
|
5
|
-
exports.throwAppError = throwAppError;
|
|
6
|
-
exports.notFound404 = notFound404;
|
|
7
|
-
exports.appErrorHandler = appErrorHandler;
|
|
8
|
-
/* --------------------------------------------------
|
|
9
|
-
* Base Error Class
|
|
10
|
-
* -------------------------------------------------- */
|
|
11
|
-
class AppError extends Error {
|
|
12
|
-
constructor(message, code, params = {}) {
|
|
13
|
-
super(message);
|
|
14
|
-
this.name = 'AppError';
|
|
15
|
-
this.code = code;
|
|
16
|
-
this.userMessage = params.userMessage ?? null;
|
|
17
|
-
this.details = params.details ?? null;
|
|
18
|
-
}
|
|
19
|
-
}
|
|
20
|
-
exports.AppError = AppError;
|
|
21
|
-
/* --------------------------------------------------
|
|
22
|
-
* Error Registry (single source of truth)
|
|
23
|
-
* -------------------------------------------------- */
|
|
24
|
-
exports.ERROR_TYPES = {
|
|
25
|
-
VALIDATION_FAILURE: {
|
|
26
|
-
message: 'Validation_Failure',
|
|
27
|
-
code: 'VALIDATION_FAILURE',
|
|
28
|
-
status: 401,
|
|
29
|
-
},
|
|
30
|
-
WRONG_CREDENTIALS: {
|
|
31
|
-
message: 'Wrong_Credentials',
|
|
32
|
-
code: 'WRONG_CREDENTIALS',
|
|
33
|
-
status: 401,
|
|
34
|
-
},
|
|
35
|
-
USED_TOKEN: {
|
|
36
|
-
message: 'Used_Token',
|
|
37
|
-
code: 'USED_TOKEN',
|
|
38
|
-
status: 401,
|
|
39
|
-
},
|
|
40
|
-
BAD_VISITOR_TOKEN: {
|
|
41
|
-
message: 'Bad_Visitor_Token',
|
|
42
|
-
code: 'BAD_VISITOR_TOKEN',
|
|
43
|
-
status: 401,
|
|
44
|
-
},
|
|
45
|
-
LOGIN_REQUIRED: {
|
|
46
|
-
message: 'Login_Required',
|
|
47
|
-
code: 'LOGIN_REQUIRED',
|
|
48
|
-
status: 401,
|
|
49
|
-
},
|
|
50
|
-
NOT_AUTHORIZED: {
|
|
51
|
-
message: 'Not_Authorized',
|
|
52
|
-
code: 'NOT_AUTHORIZED',
|
|
53
|
-
status: 401,
|
|
54
|
-
},
|
|
55
|
-
BAD_INPUT: {
|
|
56
|
-
message: 'Bad_Input',
|
|
57
|
-
code: 'BAD_INPUT',
|
|
58
|
-
status: 400,
|
|
59
|
-
},
|
|
60
|
-
INPUT_NOT_UUID: {
|
|
61
|
-
message: 'Input_Not_Uuid',
|
|
62
|
-
code: 'INPUT_NOT_UUID',
|
|
63
|
-
status: 400,
|
|
64
|
-
},
|
|
65
|
-
RECORD_NOT_SAVED: {
|
|
66
|
-
message: 'Record_NotSaved',
|
|
67
|
-
code: 'RECORD_NOT_SAVED',
|
|
68
|
-
status: 400,
|
|
69
|
-
},
|
|
70
|
-
UPDATE_FAILED: {
|
|
71
|
-
message: 'Update_Failed',
|
|
72
|
-
code: 'UPDATE_FAILED',
|
|
73
|
-
status: 400,
|
|
74
|
-
},
|
|
75
|
-
TRANSACTION_FAILED: {
|
|
76
|
-
message: 'Transaction_Failed',
|
|
77
|
-
code: 'TRANSACTION_FAILED',
|
|
78
|
-
status: 400,
|
|
79
|
-
},
|
|
80
|
-
RECORD_EXIST: {
|
|
81
|
-
message: 'Record_Exist',
|
|
82
|
-
code: 'RECORD_EXIST',
|
|
83
|
-
status: 409,
|
|
84
|
-
},
|
|
85
|
-
RECORD_NOT_FOUND: {
|
|
86
|
-
message: 'Record_NotFound',
|
|
87
|
-
code: 'RECORD_NOT_FOUND',
|
|
88
|
-
status: 404,
|
|
89
|
-
},
|
|
90
|
-
FILE_FORMAT_NOT_SUPPORTED: {
|
|
91
|
-
message: 'File_Format_Not_Supported',
|
|
92
|
-
code: 'FILE_FORMAT_NOT_SUPPORTED',
|
|
93
|
-
status: 415,
|
|
94
|
-
},
|
|
95
|
-
FILE_TOO_LARGE: {
|
|
96
|
-
message: 'File too large',
|
|
97
|
-
code: 'FILE_TOO_LARGE',
|
|
98
|
-
status: 413,
|
|
99
|
-
},
|
|
100
|
-
INVALID_TIME_VALUE: {
|
|
101
|
-
message: 'Invalid time value',
|
|
102
|
-
code: 'INVALID_TIME_VALUE',
|
|
103
|
-
status: 403,
|
|
104
|
-
},
|
|
105
|
-
};
|
|
106
|
-
/* --------------------------------------------------
|
|
107
|
-
* Generic throw helper
|
|
108
|
-
* -------------------------------------------------- */
|
|
109
|
-
function throwAppError(type, params = {}) {
|
|
110
|
-
const { message, code } = exports.ERROR_TYPES[type];
|
|
111
|
-
throw new AppError(message, code, params);
|
|
112
|
-
}
|
|
113
|
-
/* --------------------------------------------------
|
|
114
|
-
* Backward-compatible helpers (optional)
|
|
115
|
-
* -------------------------------------------------- */
|
|
116
|
-
const throwValidationFailureError = (p = {}) => throwAppError('VALIDATION_FAILURE', p);
|
|
117
|
-
exports.throwValidationFailureError = throwValidationFailureError;
|
|
118
|
-
const throwWrongCredentialsError = (p = {}) => throwAppError('WRONG_CREDENTIALS', p);
|
|
119
|
-
exports.throwWrongCredentialsError = throwWrongCredentialsError;
|
|
120
|
-
const throwRecordExistError = (p = {}) => throwAppError('RECORD_EXIST', p);
|
|
121
|
-
exports.throwRecordExistError = throwRecordExistError;
|
|
122
|
-
const throwRecordNotFoundError = (p = {}) => throwAppError('RECORD_NOT_FOUND', p);
|
|
123
|
-
exports.throwRecordNotFoundError = throwRecordNotFoundError;
|
|
124
|
-
const throwRecordNotSavedError = (p = {}) => throwAppError('RECORD_NOT_SAVED', p);
|
|
125
|
-
exports.throwRecordNotSavedError = throwRecordNotSavedError;
|
|
126
|
-
const throwLoginRequiredError = (p = {}) => throwAppError('LOGIN_REQUIRED', p);
|
|
127
|
-
exports.throwLoginRequiredError = throwLoginRequiredError;
|
|
128
|
-
const throwUpdateFailedError = (p = {}) => throwAppError('UPDATE_FAILED', p);
|
|
129
|
-
exports.throwUpdateFailedError = throwUpdateFailedError;
|
|
130
|
-
const throwTransactionFailedError = (p = {}) => throwAppError('TRANSACTION_FAILED', p);
|
|
131
|
-
exports.throwTransactionFailedError = throwTransactionFailedError;
|
|
132
|
-
const throwUsedTokenError = (p = {}) => throwAppError('USED_TOKEN', p);
|
|
133
|
-
exports.throwUsedTokenError = throwUsedTokenError;
|
|
134
|
-
const throwBadVisitorTokenError = (p = {}) => throwAppError('BAD_VISITOR_TOKEN', p);
|
|
135
|
-
exports.throwBadVisitorTokenError = throwBadVisitorTokenError;
|
|
136
|
-
const throwFileFormatNotSupportedError = (p = {}) => throwAppError('FILE_FORMAT_NOT_SUPPORTED', p);
|
|
137
|
-
exports.throwFileFormatNotSupportedError = throwFileFormatNotSupportedError;
|
|
138
|
-
const throwNotAuthorizedError = (p = {}) => throwAppError('NOT_AUTHORIZED', p);
|
|
139
|
-
exports.throwNotAuthorizedError = throwNotAuthorizedError;
|
|
140
|
-
const throwBadInputError = (p = {}) => throwAppError('BAD_INPUT', p);
|
|
141
|
-
exports.throwBadInputError = throwBadInputError;
|
|
142
|
-
const throwInputNotUuidError = (p = {}) => throwAppError('INPUT_NOT_UUID', p);
|
|
143
|
-
exports.throwInputNotUuidError = throwInputNotUuidError;
|
|
144
|
-
const throwFileTooLargeError = (p = {}) => throwAppError('FILE_TOO_LARGE', p);
|
|
145
|
-
exports.throwFileTooLargeError = throwFileTooLargeError;
|
|
146
|
-
const throwInvalidTimeValueError = (p = {}) => throwAppError('INVALID_TIME_VALUE', p);
|
|
147
|
-
exports.throwInvalidTimeValueError = throwInvalidTimeValueError;
|
|
148
|
-
/* --------------------------------------------------
|
|
149
|
-
* Express middleware
|
|
150
|
-
* -------------------------------------------------- */
|
|
151
|
-
function notFound404(req, res, _next) {
|
|
152
|
-
res.status(404).send({
|
|
153
|
-
error: {
|
|
154
|
-
code: 'NOT_FOUND',
|
|
155
|
-
message: 'Not found',
|
|
156
|
-
details: null,
|
|
157
|
-
},
|
|
158
|
-
});
|
|
159
|
-
}
|
|
160
|
-
function appErrorHandler(err, _req, res, _next) {
|
|
161
|
-
// Iteration without `.find` or `Object.values` for maximum compatibility
|
|
162
|
-
let errorType = undefined;
|
|
163
|
-
const keys = Object.keys(exports.ERROR_TYPES);
|
|
164
|
-
for (const key of keys) {
|
|
165
|
-
const candidate = exports.ERROR_TYPES[key];
|
|
166
|
-
if (candidate.message === err?.message) {
|
|
167
|
-
errorType = candidate;
|
|
168
|
-
break;
|
|
169
|
-
}
|
|
170
|
-
}
|
|
171
|
-
const status = errorType?.status ?? 500;
|
|
172
|
-
res.status(status).send({
|
|
173
|
-
error: {
|
|
174
|
-
code: err?.code ?? errorType?.code ?? null,
|
|
175
|
-
message: err?.userMessage ?? errorType?.message ?? null,
|
|
176
|
-
details: err?.details ?? null,
|
|
177
|
-
},
|
|
178
|
-
});
|
|
179
|
-
}
|