@alanszp/express-common-fn 5.0.0 → 6.0.3

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
- logger.info(`${baseLog}.error.validation`, { error });
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
- logger.info(`${baseLog}.error.typeorm.entity_not_found`, { error });
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
- logger.info(`${baseLog}.error.typeorm.query_error.duplicate`, {
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
- logger.error(`${baseLog}.error.typeorm.query_error.unknown`, { error });
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
- logger.error(`${baseLog}.error.unknown`, { error });
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;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;Y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AI,CAAC,IAAA,mBAAS,EAAC,IAAI,4BAAmB,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;aACjE;YACD,OAAO;SACR;QAED,cAAc,CAAC,KAAK,CAAC,GAAG,OAAO,gBAAgB,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.0",
3
+ "version": "6.0.3",
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": "^5.0.0",
23
- "@alanszp/express": "^5.0.0",
24
- "@alanszp/logger": "^5.0.0",
25
- "@alanszp/validations": "^5.0.0"
22
+ "@alanszp/errors": "^6.0.0",
23
+ "@alanszp/express": "^6.0.3",
24
+ "@alanszp/logger": "^6.0.0",
25
+ "@alanszp/validations": "^6.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": "c5afc8bd74e5dcf26c9260ec2a8da8ab3a3570a9"
35
+ "gitHead": "c72bece149b2fe85d1d851412cfe763e61dad71c"
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
- logger.info(`${baseLog}.error.validation`, { error });
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
- logger.info(`${baseLog}.error.typeorm.entity_not_found`, { error });
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
- logger.info(`${baseLog}.error.typeorm.query_error.duplicate`, {
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
- logger.error(`${baseLog}.error.typeorm.query_error.unknown`, { error });
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
- logger.error(`${baseLog}.error.unknown`, { error });
74
+ instanceLogger.error(`${baseLog}.error.unknown`, { error });
70
75
  res.status(500).json(errorView(new InternalServerError(error)));
71
76
  };
72
77
  }