@tsed/platform-exceptions 7.0.0-alpha.2 → 7.0.0-beta.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.
Files changed (61) hide show
  1. package/lib/cjs/components/ErrorFilter.js +48 -0
  2. package/lib/cjs/components/ErrorFilter.js.map +1 -0
  3. package/lib/cjs/components/ExceptionFilter.js +23 -0
  4. package/lib/cjs/components/ExceptionFilter.js.map +1 -0
  5. package/lib/cjs/components/MongooseErrorFilter.js +17 -0
  6. package/lib/cjs/components/MongooseErrorFilter.js.map +1 -0
  7. package/lib/cjs/components/StringErrorFilter.js +16 -0
  8. package/lib/cjs/components/StringErrorFilter.js.map +1 -0
  9. package/lib/cjs/decorators/catch.js +23 -0
  10. package/lib/cjs/decorators/catch.js.map +1 -0
  11. package/lib/cjs/domain/ExceptionFiltersContainer.js +16 -0
  12. package/lib/cjs/domain/ExceptionFiltersContainer.js.map +1 -0
  13. package/lib/cjs/domain/ExceptionSchema.js +36 -0
  14. package/lib/cjs/domain/ExceptionSchema.js.map +1 -0
  15. package/lib/cjs/errors/ResourceNotFound.js +12 -0
  16. package/lib/cjs/errors/ResourceNotFound.js.map +1 -0
  17. package/lib/cjs/index.js +19 -0
  18. package/lib/cjs/index.js.map +1 -0
  19. package/lib/cjs/interfaces/ExceptionFilterMethods.js +3 -0
  20. package/lib/cjs/interfaces/ExceptionFilterMethods.js.map +1 -0
  21. package/lib/cjs/services/PlatformExceptions.js +55 -0
  22. package/lib/cjs/services/PlatformExceptions.js.map +1 -0
  23. package/lib/esm/components/ErrorFilter.js +45 -0
  24. package/lib/esm/components/ErrorFilter.js.map +1 -0
  25. package/lib/esm/components/ExceptionFilter.js +20 -0
  26. package/lib/esm/components/ExceptionFilter.js.map +1 -0
  27. package/lib/esm/components/MongooseErrorFilter.js +14 -0
  28. package/lib/esm/components/MongooseErrorFilter.js.map +1 -0
  29. package/lib/esm/components/StringErrorFilter.js +13 -0
  30. package/lib/esm/components/StringErrorFilter.js.map +1 -0
  31. package/lib/esm/decorators/catch.js +19 -0
  32. package/lib/esm/decorators/catch.js.map +1 -0
  33. package/lib/esm/domain/ExceptionFiltersContainer.js +12 -0
  34. package/lib/esm/domain/ExceptionFiltersContainer.js.map +1 -0
  35. package/lib/esm/domain/ExceptionSchema.js +33 -0
  36. package/lib/esm/domain/ExceptionSchema.js.map +1 -0
  37. package/lib/esm/errors/ResourceNotFound.js +8 -0
  38. package/lib/esm/errors/ResourceNotFound.js.map +1 -0
  39. package/lib/esm/index.js +16 -0
  40. package/lib/esm/index.js.map +1 -0
  41. package/lib/esm/interfaces/ExceptionFilterMethods.js +2 -0
  42. package/lib/esm/interfaces/ExceptionFilterMethods.js.map +1 -0
  43. package/lib/esm/services/PlatformExceptions.js +52 -0
  44. package/lib/esm/services/PlatformExceptions.js.map +1 -0
  45. package/lib/{components → types/components}/ErrorFilter.d.ts +0 -0
  46. package/lib/{components → types/components}/ExceptionFilter.d.ts +0 -0
  47. package/lib/{components → types/components}/MongooseErrorFilter.d.ts +0 -0
  48. package/lib/{components → types/components}/StringErrorFilter.d.ts +0 -0
  49. package/lib/{decorators → types/decorators}/catch.d.ts +0 -0
  50. package/lib/{domain → types/domain}/ExceptionFiltersContainer.d.ts +0 -0
  51. package/lib/{domain → types/domain}/ExceptionSchema.d.ts +0 -0
  52. package/lib/{errors → types/errors}/ResourceNotFound.d.ts +0 -0
  53. package/lib/{index.d.ts → types/index.d.ts} +0 -0
  54. package/lib/{interfaces → types/interfaces}/ExceptionFilterMethods.d.ts +0 -0
  55. package/lib/{services → types/services}/PlatformExceptions.d.ts +0 -4
  56. package/package.json +19 -18
  57. package/tsconfig.compile.esm.json +23 -0
  58. package/lib/index.js +0 -246
  59. package/lib/index.js.map +0 -1
  60. package/lib/index.modern.js +0 -222
  61. package/lib/index.modern.js.map +0 -1
@@ -0,0 +1,48 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ErrorFilter = void 0;
4
+ const tslib_1 = require("tslib");
5
+ const core_1 = require("@tsed/core");
6
+ const catch_1 = require("../decorators/catch");
7
+ let ErrorFilter = class ErrorFilter {
8
+ catch(error, ctx) {
9
+ const { response, logger, env } = ctx;
10
+ const err = this.mapError(error, env);
11
+ logger.error({
12
+ error: { ...err, stack: error.stack }
13
+ });
14
+ response
15
+ .setHeaders(this.getHeaders(error))
16
+ .status(err.status)
17
+ .contentType("application/json")
18
+ .body(env === core_1.Env.PROD ? "InternalServerError" : err);
19
+ }
20
+ mapError(error, env) {
21
+ var _a;
22
+ return {
23
+ name: ((_a = error.origin) === null || _a === void 0 ? void 0 : _a.name) || error.name,
24
+ message: error.message,
25
+ status: error.status || 500,
26
+ errors: this.getErrors(error),
27
+ stack: env === core_1.Env.DEV ? error.stack : undefined
28
+ };
29
+ }
30
+ getErrors(error) {
31
+ return [error, error.origin].filter(Boolean).reduce((errs, { errors }) => {
32
+ return errs.concat(errors).filter(Boolean);
33
+ }, []);
34
+ }
35
+ getHeaders(error) {
36
+ return [error, error.origin].filter(Boolean).reduce((obj, { headers }) => {
37
+ return {
38
+ ...obj,
39
+ ...(headers || {})
40
+ };
41
+ }, {});
42
+ }
43
+ };
44
+ ErrorFilter = (0, tslib_1.__decorate)([
45
+ (0, catch_1.Catch)(Error)
46
+ ], ErrorFilter);
47
+ exports.ErrorFilter = ErrorFilter;
48
+ //# sourceMappingURL=ErrorFilter.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ErrorFilter.js","sourceRoot":"","sources":["../../../src/components/ErrorFilter.ts"],"names":[],"mappings":";;;;AAAA,qCAA+B;AAE/B,+CAA0C;AAI1C,IAAa,WAAW,GAAxB,MAAa,WAAW;IACtB,KAAK,CAAC,KAAU,EAAE,GAAgB;QAChC,MAAM,EAAC,QAAQ,EAAE,MAAM,EAAE,GAAG,EAAC,GAAG,GAAG,CAAC;QACpC,MAAM,GAAG,GAAG,IAAI,CAAC,QAAQ,CAAC,KAAK,EAAE,GAA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AAC,CAAC,MAAM,CAAC,CAAC,GAAG,EAAE,EAAC,OAAO,EAAM,EAAE,EAAE;YAC1E,OAAO;gBACL,GAAG,GAAG;gBACN,GAAG,CAAC,OAAO,IAAI,EAAE,CAAC;aACnB,CAAC;QACJ,CAAC,EAAE,EAAE,CAAC,CAAC;IACT,CAAC;CACF,CAAA;AAxCY,WAAW;IADvB,IAAA,aAAK,EAAC,KAAK,CAAC;GACA,WAAW,CAwCvB;AAxCY,kCAAW","sourcesContent":["import {Env} from \"@tsed/core\";\nimport {BaseContext} from \"@tsed/di\";\nimport {Catch} from \"../decorators/catch\";\nimport type {ExceptionFilterMethods} from \"../interfaces/ExceptionFilterMethods\";\n\n@Catch(Error)\nexport class ErrorFilter implements ExceptionFilterMethods {\n catch(error: any, ctx: BaseContext): void {\n const {response, logger, env} = ctx;\n const err = this.mapError(error, env);\n\n logger.error({\n error: {...err, stack: error.stack}\n });\n\n response\n .setHeaders(this.getHeaders(error))\n .status(err.status)\n .contentType(\"application/json\")\n .body(env === Env.PROD ? \"InternalServerError\" : err);\n }\n\n mapError(error: any, env?: Env) {\n return {\n name: error.origin?.name || error.name,\n message: error.message,\n status: error.status || 500,\n errors: this.getErrors(error),\n stack: env === Env.DEV ? error.stack : undefined\n };\n }\n\n protected getErrors(error: any) {\n return [error, error.origin].filter(Boolean).reduce((errs, {errors}: any) => {\n return errs.concat(errors).filter(Boolean);\n }, []);\n }\n\n protected getHeaders(error: any) {\n return [error, error.origin].filter(Boolean).reduce((obj, {headers}: any) => {\n return {\n ...obj,\n ...(headers || {})\n };\n }, {});\n }\n}\n"]}
@@ -0,0 +1,23 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ExceptionFilter = void 0;
4
+ const tslib_1 = require("tslib");
5
+ const exceptions_1 = require("@tsed/exceptions");
6
+ const catch_1 = require("../decorators/catch");
7
+ const ErrorFilter_1 = require("./ErrorFilter");
8
+ let ExceptionFilter = class ExceptionFilter extends ErrorFilter_1.ErrorFilter {
9
+ catch(error, ctx) {
10
+ const { response, logger, env } = ctx;
11
+ const err = this.mapError(error, env);
12
+ logger.error({
13
+ error: err,
14
+ stack: error.stack
15
+ });
16
+ response.setHeaders(this.getHeaders(error)).contentType("application/json").status(error.status).body(err);
17
+ }
18
+ };
19
+ ExceptionFilter = (0, tslib_1.__decorate)([
20
+ (0, catch_1.Catch)(exceptions_1.Exception)
21
+ ], ExceptionFilter);
22
+ exports.ExceptionFilter = ExceptionFilter;
23
+ //# sourceMappingURL=ExceptionFilter.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ExceptionFilter.js","sourceRoot":"","sources":["../../../src/components/ExceptionFilter.ts"],"names":[],"mappings":";;;;AACA,iDAA2C;AAC3C,+CAA0C;AAC1C,+CAA0C;AAG1C,IAAa,eAAe,GAA5B,MAAa,eAAgB,SAAQ,yBAAW;IAC9C,KAAK,CAAC,KAAgB,EAAE,GAAgB;QACtC,MAAM,EAAC,QAAQ,EAAE,MAAM,EAAE,GAAG,EAAC,GAAG,GAAG,CAAC;QACpC,MAAM,GAAG,GAAG,IAAI,CAAC,QAAQ,CAAC,KAAK,EAAE,GAAG,CAAC,CAAC;QACtC,MAAM,CAAC,KAAK,CAAC;YACX,KAAK,EAAE,GAAG;YACV,KAAK,EAAE,KAAK,CAAC,KAAK;SACnB,CAAC,CAAC;QAEH,QAAQ,CAAC,UAAU,CAAC,IAAI,CAAC,UAAU,CAAC,KAAK,CAAC,CAAC,CAAC,WAAW,CAAC,kBAAkB,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;IAC7G,CAAC;CACF,CAAA;AAXY,eAAe;IAD3B,IAAA,aAAK,EAAC,sBAAS,CAAC;GACJ,eAAe,CAW3B;AAXY,0CAAe","sourcesContent":["import {BaseContext} from \"@tsed/di\";\nimport {Exception} from \"@tsed/exceptions\";\nimport {Catch} from \"../decorators/catch\";\nimport {ErrorFilter} from \"./ErrorFilter\";\n\n@Catch(Exception)\nexport class ExceptionFilter extends ErrorFilter {\n catch(error: Exception, ctx: BaseContext) {\n const {response, logger, env} = ctx;\n const err = this.mapError(error, env);\n logger.error({\n error: err,\n stack: error.stack\n });\n\n response.setHeaders(this.getHeaders(error)).contentType(\"application/json\").status(error.status).body(err);\n }\n}\n"]}
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.MongooseErrorFilter = void 0;
4
+ const tslib_1 = require("tslib");
5
+ const exceptions_1 = require("@tsed/exceptions");
6
+ const catch_1 = require("../decorators/catch");
7
+ const ErrorFilter_1 = require("./ErrorFilter");
8
+ let MongooseErrorFilter = class MongooseErrorFilter extends ErrorFilter_1.ErrorFilter {
9
+ catch(error, ctx) {
10
+ return super.catch(new exceptions_1.BadRequest(error.message, error), ctx);
11
+ }
12
+ };
13
+ MongooseErrorFilter = (0, tslib_1.__decorate)([
14
+ (0, catch_1.Catch)("MongooseError", "MongoError")
15
+ ], MongooseErrorFilter);
16
+ exports.MongooseErrorFilter = MongooseErrorFilter;
17
+ //# sourceMappingURL=MongooseErrorFilter.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"MongooseErrorFilter.js","sourceRoot":"","sources":["../../../src/components/MongooseErrorFilter.ts"],"names":[],"mappings":";;;;AACA,iDAA4C;AAC5C,+CAA0C;AAC1C,+CAA0C;AAG1C,IAAa,mBAAmB,GAAhC,MAAa,mBAAoB,SAAQ,yBAAW;IAClD,KAAK,CAAC,KAAY,EAAE,GAAgB;QAClC,OAAO,KAAK,CAAC,KAAK,CAAC,IAAI,uBAAU,CAAC,KAAK,CAAC,OAAO,EAAE,KAAK,CAAC,EAAE,GAAG,CAAC,CAAC;IAChE,CAAC;CACF,CAAA;AAJY,mBAAmB;IAD/B,IAAA,aAAK,EAAC,eAAe,EAAE,YAAY,CAAC;GACxB,mBAAmB,CAI/B;AAJY,kDAAmB","sourcesContent":["import {BaseContext} from \"@tsed/di\";\nimport {BadRequest} from \"@tsed/exceptions\";\nimport {Catch} from \"../decorators/catch\";\nimport {ErrorFilter} from \"./ErrorFilter\";\n\n@Catch(\"MongooseError\", \"MongoError\")\nexport class MongooseErrorFilter extends ErrorFilter {\n catch(error: Error, ctx: BaseContext) {\n return super.catch(new BadRequest(error.message, error), ctx);\n }\n}\n"]}
@@ -0,0 +1,16 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.StringErrorFilter = void 0;
4
+ const tslib_1 = require("tslib");
5
+ const catch_1 = require("../decorators/catch");
6
+ const toHTML = (message = "") => message.replace(/\n/gi, "<br />");
7
+ let StringErrorFilter = class StringErrorFilter {
8
+ catch(error, ctx) {
9
+ ctx.response.status(404).body(toHTML(error));
10
+ }
11
+ };
12
+ StringErrorFilter = (0, tslib_1.__decorate)([
13
+ (0, catch_1.Catch)(String)
14
+ ], StringErrorFilter);
15
+ exports.StringErrorFilter = StringErrorFilter;
16
+ //# sourceMappingURL=StringErrorFilter.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"StringErrorFilter.js","sourceRoot":"","sources":["../../../src/components/StringErrorFilter.ts"],"names":[],"mappings":";;;;AACA,+CAA0C;AAG1C,MAAM,MAAM,GAAG,CAAC,OAAO,GAAG,EAAE,EAAE,EAAE,CAAC,OAAO,CAAC,OAAO,CAAC,MAAM,EAAE,QAAQ,CAAC,CAAC;AAGnE,IAAa,iBAAiB,GAA9B,MAAa,iBAAiB;IAC5B,KAAK,CAAC,KAAa,EAAE,GAAgB;QACnC,GAAG,CAAC,QAAQ,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC;IAC/C,CAAC;CACF,CAAA;AAJY,iBAAiB;IAD7B,IAAA,aAAK,EAAC,MAAM,CAAC;GACD,iBAAiB,CAI7B;AAJY,8CAAiB","sourcesContent":["import {BaseContext} from \"@tsed/di\";\nimport {Catch} from \"../decorators/catch\";\nimport type {ExceptionFilterMethods} from \"../interfaces/ExceptionFilterMethods\";\n\nconst toHTML = (message = \"\") => message.replace(/\\n/gi, \"<br />\");\n\n@Catch(String)\nexport class StringErrorFilter implements ExceptionFilterMethods {\n catch(error: string, ctx: BaseContext): void {\n ctx.response.status(404).body(toHTML(error));\n }\n}\n"]}
@@ -0,0 +1,23 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Catch = void 0;
4
+ const di_1 = require("@tsed/di");
5
+ const ExceptionFiltersContainer_1 = require("../domain/ExceptionFiltersContainer");
6
+ /**
7
+ * Register a new class to handle an specific exception.
8
+ * @decorator
9
+ * @param types
10
+ */
11
+ function Catch(...types) {
12
+ return (target) => {
13
+ types.forEach((type) => {
14
+ (0, ExceptionFiltersContainer_1.registerExceptionType)(type, target);
15
+ });
16
+ (0, di_1.registerProvider)({
17
+ provide: target,
18
+ useClass: target
19
+ });
20
+ };
21
+ }
22
+ exports.Catch = Catch;
23
+ //# sourceMappingURL=catch.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"catch.js","sourceRoot":"","sources":["../../../src/decorators/catch.ts"],"names":[],"mappings":";;;AACA,iCAA0C;AAC1C,mFAA0E;AAE1E;;;;GAIG;AACH,SAAgB,KAAK,CAAC,GAAG,KAAqC;IAC5D,OAAO,CAAC,MAAW,EAAE,EAAE;QACrB,KAAK,CAAC,OAAO,CAAC,CAAC,IAAI,EAAE,EAAE;YACrB,IAAA,iDAAqB,EAAC,IAAI,EAAE,MAAa,CAAC,CAAC;QAC7C,CAAC,CAAC,CAAC;QACH,IAAA,qBAAgB,EAAC;YACf,OAAO,EAAE,MAAM;YACf,QAAQ,EAAE,MAAM;SACjB,CAAC,CAAC;IACL,CAAC,CAAC;AACJ,CAAC;AAVD,sBAUC","sourcesContent":["import {Type} from \"@tsed/core\";\nimport {registerProvider} from \"@tsed/di\";\nimport {registerExceptionType} from \"../domain/ExceptionFiltersContainer\";\n\n/**\n * Register a new class to handle an specific exception.\n * @decorator\n * @param types\n */\nexport function Catch(...types: (Type<Error | any> | string)[]) {\n return (target: any) => {\n types.forEach((type) => {\n registerExceptionType(type, target as any);\n });\n registerProvider({\n provide: target,\n useClass: target\n });\n };\n}\n"]}
@@ -0,0 +1,16 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.registerExceptionType = exports.ExceptionFiltersContainer = void 0;
4
+ /**
5
+ * @ignore
6
+ */
7
+ // tslint:disable-next-line:variable-name
8
+ exports.ExceptionFiltersContainer = new Map();
9
+ /**
10
+ * @ignore
11
+ */
12
+ function registerExceptionType(type, token) {
13
+ exports.ExceptionFiltersContainer.set(type, token);
14
+ }
15
+ exports.registerExceptionType = registerExceptionType;
16
+ //# sourceMappingURL=ExceptionFiltersContainer.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ExceptionFiltersContainer.js","sourceRoot":"","sources":["../../../src/domain/ExceptionFiltersContainer.ts"],"names":[],"mappings":";;;AAMA;;GAEG;AACH,yCAAyC;AAC5B,QAAA,yBAAyB,GAAG,IAAI,GAAG,EAAoD,CAAC;AACrG;;GAEG;AACH,SAAgB,qBAAqB,CAAC,IAAwB,EAAE,KAAmC;IACjG,iCAAyB,CAAC,GAAG,CAAC,IAAI,EAAE,KAAK,CAAC,CAAC;AAC7C,CAAC;AAFD,sDAEC","sourcesContent":["import {Type} from \"@tsed/core\";\nimport type {ExceptionFilterMethods} from \"../interfaces/ExceptionFilterMethods\";\n/**\n * @ignore\n */\nexport type ExceptionFilterKey = Type<any> | Symbol | string;\n/**\n * @ignore\n */\n// tslint:disable-next-line:variable-name\nexport const ExceptionFiltersContainer = new Map<ExceptionFilterKey, Type<ExceptionFilterMethods>>();\n/**\n * @ignore\n */\nexport function registerExceptionType(type: ExceptionFilterKey, token: Type<ExceptionFilterMethods>) {\n ExceptionFiltersContainer.set(type, token);\n}\n"]}
@@ -0,0 +1,36 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const tslib_1 = require("tslib");
4
+ const Exceptions = (0, tslib_1.__importStar)(require("@tsed/exceptions"));
5
+ const exceptions_1 = require("@tsed/exceptions");
6
+ const schema_1 = require("@tsed/schema");
7
+ /**
8
+ * @ignore
9
+ */
10
+ const ErrorSchema = (0, schema_1.object)({
11
+ name: (0, schema_1.string)().required().description("The error name"),
12
+ message: (0, schema_1.string)().required().description("An error message")
13
+ })
14
+ .label("GenericError")
15
+ .unknown();
16
+ (0, schema_1.from)(exceptions_1.Exception).properties({
17
+ name: (0, schema_1.string)().required().description("The error name"),
18
+ message: (0, schema_1.string)().required().description("An error message"),
19
+ status: (0, schema_1.number)().required().description("The status code of the exception"),
20
+ errors: (0, schema_1.array)().items(ErrorSchema).description("A list of related errors"),
21
+ stack: (0, schema_1.string)().description("The stack trace (only in development mode)")
22
+ });
23
+ // Auto load models for all Exceptions
24
+ Object.values(Exceptions).forEach((target) => {
25
+ if (target !== exceptions_1.Exception && target.STATUS) {
26
+ if (target.STATUS > 302) {
27
+ const name = (0, schema_1.getStatusConstant)(target.STATUS);
28
+ (0, schema_1.from)(target).properties({
29
+ name: (0, schema_1.string)().required().example(name).default(name).description("The error name"),
30
+ status: (0, schema_1.number)().required().example(target.STATUS).default(target.STATUS).description("The status code of the exception")
31
+ });
32
+ (0, schema_1.defineStatusModel)(target.STATUS, target);
33
+ }
34
+ }
35
+ });
36
+ //# sourceMappingURL=ExceptionSchema.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ExceptionSchema.js","sourceRoot":"","sources":["../../../src/domain/ExceptionSchema.ts"],"names":[],"mappings":";;;AAAA,0EAA+C;AAC/C,iDAA2C;AAC3C,yCAAuG;AAEvG;;GAEG;AACH,MAAM,WAAW,GAAG,IAAA,eAAM,EAAC;IACzB,IAAI,EAAE,IAAA,eAAM,GAAE,CAAC,QAAQ,EAAE,CAAC,WAAW,CAAC,gBAAgB,CAAC;IACvD,OAAO,EAAE,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AAC,OAAO,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC,WAAW,CAAC,kCAAkC,CAAC;aAC1H,CAAC,CAAC;YAEH,IAAA,0BAAiB,EAAC,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;SAC1C;KACF;AACH,CAAC,CAAC,CAAC","sourcesContent":["import * as Exceptions from \"@tsed/exceptions\";\nimport {Exception} from \"@tsed/exceptions\";\nimport {array, defineStatusModel, from, getStatusConstant, number, object, string} from \"@tsed/schema\";\n\n/**\n * @ignore\n */\nconst ErrorSchema = object({\n name: string().required().description(\"The error name\"),\n message: string().required().description(\"An error message\")\n})\n .label(\"GenericError\")\n .unknown();\n\nfrom(Exception).properties({\n name: string().required().description(\"The error name\"),\n message: string().required().description(\"An error message\"),\n status: number().required().description(\"The status code of the exception\"),\n errors: array().items(ErrorSchema).description(\"A list of related errors\"),\n stack: string().description(\"The stack trace (only in development mode)\")\n});\n\n// Auto load models for all Exceptions\nObject.values(Exceptions).forEach((target: any) => {\n if (target !== Exception && target.STATUS) {\n if (target.STATUS > 302) {\n const name = getStatusConstant(target.STATUS);\n from(target).properties({\n name: string().required().example(name).default(name).description(\"The error name\"),\n status: number().required().example(target.STATUS).default(target.STATUS).description(\"The status code of the exception\")\n });\n\n defineStatusModel(target.STATUS, target);\n }\n }\n});\n"]}
@@ -0,0 +1,12 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ResourceNotFound = void 0;
4
+ const exceptions_1 = require("@tsed/exceptions");
5
+ class ResourceNotFound extends exceptions_1.NotFound {
6
+ constructor(url) {
7
+ super(`Resource "${url}" not found`);
8
+ this.url = url;
9
+ }
10
+ }
11
+ exports.ResourceNotFound = ResourceNotFound;
12
+ //# sourceMappingURL=ResourceNotFound.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ResourceNotFound.js","sourceRoot":"","sources":["../../../src/errors/ResourceNotFound.ts"],"names":[],"mappings":";;;AAAA,iDAA0C;AAE1C,MAAa,gBAAiB,SAAQ,qBAAQ;IAG5C,YAAY,GAAW;QACrB,KAAK,CAAC,aAAa,GAAG,aAAa,CAAC,CAAC;QAErC,IAAI,CAAC,GAAG,GAAG,GAAG,CAAC;IACjB,CAAC;CACF;AARD,4CAQC","sourcesContent":["import {NotFound} from \"@tsed/exceptions\";\n\nexport class ResourceNotFound extends NotFound {\n readonly url: string;\n\n constructor(url: string) {\n super(`Resource \"${url}\" not found`);\n\n this.url = url;\n }\n}\n"]}
@@ -0,0 +1,19 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const tslib_1 = require("tslib");
4
+ // components
5
+ (0, tslib_1.__exportStar)(require("./components/StringErrorFilter"), exports);
6
+ (0, tslib_1.__exportStar)(require("./components/ExceptionFilter"), exports);
7
+ (0, tslib_1.__exportStar)(require("./components/ErrorFilter"), exports);
8
+ (0, tslib_1.__exportStar)(require("./components/MongooseErrorFilter"), exports);
9
+ // decorators
10
+ (0, tslib_1.__exportStar)(require("./decorators/catch"), exports);
11
+ // domain
12
+ (0, tslib_1.__exportStar)(require("./domain/ExceptionFiltersContainer"), exports);
13
+ (0, tslib_1.__exportStar)(require("./domain/ExceptionSchema"), exports);
14
+ (0, tslib_1.__exportStar)(require("./interfaces/ExceptionFilterMethods"), exports);
15
+ // middlewares
16
+ (0, tslib_1.__exportStar)(require("./services/PlatformExceptions"), exports);
17
+ // errors
18
+ (0, tslib_1.__exportStar)(require("./errors/ResourceNotFound"), exports);
19
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";;;AAAA,aAAa;AACb,8EAA+C;AAC/C,4EAA6C;AAC7C,wEAAyC;AACzC,gFAAiD;AAEjD,aAAa;AACb,kEAAmC;AAEnC,SAAS;AACT,kFAAmD;AACnD,wEAAyC;AACzC,mFAAoD;AAEpD,cAAc;AACd,6EAA8C;AAE9C,SAAS;AACT,yEAA0C","sourcesContent":["// components\nexport * from \"./components/StringErrorFilter\";\nexport * from \"./components/ExceptionFilter\";\nexport * from \"./components/ErrorFilter\";\nexport * from \"./components/MongooseErrorFilter\";\n\n// decorators\nexport * from \"./decorators/catch\";\n\n// domain\nexport * from \"./domain/ExceptionFiltersContainer\";\nexport * from \"./domain/ExceptionSchema\";\nexport * from \"./interfaces/ExceptionFilterMethods\";\n\n// middlewares\nexport * from \"./services/PlatformExceptions\";\n\n// errors\nexport * from \"./errors/ResourceNotFound\";\n"]}
@@ -0,0 +1,3 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ //# sourceMappingURL=ExceptionFilterMethods.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ExceptionFilterMethods.js","sourceRoot":"","sources":["../../../src/interfaces/ExceptionFilterMethods.ts"],"names":[],"mappings":"","sourcesContent":["import {BaseContext} from \"@tsed/di\";\n\nexport interface ExceptionFilterMethods<T = unknown> {\n catch(error: T, ctx: BaseContext): void;\n}\n"]}
@@ -0,0 +1,55 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.PlatformExceptions = void 0;
4
+ const tslib_1 = require("tslib");
5
+ const core_1 = require("@tsed/core");
6
+ const di_1 = require("@tsed/di");
7
+ const ErrorFilter_1 = require("../components/ErrorFilter");
8
+ const ExceptionFilter_1 = require("../components/ExceptionFilter");
9
+ const MongooseErrorFilter_1 = require("../components/MongooseErrorFilter");
10
+ const StringErrorFilter_1 = require("../components/StringErrorFilter");
11
+ const ExceptionFiltersContainer_1 = require("../domain/ExceptionFiltersContainer");
12
+ const ResourceNotFound_1 = require("../errors/ResourceNotFound");
13
+ /**
14
+ * Catch all errors and return the json error with the right status code when it's possible.
15
+ *
16
+ * @platform
17
+ */
18
+ let PlatformExceptions = class PlatformExceptions {
19
+ constructor() {
20
+ this.types = new Map();
21
+ }
22
+ $onInit() {
23
+ ExceptionFiltersContainer_1.ExceptionFiltersContainer.forEach((token, type) => {
24
+ this.types.set(type, this.injector.get(token));
25
+ });
26
+ }
27
+ catch(error, ctx) {
28
+ const name = (0, core_1.nameOf)((0, core_1.classOf)(error));
29
+ if (name && this.types.has(name)) {
30
+ return this.types.get(name).catch(error, ctx);
31
+ }
32
+ const target = (0, core_1.ancestorsOf)(error)
33
+ .reverse()
34
+ .find((target) => this.types.has(target));
35
+ if (target) {
36
+ return this.types.get(target).catch(error, ctx);
37
+ }
38
+ // default
39
+ return this.types.get(Error).catch(error, ctx);
40
+ }
41
+ resourceNotFound(ctx) {
42
+ return this.catch(new ResourceNotFound_1.ResourceNotFound(ctx.request.url), ctx);
43
+ }
44
+ };
45
+ (0, tslib_1.__decorate)([
46
+ (0, di_1.Inject)(),
47
+ (0, tslib_1.__metadata)("design:type", di_1.InjectorService)
48
+ ], PlatformExceptions.prototype, "injector", void 0);
49
+ PlatformExceptions = (0, tslib_1.__decorate)([
50
+ (0, di_1.Injectable)({
51
+ imports: [ErrorFilter_1.ErrorFilter, ExceptionFilter_1.ExceptionFilter, MongooseErrorFilter_1.MongooseErrorFilter, StringErrorFilter_1.StringErrorFilter]
52
+ })
53
+ ], PlatformExceptions);
54
+ exports.PlatformExceptions = PlatformExceptions;
55
+ //# sourceMappingURL=PlatformExceptions.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"PlatformExceptions.js","sourceRoot":"","sources":["../../../src/services/PlatformExceptions.ts"],"names":[],"mappings":";;;;AAAA,qCAAwD;AACxD,iCAA0E;AAC1E,2DAAsD;AACtD,mEAA8D;AAC9D,2EAAsE;AACtE,uEAAkE;AAClE,mFAAkG;AAClG,iEAA4D;AAG5D;;;;GAIG;AAIH,IAAa,kBAAkB,GAA/B,MAAa,kBAAkB;IAA/B;QACE,UAAK,GAAoD,IAAI,GAAG,EAAE,CAAC;IAiCrE,CAAC;IA5BC,OAAO;QACL,qDAAyB,CAAC,OAAO,CAAC,CAAC,KAAK,EAAE,IAAI,EAAE,EAAE;YAChD,I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iB,CAAC;KAChF,CAAC;GACW,kBAAkB,CAkC9B;AAlCY,gDAAkB","sourcesContent":["import {ancestorsOf, classOf, nameOf} from \"@tsed/core\";\nimport {BaseContext, Inject, Injectable, InjectorService} from \"@tsed/di\";\nimport {ErrorFilter} from \"../components/ErrorFilter\";\nimport {ExceptionFilter} from \"../components/ExceptionFilter\";\nimport {MongooseErrorFilter} from \"../components/MongooseErrorFilter\";\nimport {StringErrorFilter} from \"../components/StringErrorFilter\";\nimport {ExceptionFilterKey, ExceptionFiltersContainer} from \"../domain/ExceptionFiltersContainer\";\nimport {ResourceNotFound} from \"../errors/ResourceNotFound\";\nimport {ExceptionFilterMethods} from \"../interfaces/ExceptionFilterMethods\";\n\n/**\n * Catch all errors and return the json error with the right status code when it's possible.\n *\n * @platform\n */\n@Injectable({\n imports: [ErrorFilter, ExceptionFilter, MongooseErrorFilter, StringErrorFilter]\n})\nexport class PlatformExceptions {\n types: Map<ExceptionFilterKey, ExceptionFilterMethods> = new Map();\n\n @Inject()\n injector: InjectorService;\n\n $onInit() {\n ExceptionFiltersContainer.forEach((token, type) => {\n this.types.set(type, this.injector.get(token)!);\n });\n }\n\n catch(error: unknown, ctx: BaseContext) {\n const name = nameOf(classOf(error));\n\n if (name && this.types.has(name)) {\n return this.types.get(name)!.catch(error, ctx);\n }\n\n const target = ancestorsOf(error)\n .reverse()\n .find((target) => this.types.has(target));\n\n if (target) {\n return this.types.get(target)!.catch(error, ctx);\n }\n\n // default\n return this.types.get(Error)!.catch(error, ctx);\n }\n\n resourceNotFound(ctx: BaseContext) {\n return this.catch(new ResourceNotFound(ctx.request.url), ctx);\n }\n}\n"]}
@@ -0,0 +1,45 @@
1
+ import { __decorate } from "tslib";
2
+ import { Env } from "@tsed/core";
3
+ import { Catch } from "../decorators/catch";
4
+ let ErrorFilter = class ErrorFilter {
5
+ catch(error, ctx) {
6
+ const { response, logger, env } = ctx;
7
+ const err = this.mapError(error, env);
8
+ logger.error({
9
+ error: { ...err, stack: error.stack }
10
+ });
11
+ response
12
+ .setHeaders(this.getHeaders(error))
13
+ .status(err.status)
14
+ .contentType("application/json")
15
+ .body(env === Env.PROD ? "InternalServerError" : err);
16
+ }
17
+ mapError(error, env) {
18
+ var _a;
19
+ return {
20
+ name: ((_a = error.origin) === null || _a === void 0 ? void 0 : _a.name) || error.name,
21
+ message: error.message,
22
+ status: error.status || 500,
23
+ errors: this.getErrors(error),
24
+ stack: env === Env.DEV ? error.stack : undefined
25
+ };
26
+ }
27
+ getErrors(error) {
28
+ return [error, error.origin].filter(Boolean).reduce((errs, { errors }) => {
29
+ return errs.concat(errors).filter(Boolean);
30
+ }, []);
31
+ }
32
+ getHeaders(error) {
33
+ return [error, error.origin].filter(Boolean).reduce((obj, { headers }) => {
34
+ return {
35
+ ...obj,
36
+ ...(headers || {})
37
+ };
38
+ }, {});
39
+ }
40
+ };
41
+ ErrorFilter = __decorate([
42
+ Catch(Error)
43
+ ], ErrorFilter);
44
+ export { ErrorFilter };
45
+ //# sourceMappingURL=ErrorFilter.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ErrorFilter.js","sourceRoot":"","sources":["../../../src/components/ErrorFilter.ts"],"names":[],"mappings":";AAAA,OAAO,EAAC,GAAG,EAAC,MAAM,YAAY,CAAC;AAE/B,OAAO,EAAC,KAAK,EAAC,MAAM,qBAAqB,CAAC;AAI1C,IAAa,WAAW,GAAxB,MAAa,WAAW;IACtB,KAAK,CAAC,KAAU,EAAE,GAAgB;QAChC,MAAM,EAAC,QAAQ,EAAE,MAAM,EAAE,GAAG,EAAC,GAAG,GAAG,CAAC;QACpC,MAAM,GAAG,GAAG,IAAI,CAAC,QAAQ,CAAC,KAAK,EAAE,GAAG,CAAC,CAAC;QAEtC,MAAM,CAAC,KAAK,CAAC;YACX,KAAK,EAAE,EAAC,GAAG,GAAG,EAAE,KAAK,EAAE,KAAK,CAAC,KAAK,EAAC;SACpC,CAAC,CAAC;QAEH,QAAQ;aACL,UAAU,CAAC,IAAI,CA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,CAAC,CAAC;IACT,CAAC;CACF,CAAA;AAxCY,WAAW;IADvB,KAAK,CAAC,KAAK,CAAC;GACA,WAAW,CAwCvB;SAxCY,WAAW","sourcesContent":["import {Env} from \"@tsed/core\";\nimport {BaseContext} from \"@tsed/di\";\nimport {Catch} from \"../decorators/catch\";\nimport type {ExceptionFilterMethods} from \"../interfaces/ExceptionFilterMethods\";\n\n@Catch(Error)\nexport class ErrorFilter implements ExceptionFilterMethods {\n catch(error: any, ctx: BaseContext): void {\n const {response, logger, env} = ctx;\n const err = this.mapError(error, env);\n\n logger.error({\n error: {...err, stack: error.stack}\n });\n\n response\n .setHeaders(this.getHeaders(error))\n .status(err.status)\n .contentType(\"application/json\")\n .body(env === Env.PROD ? \"InternalServerError\" : err);\n }\n\n mapError(error: any, env?: Env) {\n return {\n name: error.origin?.name || error.name,\n message: error.message,\n status: error.status || 500,\n errors: this.getErrors(error),\n stack: env === Env.DEV ? error.stack : undefined\n };\n }\n\n protected getErrors(error: any) {\n return [error, error.origin].filter(Boolean).reduce((errs, {errors}: any) => {\n return errs.concat(errors).filter(Boolean);\n }, []);\n }\n\n protected getHeaders(error: any) {\n return [error, error.origin].filter(Boolean).reduce((obj, {headers}: any) => {\n return {\n ...obj,\n ...(headers || {})\n };\n }, {});\n }\n}\n"]}
@@ -0,0 +1,20 @@
1
+ import { __decorate } from "tslib";
2
+ import { Exception } from "@tsed/exceptions";
3
+ import { Catch } from "../decorators/catch";
4
+ import { ErrorFilter } from "./ErrorFilter";
5
+ let ExceptionFilter = class ExceptionFilter extends ErrorFilter {
6
+ catch(error, ctx) {
7
+ const { response, logger, env } = ctx;
8
+ const err = this.mapError(error, env);
9
+ logger.error({
10
+ error: err,
11
+ stack: error.stack
12
+ });
13
+ response.setHeaders(this.getHeaders(error)).contentType("application/json").status(error.status).body(err);
14
+ }
15
+ };
16
+ ExceptionFilter = __decorate([
17
+ Catch(Exception)
18
+ ], ExceptionFilter);
19
+ export { ExceptionFilter };
20
+ //# sourceMappingURL=ExceptionFilter.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ExceptionFilter.js","sourceRoot":"","sources":["../../../src/components/ExceptionFilter.ts"],"names":[],"mappings":";AACA,OAAO,EAAC,SAAS,EAAC,MAAM,kBAAkB,CAAC;AAC3C,OAAO,EAAC,KAAK,EAAC,MAAM,qBAAqB,CAAC;AAC1C,OAAO,EAAC,WAAW,EAAC,MAAM,eAAe,CAAC;AAG1C,IAAa,eAAe,GAA5B,MAAa,eAAgB,SAAQ,WAAW;IAC9C,KAAK,CAAC,KAAgB,EAAE,GAAgB;QACtC,MAAM,EAAC,QAAQ,EAAE,MAAM,EAAE,GAAG,EAAC,GAAG,GAAG,CAAC;QACpC,MAAM,GAAG,GAAG,IAAI,CAAC,QAAQ,CAAC,KAAK,EAAE,GAAG,CAAC,CAAC;QACtC,MAAM,CAAC,KAAK,CAAC;YACX,KAAK,EAAE,GAAG;YACV,KAAK,EAAE,KAAK,CAAC,KAAK;SACnB,CAAC,CAAC;QAEH,QAAQ,CAAC,UAAU,CAAC,IAAI,CAAC,UAAU,CAAC,KAAK,CAAC,CAAC,CAAC,WAAW,CAAC,kBAAkB,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;IAC7G,CAAC;CACF,CAAA;AAXY,eAAe;IAD3B,KAAK,CAAC,SAAS,CAAC;GACJ,eAAe,CAW3B;SAXY,eAAe","sourcesContent":["import {BaseContext} from \"@tsed/di\";\nimport {Exception} from \"@tsed/exceptions\";\nimport {Catch} from \"../decorators/catch\";\nimport {ErrorFilter} from \"./ErrorFilter\";\n\n@Catch(Exception)\nexport class ExceptionFilter extends ErrorFilter {\n catch(error: Exception, ctx: BaseContext) {\n const {response, logger, env} = ctx;\n const err = this.mapError(error, env);\n logger.error({\n error: err,\n stack: error.stack\n });\n\n response.setHeaders(this.getHeaders(error)).contentType(\"application/json\").status(error.status).body(err);\n }\n}\n"]}
@@ -0,0 +1,14 @@
1
+ import { __decorate } from "tslib";
2
+ import { BadRequest } from "@tsed/exceptions";
3
+ import { Catch } from "../decorators/catch";
4
+ import { ErrorFilter } from "./ErrorFilter";
5
+ let MongooseErrorFilter = class MongooseErrorFilter extends ErrorFilter {
6
+ catch(error, ctx) {
7
+ return super.catch(new BadRequest(error.message, error), ctx);
8
+ }
9
+ };
10
+ MongooseErrorFilter = __decorate([
11
+ Catch("MongooseError", "MongoError")
12
+ ], MongooseErrorFilter);
13
+ export { MongooseErrorFilter };
14
+ //# sourceMappingURL=MongooseErrorFilter.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"MongooseErrorFilter.js","sourceRoot":"","sources":["../../../src/components/MongooseErrorFilter.ts"],"names":[],"mappings":";AACA,OAAO,EAAC,UAAU,EAAC,MAAM,kBAAkB,CAAC;AAC5C,OAAO,EAAC,KAAK,EAAC,MAAM,qBAAqB,CAAC;AAC1C,OAAO,EAAC,WAAW,EAAC,MAAM,eAAe,CAAC;AAG1C,IAAa,mBAAmB,GAAhC,MAAa,mBAAoB,SAAQ,WAAW;IAClD,KAAK,CAAC,KAAY,EAAE,GAAgB;QAClC,OAAO,KAAK,CAAC,KAAK,CAAC,IAAI,UAAU,CAAC,KAAK,CAAC,OAAO,EAAE,KAAK,CAAC,EAAE,GAAG,CAAC,CAAC;IAChE,CAAC;CACF,CAAA;AAJY,mBAAmB;IAD/B,KAAK,CAAC,eAAe,EAAE,YAAY,CAAC;GACxB,mBAAmB,CAI/B;SAJY,mBAAmB","sourcesContent":["import {BaseContext} from \"@tsed/di\";\nimport {BadRequest} from \"@tsed/exceptions\";\nimport {Catch} from \"../decorators/catch\";\nimport {ErrorFilter} from \"./ErrorFilter\";\n\n@Catch(\"MongooseError\", \"MongoError\")\nexport class MongooseErrorFilter extends ErrorFilter {\n catch(error: Error, ctx: BaseContext) {\n return super.catch(new BadRequest(error.message, error), ctx);\n }\n}\n"]}
@@ -0,0 +1,13 @@
1
+ import { __decorate } from "tslib";
2
+ import { Catch } from "../decorators/catch";
3
+ const toHTML = (message = "") => message.replace(/\n/gi, "<br />");
4
+ let StringErrorFilter = class StringErrorFilter {
5
+ catch(error, ctx) {
6
+ ctx.response.status(404).body(toHTML(error));
7
+ }
8
+ };
9
+ StringErrorFilter = __decorate([
10
+ Catch(String)
11
+ ], StringErrorFilter);
12
+ export { StringErrorFilter };
13
+ //# sourceMappingURL=StringErrorFilter.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"StringErrorFilter.js","sourceRoot":"","sources":["../../../src/components/StringErrorFilter.ts"],"names":[],"mappings":";AACA,OAAO,EAAC,KAAK,EAAC,MAAM,qBAAqB,CAAC;AAG1C,MAAM,MAAM,GAAG,CAAC,OAAO,GAAG,EAAE,EAAE,EAAE,CAAC,OAAO,CAAC,OAAO,CAAC,MAAM,EAAE,QAAQ,CAAC,CAAC;AAGnE,IAAa,iBAAiB,GAA9B,MAAa,iBAAiB;IAC5B,KAAK,CAAC,KAAa,EAAE,GAAgB;QACnC,GAAG,CAAC,QAAQ,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC;IAC/C,CAAC;CACF,CAAA;AAJY,iBAAiB;IAD7B,KAAK,CAAC,MAAM,CAAC;GACD,iBAAiB,CAI7B;SAJY,iBAAiB","sourcesContent":["import {BaseContext} from \"@tsed/di\";\nimport {Catch} from \"../decorators/catch\";\nimport type {ExceptionFilterMethods} from \"../interfaces/ExceptionFilterMethods\";\n\nconst toHTML = (message = \"\") => message.replace(/\\n/gi, \"<br />\");\n\n@Catch(String)\nexport class StringErrorFilter implements ExceptionFilterMethods {\n catch(error: string, ctx: BaseContext): void {\n ctx.response.status(404).body(toHTML(error));\n }\n}\n"]}
@@ -0,0 +1,19 @@
1
+ import { registerProvider } from "@tsed/di";
2
+ import { registerExceptionType } from "../domain/ExceptionFiltersContainer";
3
+ /**
4
+ * Register a new class to handle an specific exception.
5
+ * @decorator
6
+ * @param types
7
+ */
8
+ export function Catch(...types) {
9
+ return (target) => {
10
+ types.forEach((type) => {
11
+ registerExceptionType(type, target);
12
+ });
13
+ registerProvider({
14
+ provide: target,
15
+ useClass: target
16
+ });
17
+ };
18
+ }
19
+ //# sourceMappingURL=catch.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"catch.js","sourceRoot":"","sources":["../../../src/decorators/catch.ts"],"names":[],"mappings":"AACA,OAAO,EAAC,gBAAgB,EAAC,MAAM,UAAU,CAAC;AAC1C,OAAO,EAAC,qBAAqB,EAAC,MAAM,qCAAqC,CAAC;AAE1E;;;;GAIG;AACH,MAAM,UAAU,KAAK,CAAC,GAAG,KAAqC;IAC5D,OAAO,CAAC,MAAW,EAAE,EAAE;QACrB,KAAK,CAAC,OAAO,CAAC,CAAC,IAAI,EAAE,EAAE;YACrB,qBAAqB,CAAC,IAAI,EAAE,MAAa,CAAC,CAAC;QAC7C,CAAC,CAAC,CAAC;QACH,gBAAgB,CAAC;YACf,OAAO,EAAE,MAAM;YACf,QAAQ,EAAE,MAAM;SACjB,CAAC,CAAC;IACL,CAAC,CAAC;AACJ,CAAC","sourcesContent":["import {Type} from \"@tsed/core\";\nimport {registerProvider} from \"@tsed/di\";\nimport {registerExceptionType} from \"../domain/ExceptionFiltersContainer\";\n\n/**\n * Register a new class to handle an specific exception.\n * @decorator\n * @param types\n */\nexport function Catch(...types: (Type<Error | any> | string)[]) {\n return (target: any) => {\n types.forEach((type) => {\n registerExceptionType(type, target as any);\n });\n registerProvider({\n provide: target,\n useClass: target\n });\n };\n}\n"]}
@@ -0,0 +1,12 @@
1
+ /**
2
+ * @ignore
3
+ */
4
+ // tslint:disable-next-line:variable-name
5
+ export const ExceptionFiltersContainer = new Map();
6
+ /**
7
+ * @ignore
8
+ */
9
+ export function registerExceptionType(type, token) {
10
+ ExceptionFiltersContainer.set(type, token);
11
+ }
12
+ //# sourceMappingURL=ExceptionFiltersContainer.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ExceptionFiltersContainer.js","sourceRoot":"","sources":["../../../src/domain/ExceptionFiltersContainer.ts"],"names":[],"mappings":"AAMA;;GAEG;AACH,yCAAyC;AACzC,MAAM,CAAC,MAAM,yBAAyB,GAAG,IAAI,GAAG,EAAoD,CAAC;AACrG;;GAEG;AACH,MAAM,UAAU,qBAAqB,CAAC,IAAwB,EAAE,KAAmC;IACjG,yBAAyB,CAAC,GAAG,CAAC,IAAI,EAAE,KAAK,CAAC,CAAC;AAC7C,CAAC","sourcesContent":["import {Type} from \"@tsed/core\";\nimport type {ExceptionFilterMethods} from \"../interfaces/ExceptionFilterMethods\";\n/**\n * @ignore\n */\nexport type ExceptionFilterKey = Type<any> | Symbol | string;\n/**\n * @ignore\n */\n// tslint:disable-next-line:variable-name\nexport const ExceptionFiltersContainer = new Map<ExceptionFilterKey, Type<ExceptionFilterMethods>>();\n/**\n * @ignore\n */\nexport function registerExceptionType(type: ExceptionFilterKey, token: Type<ExceptionFilterMethods>) {\n ExceptionFiltersContainer.set(type, token);\n}\n"]}
@@ -0,0 +1,33 @@
1
+ import * as Exceptions from "@tsed/exceptions";
2
+ import { Exception } from "@tsed/exceptions";
3
+ import { array, defineStatusModel, from, getStatusConstant, number, object, string } from "@tsed/schema";
4
+ /**
5
+ * @ignore
6
+ */
7
+ const ErrorSchema = object({
8
+ name: string().required().description("The error name"),
9
+ message: string().required().description("An error message")
10
+ })
11
+ .label("GenericError")
12
+ .unknown();
13
+ from(Exception).properties({
14
+ name: string().required().description("The error name"),
15
+ message: string().required().description("An error message"),
16
+ status: number().required().description("The status code of the exception"),
17
+ errors: array().items(ErrorSchema).description("A list of related errors"),
18
+ stack: string().description("The stack trace (only in development mode)")
19
+ });
20
+ // Auto load models for all Exceptions
21
+ Object.values(Exceptions).forEach((target) => {
22
+ if (target !== Exception && target.STATUS) {
23
+ if (target.STATUS > 302) {
24
+ const name = getStatusConstant(target.STATUS);
25
+ from(target).properties({
26
+ name: string().required().example(name).default(name).description("The error name"),
27
+ status: number().required().example(target.STATUS).default(target.STATUS).description("The status code of the exception")
28
+ });
29
+ defineStatusModel(target.STATUS, target);
30
+ }
31
+ }
32
+ });
33
+ //# sourceMappingURL=ExceptionSchema.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ExceptionSchema.js","sourceRoot":"","sources":["../../../src/domain/ExceptionSchema.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,UAAU,MAAM,kBAAkB,CAAC;AAC/C,OAAO,EAAC,SAAS,EAAC,MAAM,kBAAkB,CAAC;AAC3C,OAAO,EAAC,KAAK,EAAE,iBAAiB,EAAE,IAAI,EAAE,iBAAiB,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAC,MAAM,cAAc,CAAC;AAEvG;;GAEG;AACH,MAAM,WAAW,GAAG,MAAM,CAAC;IACzB,IAAI,EAAE,MAAM,EAAE,CAAC,QAAQ,EAAE,CAAC,WAAW,CAAC,gBAAgB,CAAC;IACvD,OAAO,EAAE,MAAM,EAAE,CAAC,QAAQ,EAAE,CAAC,WAAW,CAAC,kBAAkB,CAAC;CAC7D,CAAC;KACC,KAAK,CAAC,cAAc,CAAC;KACrB,OAAO,EAAE,CAAC;AAEb,IAAI,CAAC,SAAS,CAAC,CAAC,UAAU,CAAC;IACzB,IAAI,EAAE,MAAM,EAAE,CAAC,QAAQ,EAAE,CAAC,WAAW,CAAC,gBAAgB,CAAC;IACvD,OAAO,EAAE,MAAM,EAAE,CAAC,QAAQ,EAAE,CAAC,WAAW,CAAC,kBAAkB,CAAC;IAC5D,MAAM,EAAE,MAAM,EAAE,CAAC,QAAQ,EAAE,CAAC,WAAW,CAAC,kCAAkC,CAAC;IAC3E,MAAM,EAAE,KAAK,EAAE,CAAC,KAAK,CAAC,WAAW,CAAC,CAAC,WAAW,CAAC,0BAA0B,CAAC;IAC1E,KAAK,EAAE,MAAM,EAAE,CAAC,WAAW,CAAC,4CAA4C,CAAC;CAC1E,CAAC,CAAC;AAEH,sCAAsC;AACtC,MAAM,CAAC,MAAM,CAAC,UAAU,CAAC,CAAC,OAAO,CAAC,CAAC,MAAW,EAAE,EAAE;IAChD,IAAI,MAAM,KAAK,SAAS,IAAI,MAAM,CAAC,MAAM,EAAE;QACzC,IAAI,MAAM,CAAC,MAAM,GAAG,GAAG,EAAE;YACvB,MAAM,IAAI,GAAG,iBAAiB,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC;YAC9C,IAAI,CAAC,MAAM,CAAC,CAAC,UAAU,CAAC;gBACtB,IAAI,EAAE,MAAM,EAAE,CAAC,QAAQ,EAAE,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC,WAAW,CAAC,gBAAgB,CAAC;gBACnF,MAAM,EAAE,MAAM,EAAE,CAAC,QAAQ,EAAE,CAAC,OAAO,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC,OAAO,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC,WAAW,CAAC,kCAAkC,CAAC;aAC1H,CAAC,CAAC;YAEH,iBAAiB,CAAC,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;SAC1C;KACF;AACH,CAAC,CAAC,CAAC","sourcesContent":["import * as Exceptions from \"@tsed/exceptions\";\nimport {Exception} from \"@tsed/exceptions\";\nimport {array, defineStatusModel, from, getStatusConstant, number, object, string} from \"@tsed/schema\";\n\n/**\n * @ignore\n */\nconst ErrorSchema = object({\n name: string().required().description(\"The error name\"),\n message: string().required().description(\"An error message\")\n})\n .label(\"GenericError\")\n .unknown();\n\nfrom(Exception).properties({\n name: string().required().description(\"The error name\"),\n message: string().required().description(\"An error message\"),\n status: number().required().description(\"The status code of the exception\"),\n errors: array().items(ErrorSchema).description(\"A list of related errors\"),\n stack: string().description(\"The stack trace (only in development mode)\")\n});\n\n// Auto load models for all Exceptions\nObject.values(Exceptions).forEach((target: any) => {\n if (target !== Exception && target.STATUS) {\n if (target.STATUS > 302) {\n const name = getStatusConstant(target.STATUS);\n from(target).properties({\n name: string().required().example(name).default(name).description(\"The error name\"),\n status: number().required().example(target.STATUS).default(target.STATUS).description(\"The status code of the exception\")\n });\n\n defineStatusModel(target.STATUS, target);\n }\n }\n});\n"]}
@@ -0,0 +1,8 @@
1
+ import { NotFound } from "@tsed/exceptions";
2
+ export class ResourceNotFound extends NotFound {
3
+ constructor(url) {
4
+ super(`Resource "${url}" not found`);
5
+ this.url = url;
6
+ }
7
+ }
8
+ //# sourceMappingURL=ResourceNotFound.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ResourceNotFound.js","sourceRoot":"","sources":["../../../src/errors/ResourceNotFound.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,QAAQ,EAAC,MAAM,kBAAkB,CAAC;AAE1C,MAAM,OAAO,gBAAiB,SAAQ,QAAQ;IAG5C,YAAY,GAAW;QACrB,KAAK,CAAC,aAAa,GAAG,aAAa,CAAC,CAAC;QAErC,IAAI,CAAC,GAAG,GAAG,GAAG,CAAC;IACjB,CAAC;CACF","sourcesContent":["import {NotFound} from \"@tsed/exceptions\";\n\nexport class ResourceNotFound extends NotFound {\n readonly url: string;\n\n constructor(url: string) {\n super(`Resource \"${url}\" not found`);\n\n this.url = url;\n }\n}\n"]}
@@ -0,0 +1,16 @@
1
+ // components
2
+ export * from "./components/StringErrorFilter";
3
+ export * from "./components/ExceptionFilter";
4
+ export * from "./components/ErrorFilter";
5
+ export * from "./components/MongooseErrorFilter";
6
+ // decorators
7
+ export * from "./decorators/catch";
8
+ // domain
9
+ export * from "./domain/ExceptionFiltersContainer";
10
+ export * from "./domain/ExceptionSchema";
11
+ export * from "./interfaces/ExceptionFilterMethods";
12
+ // middlewares
13
+ export * from "./services/PlatformExceptions";
14
+ // errors
15
+ export * from "./errors/ResourceNotFound";
16
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,aAAa;AACb,cAAc,gCAAgC,CAAC;AAC/C,cAAc,8BAA8B,CAAC;AAC7C,cAAc,0BAA0B,CAAC;AACzC,cAAc,kCAAkC,CAAC;AAEjD,aAAa;AACb,cAAc,oBAAoB,CAAC;AAEnC,SAAS;AACT,cAAc,oCAAoC,CAAC;AACnD,cAAc,0BAA0B,CAAC;AACzC,cAAc,qCAAqC,CAAC;AAEpD,cAAc;AACd,cAAc,+BAA+B,CAAC;AAE9C,SAAS;AACT,cAAc,2BAA2B,CAAC","sourcesContent":["// components\nexport * from \"./components/StringErrorFilter\";\nexport * from \"./components/ExceptionFilter\";\nexport * from \"./components/ErrorFilter\";\nexport * from \"./components/MongooseErrorFilter\";\n\n// decorators\nexport * from \"./decorators/catch\";\n\n// domain\nexport * from \"./domain/ExceptionFiltersContainer\";\nexport * from \"./domain/ExceptionSchema\";\nexport * from \"./interfaces/ExceptionFilterMethods\";\n\n// middlewares\nexport * from \"./services/PlatformExceptions\";\n\n// errors\nexport * from \"./errors/ResourceNotFound\";\n"]}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=ExceptionFilterMethods.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ExceptionFilterMethods.js","sourceRoot":"","sources":["../../../src/interfaces/ExceptionFilterMethods.ts"],"names":[],"mappings":"","sourcesContent":["import {BaseContext} from \"@tsed/di\";\n\nexport interface ExceptionFilterMethods<T = unknown> {\n catch(error: T, ctx: BaseContext): void;\n}\n"]}