@alanszp/express-common-fn 5.0.0 → 5.0.1
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.
|
@@ -13,13 +13,16 @@ function commonErrorsHandler(loggerFn) {
|
|
|
13
13
|
const logger = loggerFn();
|
|
14
14
|
return function handleCommonErrors(error, res, baseLog, options) {
|
|
15
15
|
const opts = Object.assign(Object.assign({}, defaultsOption), options);
|
|
16
|
+
const instanceLogger = logger.child(opts.extraContext);
|
|
16
17
|
if (error instanceof validations_1.ModelValidationError) {
|
|
17
|
-
|
|
18
|
+
instanceLogger.info(`${baseLog}.error.validation`, { error });
|
|
18
19
|
res.status(400).json((0, express_1.errorView)(error));
|
|
19
20
|
return;
|
|
20
21
|
}
|
|
21
22
|
if (error instanceof typeorm_1.EntityNotFoundError) {
|
|
22
|
-
|
|
23
|
+
instanceLogger.info(`${baseLog}.error.typeorm.entity_not_found`, {
|
|
24
|
+
error,
|
|
25
|
+
});
|
|
23
26
|
if (opts.entityNotFound === 400) {
|
|
24
27
|
res
|
|
25
28
|
.status(400)
|
|
@@ -32,7 +35,7 @@ function commonErrorsHandler(loggerFn) {
|
|
|
32
35
|
}
|
|
33
36
|
if (error instanceof typeorm_1.QueryFailedError) {
|
|
34
37
|
if (error.code === "23505") {
|
|
35
|
-
|
|
38
|
+
instanceLogger.info(`${baseLog}.error.typeorm.query_error.duplicate`, {
|
|
36
39
|
error,
|
|
37
40
|
});
|
|
38
41
|
res
|
|
@@ -40,12 +43,14 @@ function commonErrorsHandler(loggerFn) {
|
|
|
40
43
|
.json((0, express_1.errorView)(new errors_1.BadRequestError("Entity already exists")));
|
|
41
44
|
}
|
|
42
45
|
else {
|
|
43
|
-
|
|
46
|
+
instanceLogger.error(`${baseLog}.error.typeorm.query_error.unknown`, {
|
|
47
|
+
error,
|
|
48
|
+
});
|
|
44
49
|
res.status(500).json((0, express_1.errorView)(new errors_1.InternalServerError(error)));
|
|
45
50
|
}
|
|
46
51
|
return;
|
|
47
52
|
}
|
|
48
|
-
|
|
53
|
+
instanceLogger.error(`${baseLog}.error.unknown`, { error });
|
|
49
54
|
res.status(500).json((0, express_1.errorView)(new errors_1.InternalServerError(error)));
|
|
50
55
|
};
|
|
51
56
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"commonErrorsHandler.js","sourceRoot":"","sources":["../src/commonErrorsHandler.ts"],"names":[],"mappings":";;;AAAA,8CAA6C;AAC7C,4CAIyB;AAEzB,sDAA4D;AAE5D,qCAAgE;AAOhE,MAAM,cAAc,GAAuB;IACzC,cAAc,EAAE,GAAG;IACnB,YAAY,EAAE,EAAE;CACjB,CAAC;AAEF,SAAgB,mBAAmB,CAAC,QAAuB;IACzD,MAAM,MAAM,GAAG,QAAQ,EAAE,CAAC;IAE1B,OAAO,SAAS,kBAAkB,CAChC,KAAc,EACd,GAAa,EACb,OAAe,EACf,OAAqC;QAErC,MAAM,IAAI,mCACL,cAAc,GACd,OAAO,CACX,CAAC;
|
|
1
|
+
{"version":3,"file":"commonErrorsHandler.js","sourceRoot":"","sources":["../src/commonErrorsHandler.ts"],"names":[],"mappings":";;;AAAA,8CAA6C;AAC7C,4CAIyB;AAEzB,sDAA4D;AAE5D,qCAAgE;AAOhE,MAAM,cAAc,GAAuB;IACzC,cAAc,EAAE,GAAG;IACnB,YAAY,EAAE,EAAE;CACjB,CAAC;AAEF,SAAgB,mBAAmB,CAAC,QAAuB;IACzD,MAAM,MAAM,GAAG,QAAQ,EAAE,CAAC;IAE1B,OAAO,SAAS,kBAAkB,CAChC,KAAc,EACd,GAAa,EACb,OAAe,EACf,OAAqC;QAErC,MAAM,IAAI,mCACL,cAAc,GACd,OAAO,CACX,CAAC;QACF,MAAM,cAAc,GAAG,MAAM,CAAC,KAAK,CAAC,IAAI,CAAC,YAAY,CAAC,CAAC;QAEvD,IAAI,KAAK,YAAY,kCAAoB,EAAE;YACzC,cAAc,CAAC,IAAI,CAAC,GAAG,OAAO,mB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EAAE,EAAE,KAAK,EAAE,CAAC,CAAC;QAC5D,GAAG,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC,IAAA,mBAAS,EAAC,IAAI,4BAAmB,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;IAClE,CAAC,CAAC;AACJ,CAAC;AAvDD,kDAuDC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@alanszp/express-common-fn",
|
|
3
|
-
"version": "5.0.
|
|
3
|
+
"version": "5.0.1",
|
|
4
4
|
"description": "Alan's express function to handle common errors and format uniformly.",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"typings": "dist/index.d.ts",
|
|
@@ -32,5 +32,5 @@
|
|
|
32
32
|
"@types/express": "^4.17.12",
|
|
33
33
|
"typeorm": "^0.2.34"
|
|
34
34
|
},
|
|
35
|
-
"gitHead": "
|
|
35
|
+
"gitHead": "3ad5e43883c1e9ea6e64a7830a095960d82d7998"
|
|
36
36
|
}
|
|
@@ -32,15 +32,18 @@ export function commonErrorsHandler(loggerFn: () => ILogger) {
|
|
|
32
32
|
...defaultsOption,
|
|
33
33
|
...options,
|
|
34
34
|
};
|
|
35
|
+
const instanceLogger = logger.child(opts.extraContext);
|
|
35
36
|
|
|
36
37
|
if (error instanceof ModelValidationError) {
|
|
37
|
-
|
|
38
|
+
instanceLogger.info(`${baseLog}.error.validation`, { error });
|
|
38
39
|
res.status(400).json(errorView(error));
|
|
39
40
|
return;
|
|
40
41
|
}
|
|
41
42
|
|
|
42
43
|
if (error instanceof EntityNotFoundError) {
|
|
43
|
-
|
|
44
|
+
instanceLogger.info(`${baseLog}.error.typeorm.entity_not_found`, {
|
|
45
|
+
error,
|
|
46
|
+
});
|
|
44
47
|
if (opts.entityNotFound === 400) {
|
|
45
48
|
res
|
|
46
49
|
.status(400)
|
|
@@ -53,20 +56,22 @@ export function commonErrorsHandler(loggerFn: () => ILogger) {
|
|
|
53
56
|
|
|
54
57
|
if (error instanceof QueryFailedError) {
|
|
55
58
|
if ((error as unknown as { code: string }).code === "23505") {
|
|
56
|
-
|
|
59
|
+
instanceLogger.info(`${baseLog}.error.typeorm.query_error.duplicate`, {
|
|
57
60
|
error,
|
|
58
61
|
});
|
|
59
62
|
res
|
|
60
63
|
.status(400)
|
|
61
64
|
.json(errorView(new BadRequestError("Entity already exists")));
|
|
62
65
|
} else {
|
|
63
|
-
|
|
66
|
+
instanceLogger.error(`${baseLog}.error.typeorm.query_error.unknown`, {
|
|
67
|
+
error,
|
|
68
|
+
});
|
|
64
69
|
res.status(500).json(errorView(new InternalServerError(error)));
|
|
65
70
|
}
|
|
66
71
|
return;
|
|
67
72
|
}
|
|
68
73
|
|
|
69
|
-
|
|
74
|
+
instanceLogger.error(`${baseLog}.error.unknown`, { error });
|
|
70
75
|
res.status(500).json(errorView(new InternalServerError(error)));
|
|
71
76
|
};
|
|
72
77
|
}
|