@tsed/platform-exceptions 7.0.0-beta.11 → 7.0.0-beta.14
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.
- package/jest.config.js +14 -0
- package/lib/cjs/components/ErrorFilter.js +1 -2
- package/lib/cjs/components/ErrorFilter.js.map +1 -1
- package/lib/cjs/errors/ResourceNotFound.js +1 -0
- package/lib/cjs/errors/ResourceNotFound.js.map +1 -1
- package/lib/cjs/interfaces/ExceptionFilterMethods.js.map +1 -1
- package/lib/cjs/services/PlatformExceptions.js +2 -3
- package/lib/cjs/services/PlatformExceptions.js.map +1 -1
- package/lib/esm/components/ErrorFilter.js +1 -2
- package/lib/esm/components/ErrorFilter.js.map +1 -1
- package/lib/esm/errors/ResourceNotFound.js +1 -0
- package/lib/esm/errors/ResourceNotFound.js.map +1 -1
- package/lib/esm/interfaces/ExceptionFilterMethods.js.map +1 -1
- package/lib/esm/services/PlatformExceptions.js +2 -3
- package/lib/esm/services/PlatformExceptions.js.map +1 -1
- package/lib/types/interfaces/ExceptionFilterMethods.d.ts +2 -2
- package/lib/types/services/PlatformExceptions.d.ts +3 -3
- package/package.json +16 -12
- package/readme.md +2 -1
package/jest.config.js
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
// For a detailed explanation regarding each configuration property, visit:
|
|
2
|
+
// https://jestjs.io/docs/en/configuration.html
|
|
3
|
+
|
|
4
|
+
module.exports = {
|
|
5
|
+
...require("@tsed/jest-config")(__dirname, "platform-exceptions"),
|
|
6
|
+
coverageThreshold: {
|
|
7
|
+
global: {
|
|
8
|
+
statements: 100,
|
|
9
|
+
branches: 94.73,
|
|
10
|
+
functions: 100,
|
|
11
|
+
lines: 100
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
};
|
|
@@ -18,9 +18,8 @@ let ErrorFilter = class ErrorFilter {
|
|
|
18
18
|
.body(env === core_1.Env.PROD ? "InternalServerError" : err);
|
|
19
19
|
}
|
|
20
20
|
mapError(error, env) {
|
|
21
|
-
var _a;
|
|
22
21
|
return {
|
|
23
|
-
name:
|
|
22
|
+
name: error.origin?.name || error.name,
|
|
24
23
|
message: error.message,
|
|
25
24
|
status: error.status || 500,
|
|
26
25
|
errors: this.getErrors(error),
|
|
@@ -1 +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,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,CAAC,UAAU,CAAC,KAAK,CAAC,CAAC;aAClC,MAAM,CAAC,GAAG,CAAC,MAAM,CAAC;aAClB,WAAW,CAAC,kBAAkB,CAAC;aAC/B,IAAI,CAAC,GAAG,KAAK,UAAG,CAAC,IAAI,CAAC,CAAC,CAAC,qBAAqB,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC;IAC1D,CAAC;IAED,QAAQ,CAAC,KAAU,EAAE,GAAS
|
|
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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C,KAAK,EAAE,KAAK,CAAC,MAAM,CAAC,CAAC,MAAM,CAAC,OAAO,CAAC,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"]}
|
|
@@ -3,6 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.ResourceNotFound = void 0;
|
|
4
4
|
const exceptions_1 = require("@tsed/exceptions");
|
|
5
5
|
class ResourceNotFound extends exceptions_1.NotFound {
|
|
6
|
+
url;
|
|
6
7
|
constructor(url) {
|
|
7
8
|
super(`Resource "${url}" not found`);
|
|
8
9
|
this.url = url;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ResourceNotFound.js","sourceRoot":"","sources":["../../../src/errors/ResourceNotFound.ts"],"names":[],"mappings":";;;AAAA,iDAA0C;AAE1C,MAAa,gBAAiB,SAAQ,qBAAQ;
|
|
1
|
+
{"version":3,"file":"ResourceNotFound.js","sourceRoot":"","sources":["../../../src/errors/ResourceNotFound.ts"],"names":[],"mappings":";;;AAAA,iDAA0C;AAE1C,MAAa,gBAAiB,SAAQ,qBAAQ;IACnC,GAAG,CAAS;IAErB,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"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ExceptionFilterMethods.js","sourceRoot":"","sources":["../../../src/interfaces/ExceptionFilterMethods.ts"],"names":[],"mappings":"","sourcesContent":["import {
|
|
1
|
+
{"version":3,"file":"ExceptionFilterMethods.js","sourceRoot":"","sources":["../../../src/interfaces/ExceptionFilterMethods.ts"],"names":[],"mappings":"","sourcesContent":["import {DIContext} from \"@tsed/di\";\n\nexport interface ExceptionFilterMethods<T = unknown> {\n catch(error: T, ctx: DIContext): void;\n}\n"]}
|
|
@@ -16,9 +16,8 @@ const ResourceNotFound_1 = require("../errors/ResourceNotFound");
|
|
|
16
16
|
* @platform
|
|
17
17
|
*/
|
|
18
18
|
let PlatformExceptions = class PlatformExceptions {
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
}
|
|
19
|
+
types = new Map();
|
|
20
|
+
injector;
|
|
22
21
|
$onInit() {
|
|
23
22
|
ExceptionFiltersContainer_1.ExceptionFiltersContainer.forEach((token, type) => {
|
|
24
23
|
this.types.set(type, this.injector.get(token));
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"PlatformExceptions.js","sourceRoot":"","sources":["../../../src/services/PlatformExceptions.ts"],"names":[],"mappings":";;;;AAAA,qCAAwD;AACxD,
|
|
1
|
+
{"version":3,"file":"PlatformExceptions.js","sourceRoot":"","sources":["../../../src/services/PlatformExceptions.ts"],"names":[],"mappings":";;;;AAAA,qCAAwD;AACxD,iCAAqF;AACrF,2DAAsD;AACtD,mEAA8D;AAC9D,2EAAsE;AACtE,uEAAkE;AAClE,mFAAkG;AAClG,iEAA4D;AAG5D;;;;GAIG;AAIH,IAAa,kBAAkB,GAA/B,MAAa,kBAAkB;IAC7B,KAAK,GAAoD,IAAI,GAAG,EAAE,CAAC;IAGnE,QAAQ,CAAkB;IAE1B,OAAO;QACL,qDAAyB,CAAC,OAAO,CAAC,CAAC,KAAK,EAAE,IAAI,EAAE,EAAE;YAChD,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,IAAI,EAAE,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,KAAK,CAAE,CAAC,CAAC;QAClD,CAAC,CAAC,CAAC;IACL,CAAC;IAED,KAAK,CAAC,KAAc,EAAE,GAAc;QAClC,MAAM,IAAI,GAAG,IAAA,aAAM,EAAC,IAAA,cAAO,EAAC,KAAK,CAAC,CAAC,CAAC;QAEpC,IAAI,IAAI,IAAI,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,IAAI,CAAC,EAAE;YAChC,OAAO,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,IAAI,CAAE,CAAC,KAAK,CAAC,KAAK,EAAE,GAAG,CAAC,CAAC;SAChD;QAED,MAAM,MAAM,GAAG,IAAA,kBAAW,EAAC,KAAK,CAAC;aAC9B,OAAO,EAAE;aACT,IAAI,CAAC,CAAC,MAAM,EAAE,EAAE,CAAC,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC,CAAC;QAE5C,IAAI,MAAM,EAAE;YACV,OAAO,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,MAAM,CAAE,CAAC,KAAK,CAAC,KAAK,EAAE,GAAG,CAAC,CAAC;SAClD;QAED,UAAU;QACV,OAAO,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,KAAK,CAAE,CAAC,KAAK,CAAC,KAAK,EAAE,GAAG,CAAC,CAAC;IAClD,CAAC;IAED,gBAAgB,CAAC,GAAc;QAC7B,OAAO,IAAI,CAAC,KAAK,CAAC,IAAI,mCAAgB,CAAC,GAAG,CAAC,OAAO,CAAC,GAAG,CAAC,EAAE,GAAG,CAAC,CAAC;IAChE,CAAC;CACF,CAAA;AA9BC;IADC,IAAA,WAAM,GAAE;sCACC,oBAAe;oDAAC;AAJf,kBAAkB;IAH9B,IAAA,eAAU,EAAC;QACV,OAAO,EAAE,CAAC,yBAAW,EAAE,iCAAe,EAAE,yCAAmB,EAAE,qCAAiB,CAAC;KAChF,CAAC;GACW,kBAAkB,CAkC9B;AAlCY,gDAAkB","sourcesContent":["import {ancestorsOf, classOf, nameOf} from \"@tsed/core\";\nimport {BaseContext, DIContext, 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: DIContext) {\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: DIContext) {\n return this.catch(new ResourceNotFound(ctx.request.url), ctx);\n }\n}\n"]}
|
|
@@ -15,9 +15,8 @@ let ErrorFilter = class ErrorFilter {
|
|
|
15
15
|
.body(env === Env.PROD ? "InternalServerError" : err);
|
|
16
16
|
}
|
|
17
17
|
mapError(error, env) {
|
|
18
|
-
var _a;
|
|
19
18
|
return {
|
|
20
|
-
name:
|
|
19
|
+
name: error.origin?.name || error.name,
|
|
21
20
|
message: error.message,
|
|
22
21
|
status: error.status || 500,
|
|
23
22
|
errors: this.getErrors(error),
|
|
@@ -1 +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,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AAS;QAC5B,OAAO;YACL,IAAI,EAAE,KAAK,CAAC,MAAM,EAAE,IAAI,IAAI,KAAK,CAAC,IAAI;YACtC,OAAO,EAAE,KAAK,CAAC,OAAO;YACtB,MAAM,EAAE,KAAK,CAAC,MAAM,IAAI,GAAG;YAC3B,MAAM,EAAE,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC;YAC7B,KAAK,EAAE,GAAG,KAAK,GAAG,CAAC,GAAG,CAAC,CAAC,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC,CAAC,SAAS;SACjD,CAAC;IACJ,CAAC;IAES,SAAS,CAAC,KAAU;QAC5B,OAAO,CAAC,KAAK,EAAE,KAAK,CAAC,MAAM,CAAC,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC,MAAM,CAAC,CAAC,IAAI,EAAE,EAAC,MAAM,EAAM,EAAE,EAAE;YAC1E,OAAO,IAAI,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC;QAC7C,CAAC,EAAE,EAAE,CAAC,CAAC;IACT,CAAC;IAES,UAAU,CAAC,KAAU;QAC7B,OAAO,CAAC,KAAK,EAAE,KAAK,CAAC,MAAM,CAAC,CAAC,MAAM,CAAC,OAAO,CAAC,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,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"]}
|
|
@@ -1 +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;
|
|
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;IACnC,GAAG,CAAS;IAErB,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"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ExceptionFilterMethods.js","sourceRoot":"","sources":["../../../src/interfaces/ExceptionFilterMethods.ts"],"names":[],"mappings":"","sourcesContent":["import {
|
|
1
|
+
{"version":3,"file":"ExceptionFilterMethods.js","sourceRoot":"","sources":["../../../src/interfaces/ExceptionFilterMethods.ts"],"names":[],"mappings":"","sourcesContent":["import {DIContext} from \"@tsed/di\";\n\nexport interface ExceptionFilterMethods<T = unknown> {\n catch(error: T, ctx: DIContext): void;\n}\n"]}
|
|
@@ -13,9 +13,8 @@ import { ResourceNotFound } from "../errors/ResourceNotFound.js";
|
|
|
13
13
|
* @platform
|
|
14
14
|
*/
|
|
15
15
|
let PlatformExceptions = class PlatformExceptions {
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
}
|
|
16
|
+
types = new Map();
|
|
17
|
+
injector;
|
|
19
18
|
$onInit() {
|
|
20
19
|
ExceptionFiltersContainer.forEach((token, type) => {
|
|
21
20
|
this.types.set(type, this.injector.get(token));
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"PlatformExceptions.js","sourceRoot":"","sources":["../../../src/services/PlatformExceptions.ts"],"names":[],"mappings":";AAAA,OAAO,EAAC,WAAW,EAAE,OAAO,EAAE,MAAM,EAAC,MAAM,YAAY,CAAC;AACxD,OAAO,
|
|
1
|
+
{"version":3,"file":"PlatformExceptions.js","sourceRoot":"","sources":["../../../src/services/PlatformExceptions.ts"],"names":[],"mappings":";AAAA,OAAO,EAAC,WAAW,EAAE,OAAO,EAAE,MAAM,EAAC,MAAM,YAAY,CAAC;AACxD,OAAO,EAAyB,MAAM,EAAE,UAAU,EAAE,eAAe,EAAC,MAAM,UAAU,CAAC;AACrF,OAAO,EAAC,WAAW,EAAC,MAAM,2BAA2B,CAAC;AACtD,OAAO,EAAC,eAAe,EAAC,MAAM,+BAA+B,CAAC;AAC9D,OAAO,EAAC,mBAAmB,EAAC,MAAM,mCAAmC,CAAC;AACtE,OAAO,EAAC,iBAAiB,EAAC,M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AAgB,CAAC,GAAc;QAC7B,OAAO,IAAI,CAAC,KAAK,CAAC,IAAI,gBAAgB,CAAC,GAAG,CAAC,OAAO,CAAC,GAAG,CAAC,EAAE,GAAG,CAAC,CAAC;IAChE,CAAC;CACF,CAAA;AA9BC;IADC,MAAM,EAAE;8BACC,eAAe;oDAAC;AAJf,kBAAkB;IAH9B,UAAU,CAAC;QACV,OAAO,EAAE,CAAC,WAAW,EAAE,eAAe,EAAE,mBAAmB,EAAE,iBAAiB,CAAC;KAChF,CAAC;GACW,kBAAkB,CAkC9B;SAlCY,kBAAkB","sourcesContent":["import {ancestorsOf, classOf, nameOf} from \"@tsed/core\";\nimport {BaseContext, DIContext, 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: DIContext) {\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: DIContext) {\n return this.catch(new ResourceNotFound(ctx.request.url), ctx);\n }\n}\n"]}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { DIContext, InjectorService } from "@tsed/di";
|
|
2
2
|
import { ExceptionFilterKey } from "../domain/ExceptionFiltersContainer";
|
|
3
3
|
import { ExceptionFilterMethods } from "../interfaces/ExceptionFilterMethods";
|
|
4
4
|
/**
|
|
@@ -10,6 +10,6 @@ export declare class PlatformExceptions {
|
|
|
10
10
|
types: Map<ExceptionFilterKey, ExceptionFilterMethods>;
|
|
11
11
|
injector: InjectorService;
|
|
12
12
|
$onInit(): void;
|
|
13
|
-
catch(error: unknown, ctx:
|
|
14
|
-
resourceNotFound(ctx:
|
|
13
|
+
catch(error: unknown, ctx: DIContext): void;
|
|
14
|
+
resourceNotFound(ctx: DIContext): void;
|
|
15
15
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tsed/platform-exceptions",
|
|
3
|
-
"version": "7.0.0-beta.
|
|
3
|
+
"version": "7.0.0-beta.14",
|
|
4
4
|
"description": "Platform exceptions module for Ts.ED Framework",
|
|
5
5
|
"private": false,
|
|
6
6
|
"source": "./src/index.ts",
|
|
@@ -17,22 +17,23 @@
|
|
|
17
17
|
"build": "yarn barrels && yarn run build:esm && yarn run build:cjs",
|
|
18
18
|
"build:cjs": "tsc --build tsconfig.compile.json",
|
|
19
19
|
"build:esm": "tsc --build tsconfig.compile.esm.json",
|
|
20
|
-
"barrels": "yarn barrelsby --delete -d ./src -e \"\\.spec\\.ts\" -e \"__mock__\" -e \".benchmark.ts\""
|
|
20
|
+
"barrels": "yarn barrelsby --delete -d ./src -e \"\\.spec\\.ts\" -e \"__mock__\" -e \".benchmark.ts\"",
|
|
21
|
+
"test": "cross-env NODE_ENV=test yarn jest --max-workers=2"
|
|
21
22
|
},
|
|
22
23
|
"dependencies": {
|
|
23
|
-
"tslib": "2.
|
|
24
|
+
"tslib": "2.4.0"
|
|
24
25
|
},
|
|
25
26
|
"devDependencies": {
|
|
26
|
-
"@tsed/core": "7.0.0-beta.
|
|
27
|
-
"@tsed/di": "7.0.0-beta.
|
|
28
|
-
"@tsed/exceptions": "7.0.0-beta.
|
|
29
|
-
"@tsed/schema": "7.0.0-beta.
|
|
27
|
+
"@tsed/core": "7.0.0-beta.14",
|
|
28
|
+
"@tsed/di": "7.0.0-beta.14",
|
|
29
|
+
"@tsed/exceptions": "7.0.0-beta.14",
|
|
30
|
+
"@tsed/schema": "7.0.0-beta.14"
|
|
30
31
|
},
|
|
31
32
|
"peerDependencies": {
|
|
32
|
-
"@tsed/core": "^7.0.0-beta.
|
|
33
|
-
"@tsed/di": "^7.0.0-beta.
|
|
34
|
-
"@tsed/exceptions": "^7.0.0-beta.
|
|
35
|
-
"@tsed/schema": "^7.0.0-beta.
|
|
33
|
+
"@tsed/core": "^7.0.0-beta.14",
|
|
34
|
+
"@tsed/di": "^7.0.0-beta.14",
|
|
35
|
+
"@tsed/exceptions": "^7.0.0-beta.14",
|
|
36
|
+
"@tsed/schema": "^7.0.0-beta.14"
|
|
36
37
|
},
|
|
37
38
|
"peerDependenciesMeta": {
|
|
38
39
|
"@tsed/core": {
|
|
@@ -54,5 +55,8 @@
|
|
|
54
55
|
},
|
|
55
56
|
"homepage": "https://github.com/tsedio/tsed/tree/beta/packages/platform/platform-exceptions",
|
|
56
57
|
"author": "Romain Lenzotti",
|
|
57
|
-
"license": "MIT"
|
|
58
|
+
"license": "MIT",
|
|
59
|
+
"publishConfig": {
|
|
60
|
+
"tag": "beta"
|
|
61
|
+
}
|
|
58
62
|
}
|
package/readme.md
CHANGED
|
@@ -11,7 +11,8 @@
|
|
|
11
11
|
[](https://badge.fury.io/js/%40tsed%2Fcommon)
|
|
12
12
|
[](https://github.com/semantic-release/semantic-release)
|
|
13
13
|
[](https://github.com/prettier/prettier)
|
|
14
|
-
[](https://github.com/sponsors/romakita)
|
|
15
|
+
[](https://opencollective.com/tsed)
|
|
15
16
|
|
|
16
17
|
</div>
|
|
17
18
|
|