@alanszp/express-common-fn 4.0.10 → 6.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,23 +3,26 @@ 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 typeorm_1 = require("@alanszp/typeorm");
7
- const typeorm_2 = require("typeorm");
6
+ const validations_1 = require("@alanszp/validations");
7
+ const typeorm_1 = require("typeorm");
8
8
  const defaultsOption = {
9
- entityNotFound: 400,
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 typeorm_1.ModelValidationError) {
17
- logger.info(`${baseLog}.error.validation`, { error });
16
+ const instanceLogger = logger.child(opts.extraContext);
17
+ if (error instanceof validations_1.ModelValidationError) {
18
+ instanceLogger.info(`${baseLog}.error.validation`, { error });
18
19
  res.status(400).json((0, express_1.errorView)(error));
19
20
  return;
20
21
  }
21
- if (error instanceof typeorm_2.EntityNotFoundError) {
22
- logger.info(`${baseLog}.error.typeorm.entity_not_found`, { error });
22
+ if (error instanceof typeorm_1.EntityNotFoundError) {
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)
@@ -30,9 +33,9 @@ function commonErrorsHandler(loggerFn) {
30
33
  }
31
34
  return;
32
35
  }
33
- if (error instanceof typeorm_2.QueryFailedError) {
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,8CAAwD;AAExD,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,8BAAoB,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,i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IAAK,KAAqC,CAAC,IAAI,KAAK,OAAO,EAAE;gBAC3D,cAAc,CAAC,IAAI,CAAC,GAAG,OAAO,sCAAsC,EAAE;oBACpE,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,cAAc,CAAC,KAAK,CAAC,GAAG,OAAO,oCAAoC,EAAE;oBACnE,KAAK;iBACN,CAAC,CAAC;gBACH,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,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": "4.0.10",
3
+ "version": "6.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": "^4.0.5",
23
- "@alanszp/express": "^4.0.10",
24
- "@alanszp/logger": "^4.0.10",
25
- "@alanszp/typeorm": "^4.0.10"
22
+ "@alanszp/errors": "^6.0.0",
23
+ "@alanszp/express": "^6.0.0",
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": "8f2981459501da8cf7e9c7583f7508753f286c70"
35
+ "gitHead": "75b34d763277366fdf93146cd1d20084ff469273"
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/typeorm";
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: 400,
18
+ entityNotFound: 404,
19
19
  extraContext: {},
20
20
  };
21
21
 
@@ -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
  }