@alanszp/express-common-fn 4.0.10 → 5.0.0
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.
|
@@ -3,22 +3,22 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.commonErrorsHandler = void 0;
|
|
4
4
|
const express_1 = require("@alanszp/express");
|
|
5
5
|
const errors_1 = require("@alanszp/errors");
|
|
6
|
-
const
|
|
7
|
-
const
|
|
6
|
+
const validations_1 = require("@alanszp/validations");
|
|
7
|
+
const typeorm_1 = require("typeorm");
|
|
8
8
|
const defaultsOption = {
|
|
9
|
-
entityNotFound:
|
|
9
|
+
entityNotFound: 404,
|
|
10
10
|
extraContext: {},
|
|
11
11
|
};
|
|
12
12
|
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
|
-
if (error instanceof
|
|
16
|
+
if (error instanceof validations_1.ModelValidationError) {
|
|
17
17
|
logger.info(`${baseLog}.error.validation`, { error });
|
|
18
18
|
res.status(400).json((0, express_1.errorView)(error));
|
|
19
19
|
return;
|
|
20
20
|
}
|
|
21
|
-
if (error instanceof
|
|
21
|
+
if (error instanceof typeorm_1.EntityNotFoundError) {
|
|
22
22
|
logger.info(`${baseLog}.error.typeorm.entity_not_found`, { error });
|
|
23
23
|
if (opts.entityNotFound === 400) {
|
|
24
24
|
res
|
|
@@ -30,7 +30,7 @@ function commonErrorsHandler(loggerFn) {
|
|
|
30
30
|
}
|
|
31
31
|
return;
|
|
32
32
|
}
|
|
33
|
-
if (error instanceof
|
|
33
|
+
if (error instanceof typeorm_1.QueryFailedError) {
|
|
34
34
|
if (error.code === "23505") {
|
|
35
35
|
logger.info(`${baseLog}.error.typeorm.query_error.duplicate`, {
|
|
36
36
|
error,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"commonErrorsHandler.js","sourceRoot":"","sources":["../src/commonErrorsHandler.ts"],"names":[],"mappings":";;;AAAA,8CAA6C;AAC7C,4CAIyB;AAEzB,
|
|
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;QAEF,IAAI,KAAK,YAAY,kCAAoB,EAAE;YACzC,MAAM,CAAC,IAAI,CAAC,GAAG,OAAO,mBAAmB,EAAE,EAAE,KAAK,EAAE,CAAC,CAAC;YACtD,GAAG,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC,IAAA,mBAAS,EAAC,KAAK,CAAC,CAAC,CAAC;YACvC,OAAO;SACR;QAED,IAAI,KAAK,YAAY,6BAAmB,EAAE;YACxC,MAAM,CAAC,IAAI,CAAC,GAAG,OAAO,iCAAiC,EAAE,EAAE,KAAK,EAAE,CAAC,CAAC;YACpE,IAAI,IAAI,CAAC,cAAc,KAAK,GAAG,EAAE;gBAC/B,GAAG;qBACA,MAAM,CAAC,GAAG,CAAC;qBACX,IAAI,CAAC,IAAA,mBAAS,EAAC,IAAI,wBAAe,CAAC,oBAAoB,CAAC,CAAC,CAAC,CAAC;aAC/D;iBAAM;gBACL,GAAG,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC,IAAA,mBAAS,EAAC,IAAI,sBAAa,EAAE,CAAC,CAAC,CAAC;aACtD;YACD,OAAO;SACR;QAED,IAAI,KAAK,YAAY,0BAAgB,EAAE;YACrC,IAAK,KAAqC,CAAC,IAAI,KAAK,OAAO,EAAE;gBAC3D,MAAM,CAAC,IAAI,CAAC,GAAG,OAAO,sCAAsC,EAAE;oBAC5D,KAAK;iBACN,CAAC,CAAC;gBACH,GAAG;qBACA,MAAM,CAAC,GAAG,CAAC;qBACX,IAAI,CAAC,IAAA,mBAAS,EAAC,IAAI,wBAAe,CAAC,uBAAuB,CAAC,CAAC,CAAC,CAAC;aAClE;iBAAM;gBACL,MAAM,CAAC,KAAK,CAAC,GAAG,OAAO,oCAAoC,EAAE,EAAE,KAAK,EAAE,CAAC,CAAC;gBACxE,GAAG,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC,IAAA,mBAAS,EAAC,IAAI,4BAAmB,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;aACjE;YACD,OAAO;SACR;QAED,MAAM,CAAC,KAAK,CAAC,GAAG,OAAO,gBAAgB,EAAE,EAAE,KAAK,EAAE,CAAC,CAAC;QACpD,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;AAlDD,kDAkDC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@alanszp/express-common-fn",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "5.0.0",
|
|
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",
|
|
@@ -19,10 +19,10 @@
|
|
|
19
19
|
"yalc-publish": "yarn run yalc publish"
|
|
20
20
|
},
|
|
21
21
|
"dependencies": {
|
|
22
|
-
"@alanszp/errors": "^
|
|
23
|
-
"@alanszp/express": "^
|
|
24
|
-
"@alanszp/logger": "^
|
|
25
|
-
"@alanszp/
|
|
22
|
+
"@alanszp/errors": "^5.0.0",
|
|
23
|
+
"@alanszp/express": "^5.0.0",
|
|
24
|
+
"@alanszp/logger": "^5.0.0",
|
|
25
|
+
"@alanszp/validations": "^5.0.0"
|
|
26
26
|
},
|
|
27
27
|
"peerDependencies": {
|
|
28
28
|
"express": "^4.17.1",
|
|
@@ -32,5 +32,5 @@
|
|
|
32
32
|
"@types/express": "^4.17.12",
|
|
33
33
|
"typeorm": "^0.2.34"
|
|
34
34
|
},
|
|
35
|
-
"gitHead": "
|
|
35
|
+
"gitHead": "c5afc8bd74e5dcf26c9260ec2a8da8ab3a3570a9"
|
|
36
36
|
}
|
|
@@ -5,7 +5,7 @@ import {
|
|
|
5
5
|
NotFoundError,
|
|
6
6
|
} from "@alanszp/errors";
|
|
7
7
|
import { Context, ILogger } from "@alanszp/logger";
|
|
8
|
-
import { ModelValidationError } from "@alanszp/
|
|
8
|
+
import { ModelValidationError } from "@alanszp/validations";
|
|
9
9
|
import { Response } from "express";
|
|
10
10
|
import { EntityNotFoundError, QueryFailedError } from "typeorm";
|
|
11
11
|
|
|
@@ -15,7 +15,7 @@ export interface CommonErrorOptions {
|
|
|
15
15
|
}
|
|
16
16
|
|
|
17
17
|
const defaultsOption: CommonErrorOptions = {
|
|
18
|
-
entityNotFound:
|
|
18
|
+
entityNotFound: 404,
|
|
19
19
|
extraContext: {},
|
|
20
20
|
};
|
|
21
21
|
|