@russ-b/nestjs-common-tools 1.11.0 → 1.12.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.
@@ -1,6 +1,13 @@
1
- import { ExceptionFilter, ArgumentsHost } from '@nestjs/common';
1
+ import { ArgumentsHost, ExceptionFilter, HttpException } from '@nestjs/common';
2
2
  import { QueryFailedError } from 'typeorm';
3
+ import { DriverError } from '../types';
4
+ type ConstraintHandler = (error: DriverError) => HttpException;
5
+ interface TypeOrmExceptionFilterOptions {
6
+ constraints?: Record<string, ConstraintHandler>;
7
+ }
3
8
  export declare class TypeOrmExceptionFilter implements ExceptionFilter {
4
- catch(exception: QueryFailedError, host: ArgumentsHost): void;
5
- private throwHttpError;
9
+ private readonly options;
10
+ constructor(options?: TypeOrmExceptionFilterOptions);
11
+ catch(exception: QueryFailedError, host: ArgumentsHost): never;
6
12
  }
13
+ export {};
@@ -5,32 +5,49 @@ var __decorate = (this && this.__decorate) || function (decorators, target, key,
5
5
  else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
6
6
  return c > 3 && r && Object.defineProperty(target, key, r), r;
7
7
  };
8
+ var __metadata = (this && this.__metadata) || function (k, v) {
9
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
10
+ };
8
11
  Object.defineProperty(exports, "__esModule", { value: true });
9
12
  exports.TypeOrmExceptionFilter = void 0;
10
13
  const common_1 = require("@nestjs/common");
11
14
  const typeorm_1 = require("typeorm");
12
15
  let TypeOrmExceptionFilter = class TypeOrmExceptionFilter {
16
+ constructor(options = {}) {
17
+ this.options = options;
18
+ }
13
19
  catch(exception, host) {
14
20
  if (host.getType() !== 'http') {
15
- return;
21
+ throw exception;
16
22
  }
17
- const errno = exception.errno || +exception.code;
18
- this.throwHttpError(errno);
19
- }
20
- throwHttpError(code) {
23
+ const error = exception.driverError;
24
+ const constraint = error.constraint;
25
+ if (constraint && this.options.constraints?.[constraint]) {
26
+ throw this.options.constraints[constraint](error);
27
+ }
28
+ const code = String(error.code ?? error.errno ?? '');
21
29
  switch (code) {
22
- case 1062:
23
- case 23505:
24
- throw new common_1.ConflictException('A record with these unique constraint(s) already exists');
25
- case 1451:
26
- throw new common_1.BadRequestException('Cannot delete or update a parent row: a foreign key constraint fails');
27
- case 1452:
28
- throw new common_1.BadRequestException('Cannot add or update a child row: a foreign key constraint fails');
30
+ case '1062':
31
+ case '23505':
32
+ throw new common_1.ConflictException('Unique constraint violation');
33
+ case '1451':
34
+ case '1452':
35
+ case '23503':
36
+ throw new common_1.BadRequestException('Foreign key constraint violation');
37
+ case '23502':
38
+ throw new common_1.BadRequestException('Required field is missing');
39
+ case '23514':
40
+ throw new common_1.BadRequestException('Check constraint violation');
41
+ case '22P02':
42
+ throw new common_1.BadRequestException('Invalid input format');
43
+ default:
44
+ throw exception;
29
45
  }
30
46
  }
31
47
  };
32
48
  exports.TypeOrmExceptionFilter = TypeOrmExceptionFilter;
33
49
  exports.TypeOrmExceptionFilter = TypeOrmExceptionFilter = __decorate([
34
- (0, common_1.Catch)(typeorm_1.QueryFailedError)
50
+ (0, common_1.Catch)(typeorm_1.QueryFailedError),
51
+ __metadata("design:paramtypes", [Object])
35
52
  ], TypeOrmExceptionFilter);
36
53
  //# sourceMappingURL=typeorm-exception.filter.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"typeorm-exception.filter.js","sourceRoot":"","sources":["../../../src/common/filters/typeorm-exception.filter.ts"],"names":[],"mappings":";;;;;;;;;AAAA,2CAMwB;AACxB,qCAA2C;AAGpC,IAAM,sBAAsB,GAA5B,MAAM,sBAAsB;IACjC,KAAK,CAAC,SAA2B,EAAE,IAAmB;QACpD,IAAI,IAAI,CAAC,OAAO,EAAE,KAAK,MAAM,EAAE,CAAC;YAC9B,OAAO;QACT,CAAC;QAED,MAAM,KAAK,GAAY,SAAiB,CAAC,KAAK,IAAI,CAAE,SAAiB,CAAC,IAAI,CAAC;QAE3E,IAAI,CAAC,cAAc,CAAC,KAAK,CAAC,CAAC;IAC7B,CAAC;IAEO,cAAc,CAAC,IAAY;QACjC,QAAQ,IAAI,EAAE,CAAC;YACb,KAAK,IAAI,CAAC;YACV,KAAK,KAAK;gBACR,MAAM,IAAI,0BAAiB,CACzB,yDAAyD,CAC1D,CAAC;YACJ,KAAK,IAAI;gBACP,MAAM,IAAI,4BAAmB,CAC3B,sEAAsE,CACvE,CAAC;YACJ,KAAK,IAAI;gBACP,MAAM,IAAI,4BAAmB,CAC3B,kEAAkE,CACnE,CAAC;QACN,CAAC;IACH,CAAC;CACF,CAAA;AA5BY,wDAAsB;iCAAtB,sBAAsB;IADlC,IAAA,cAAK,EAAC,0BAAgB,CAAC;GACX,sBAAsB,CA4BlC"}
1
+ {"version":3,"file":"typeorm-exception.filter.js","sourceRoot":"","sources":["../../../src/common/filters/typeorm-exception.filter.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,2CAOwB;AACxB,qCAA2C;AAUpC,IAAM,sBAAsB,GAA5B,MAAM,sBAAsB;IACjC,YAA6B,UAAyC,EAAE;QAA3C,YAAO,GAAP,OAAO,CAAoC;IAAG,CAAC;IAE5E,KAAK,CAAC,SAA2B,EAAE,IAAmB;QACpD,IAAI,IAAI,CAAC,OAAO,EAAE,KAAK,MAAM,EAAE,CAAC;YAC9B,MAAM,SAAS,CAAC;QAClB,CAAC;QAED,MAAM,KAAK,GAAG,SAAS,CAAC,WAA0B,CAAC;QACnD,MAAM,UAAU,GAAG,KAAK,CAAC,UAAU,CAAC;QAEpC,IAAI,UAAU,IAAI,IAAI,CAAC,OAAO,CAAC,WAAW,EAAE,CAAC,UAAU,CAAC,EAAE,CAAC;YACzD,MAAM,IAAI,CAAC,OAAO,CAAC,WAAW,CAAC,UAAU,CAAC,CAAC,KAAK,CAAC,CAAC;QACpD,CAAC;QAED,MAAM,IAAI,GAAG,MAAM,CAAC,KAAK,CAAC,IAAI,IAAI,KAAK,CAAC,KAAK,IAAI,EAAE,CAAC,CAAC;QAErD,QAAQ,IAAI,EAAE,CAAC;YACb,KAAK,MAAM,CAAC;YACZ,KAAK,OAAO;gBACV,MAAM,IAAI,0BAAiB,CAAC,6BAA6B,CAAC,CAAC;YAC7D,KAAK,MAAM,CAAC;YACZ,KAAK,MAAM,CAAC;YACZ,KAAK,OAAO;gBACV,MAAM,IAAI,4BAAmB,CAAC,kCAAkC,CAAC,CAAC;YACpE,KAAK,OAAO;gBACV,MAAM,IAAI,4BAAmB,CAAC,2BAA2B,CAAC,CAAC;YAC7D,KAAK,OAAO;gBACV,MAAM,IAAI,4BAAmB,CAAC,4BAA4B,CAAC,CAAC;YAC9D,KAAK,OAAO;gBACV,MAAM,IAAI,4BAAmB,CAAC,sBAAsB,CAAC,CAAC;YACxD;gBACE,MAAM,SAAS,CAAC;QACpB,CAAC;IACH,CAAC;CACF,CAAA;AAnCY,wDAAsB;iCAAtB,sBAAsB;IADlC,IAAA,cAAK,EAAC,0BAAgB,CAAC;;GACX,sBAAsB,CAmClC"}
@@ -0,0 +1 @@
1
+ export * from './typeorm-driver.types';
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./typeorm-driver.types"), exports);
18
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/common/types/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,yDAAuC"}
@@ -0,0 +1,8 @@
1
+ export type DriverError = {
2
+ code?: string | number;
3
+ errno?: number;
4
+ constraint?: string;
5
+ detail?: string;
6
+ table?: string;
7
+ column?: string;
8
+ };
@@ -0,0 +1,3 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ //# sourceMappingURL=typeorm-driver.types.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"typeorm-driver.types.js","sourceRoot":"","sources":["../../../src/common/types/typeorm-driver.types.ts"],"names":[],"mappings":""}