@twin.org/api-models 0.0.3-next.13 → 0.0.3-next.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.
|
@@ -6,6 +6,19 @@ import { HeaderTypes, HttpStatusCode, MimeTypes } from "@twin.org/web";
|
|
|
6
6
|
* Class to help with processing http errors.
|
|
7
7
|
*/
|
|
8
8
|
export class HttpErrorHelper {
|
|
9
|
+
/**
|
|
10
|
+
* Mapping of error types to status codes.
|
|
11
|
+
*/
|
|
12
|
+
static ERROR_TYPE_MAP = {
|
|
13
|
+
[GuardError.CLASS_NAME]: HttpStatusCode.badRequest,
|
|
14
|
+
[ValidationError.CLASS_NAME]: HttpStatusCode.badRequest,
|
|
15
|
+
[ConflictError.CLASS_NAME]: HttpStatusCode.conflict,
|
|
16
|
+
[AlreadyExistsError.CLASS_NAME]: HttpStatusCode.conflict,
|
|
17
|
+
[NotFoundError.CLASS_NAME]: HttpStatusCode.notFound,
|
|
18
|
+
[UnauthorizedError.CLASS_NAME]: HttpStatusCode.unauthorized,
|
|
19
|
+
[NotImplementedError.CLASS_NAME]: HttpStatusCode.forbidden,
|
|
20
|
+
[UnprocessableError.CLASS_NAME]: HttpStatusCode.unprocessableEntity
|
|
21
|
+
};
|
|
9
22
|
/**
|
|
10
23
|
* Process the errors from the routes.
|
|
11
24
|
* @param err The error to process.
|
|
@@ -18,27 +31,17 @@ export class HttpErrorHelper {
|
|
|
18
31
|
// types then set the http response code accordingly
|
|
19
32
|
const flattened = BaseError.flatten(error);
|
|
20
33
|
let httpStatusCode = HttpStatusCode.internalServerError;
|
|
21
|
-
const errorTypeMap = {
|
|
22
|
-
[GuardError.CLASS_NAME]: HttpStatusCode.badRequest,
|
|
23
|
-
[ValidationError.CLASS_NAME]: HttpStatusCode.badRequest,
|
|
24
|
-
[ConflictError.CLASS_NAME]: HttpStatusCode.conflict,
|
|
25
|
-
[AlreadyExistsError.CLASS_NAME]: HttpStatusCode.conflict,
|
|
26
|
-
[NotFoundError.CLASS_NAME]: HttpStatusCode.notFound,
|
|
27
|
-
[UnauthorizedError.CLASS_NAME]: HttpStatusCode.unauthorized,
|
|
28
|
-
[NotImplementedError.CLASS_NAME]: HttpStatusCode.forbidden,
|
|
29
|
-
[UnprocessableError.CLASS_NAME]: HttpStatusCode.unprocessableEntity
|
|
30
|
-
};
|
|
31
34
|
// First check the primary error, as we don't want to override that with a sub error
|
|
32
35
|
if (flattened.length > 0) {
|
|
33
36
|
const primaryError = flattened[0];
|
|
34
|
-
if (
|
|
35
|
-
httpStatusCode =
|
|
37
|
+
if (HttpErrorHelper.ERROR_TYPE_MAP[primaryError.name]) {
|
|
38
|
+
httpStatusCode = HttpErrorHelper.ERROR_TYPE_MAP[primaryError.name];
|
|
36
39
|
}
|
|
37
40
|
// The primary error is still internal server error, check the sub errors
|
|
38
41
|
if (httpStatusCode === HttpStatusCode.internalServerError) {
|
|
39
|
-
for (const className in
|
|
42
|
+
for (const className in HttpErrorHelper.ERROR_TYPE_MAP) {
|
|
40
43
|
if (flattened.some(e => BaseError.isErrorName(e, className))) {
|
|
41
|
-
httpStatusCode =
|
|
44
|
+
httpStatusCode = HttpErrorHelper.ERROR_TYPE_MAP[className];
|
|
42
45
|
break;
|
|
43
46
|
}
|
|
44
47
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"httpErrorHelper.js","sourceRoot":"","sources":["../../../src/helpers/httpErrorHelper.ts"],"names":[],"mappings":"AAAA,gCAAgC;AAChC,uCAAuC;AACvC,OAAO,EACN,kBAAkB,EAClB,SAAS,EACT,aAAa,EACb,UAAU,EAEV,aAAa,EACb,mBAAmB,EACnB,iBAAiB,EACjB,kBAAkB,EAClB,eAAe,EACf,MAAM,gBAAgB,CAAC;AACxB,OAAO,EAAE,WAAW,EAAE,cAAc,EAAE,SAAS,EAAE,MAAM,eAAe,CAAC;AAGvE;;GAEG;AACH,MAAM,OAAO,eAAe;IAC3B
|
|
1
|
+
{"version":3,"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AAC,MAAM,GAAG,CAAC,EAAE,CAAC;YAC1B,MAAM,YAAY,GAAG,SAAS,CAAC,CAAC,CAAC,CAAC;YAClC,IAAI,eAAe,CAAC,cAAc,CAAC,YAAY,CAAC,IAAI,CAAC,EAAE,CAAC;gBACvD,cAAc,GAAG,eAAe,CAAC,cAAc,CAAC,YAAY,CAAC,IAAI,CAAC,CAAC;YACpE,CAAC;YAED,yEAAyE;YACzE,IAAI,cAAc,KAAK,cAAc,CAAC,mBAAmB,EAAE,CAAC;gBAC3D,KAAK,MAAM,SAAS,IAAI,eAAe,CAAC,cAAc,EAAE,CAAC;oBACxD,IAAI,SAAS,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,SAAS,CAAC,WAAW,CAAC,CAAC,EAAE,SAAS,CAAC,CAAC,EAAE,CAAC;wBAC9D,cAAc,GAAG,eAAe,CAAC,cAAc,CAAC,SAAS,CAAC,CAAC;wBAC3D,MAAM;oBACP,CAAC;gBACF,CAAC;YACF,CAAC;QACF,CAAC;QAED,MAAM,WAAW,GAAG,KAAK,CAAC,YAAY,CAAC,YAAY,CAAC,CAAC;QAErD,OAAO;YACN,KAAK,EAAE,WAAW;YAClB,cAAc;SACd,CAAC;IACH,CAAC;IAED;;;;;OAKG;IACI,MAAM,CAAC,aAAa,CAC1B,QAAuB,EACvB,KAAa,EACb,UAA0B;QAE1B,QAAQ,CAAC,OAAO,KAAK,EAAE,CAAC;QACxB,QAAQ,CAAC,OAAO,CAAC,WAAW,CAAC,WAAW,CAAC,GAAG,GAAG,SAAS,CAAC,IAAI,iBAAiB,CAAC;QAC/E,QAAQ,CAAC,IAAI,GAAG,KAAK,CAAC;QACtB,QAAQ,CAAC,UAAU,GAAG,UAAU,CAAC;IAClC,CAAC","sourcesContent":["// Copyright 2024 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\nimport {\n\tAlreadyExistsError,\n\tBaseError,\n\tConflictError,\n\tGuardError,\n\ttype IError,\n\tNotFoundError,\n\tNotImplementedError,\n\tUnauthorizedError,\n\tUnprocessableError,\n\tValidationError\n} from \"@twin.org/core\";\nimport { HeaderTypes, HttpStatusCode, MimeTypes } from \"@twin.org/web\";\nimport type { IHttpResponse } from \"../models/protocol/IHttpResponse.js\";\n\n/**\n * Class to help with processing http errors.\n */\nexport class HttpErrorHelper {\n\t/**\n\t * Mapping of error types to status codes.\n\t */\n\tpublic static readonly ERROR_TYPE_MAP: { [id: string]: HttpStatusCode } = {\n\t\t[GuardError.CLASS_NAME]: HttpStatusCode.badRequest,\n\t\t[ValidationError.CLASS_NAME]: HttpStatusCode.badRequest,\n\t\t[ConflictError.CLASS_NAME]: HttpStatusCode.conflict,\n\t\t[AlreadyExistsError.CLASS_NAME]: HttpStatusCode.conflict,\n\t\t[NotFoundError.CLASS_NAME]: HttpStatusCode.notFound,\n\t\t[UnauthorizedError.CLASS_NAME]: HttpStatusCode.unauthorized,\n\t\t[NotImplementedError.CLASS_NAME]: HttpStatusCode.forbidden,\n\t\t[UnprocessableError.CLASS_NAME]: HttpStatusCode.unprocessableEntity\n\t};\n\n\t/**\n\t * Process the errors from the routes.\n\t * @param err The error to process.\n\t * @param includeStack Should the stack be included in the error.\n\t * @returns The status code and additional error data.\n\t */\n\tpublic static processError(\n\t\terr: unknown,\n\t\tincludeStack?: boolean\n\t): {\n\t\terror: IError;\n\t\thttpStatusCode: HttpStatusCode;\n\t} {\n\t\tconst error: BaseError = BaseError.fromError(err);\n\n\t\t// If the error or any of its sub errors are of the specific\n\t\t// types then set the http response code accordingly\n\t\tconst flattened = BaseError.flatten(error);\n\n\t\tlet httpStatusCode: HttpStatusCode = HttpStatusCode.internalServerError;\n\n\t\t// First check the primary error, as we don't want to override that with a sub error\n\t\tif (flattened.length > 0) {\n\t\t\tconst primaryError = flattened[0];\n\t\t\tif (HttpErrorHelper.ERROR_TYPE_MAP[primaryError.name]) {\n\t\t\t\thttpStatusCode = HttpErrorHelper.ERROR_TYPE_MAP[primaryError.name];\n\t\t\t}\n\n\t\t\t// The primary error is still internal server error, check the sub errors\n\t\t\tif (httpStatusCode === HttpStatusCode.internalServerError) {\n\t\t\t\tfor (const className in HttpErrorHelper.ERROR_TYPE_MAP) {\n\t\t\t\t\tif (flattened.some(e => BaseError.isErrorName(e, className))) {\n\t\t\t\t\t\thttpStatusCode = HttpErrorHelper.ERROR_TYPE_MAP[className];\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\tconst returnError = error.toJsonObject(includeStack);\n\n\t\treturn {\n\t\t\terror: returnError,\n\t\t\thttpStatusCode\n\t\t};\n\t}\n\n\t/**\n\t * Build an error response.\n\t * @param response The response to build the error into.\n\t * @param error The error to build the response for.\n\t * @param statusCode The status code to use for the error.\n\t */\n\tpublic static buildResponse(\n\t\tresponse: IHttpResponse,\n\t\terror: IError,\n\t\tstatusCode: HttpStatusCode\n\t): void {\n\t\tresponse.headers ??= {};\n\t\tresponse.headers[HeaderTypes.ContentType] = `${MimeTypes.Json}; charset=utf-8`;\n\t\tresponse.body = error;\n\t\tresponse.statusCode = statusCode;\n\t}\n}\n"]}
|
|
@@ -5,6 +5,12 @@ import type { IHttpResponse } from "../models/protocol/IHttpResponse.js";
|
|
|
5
5
|
* Class to help with processing http errors.
|
|
6
6
|
*/
|
|
7
7
|
export declare class HttpErrorHelper {
|
|
8
|
+
/**
|
|
9
|
+
* Mapping of error types to status codes.
|
|
10
|
+
*/
|
|
11
|
+
static readonly ERROR_TYPE_MAP: {
|
|
12
|
+
[id: string]: HttpStatusCode;
|
|
13
|
+
};
|
|
8
14
|
/**
|
|
9
15
|
* Process the errors from the routes.
|
|
10
16
|
* @param err The error to process.
|
package/docs/changelog.md
CHANGED
|
@@ -1,5 +1,12 @@
|
|
|
1
1
|
# @twin.org/api-models - Changelog
|
|
2
2
|
|
|
3
|
+
## [0.0.3-next.14](https://github.com/twinfoundation/api/compare/api-models-v0.0.3-next.13...api-models-v0.0.3-next.14) (2026-01-20)
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
### Features
|
|
7
|
+
|
|
8
|
+
* export error type map ([#68](https://github.com/twinfoundation/api/issues/68)) ([697dfc4](https://github.com/twinfoundation/api/commit/697dfc4c9f6a7be493bf4b3619d7bcebf2e4584e))
|
|
9
|
+
|
|
3
10
|
## [0.0.3-next.13](https://github.com/twinfoundation/api/compare/api-models-v0.0.3-next.12...api-models-v0.0.3-next.13) (2026-01-19)
|
|
4
11
|
|
|
5
12
|
|
|
@@ -12,6 +12,18 @@ Class to help with processing http errors.
|
|
|
12
12
|
|
|
13
13
|
`HttpErrorHelper`
|
|
14
14
|
|
|
15
|
+
## Properties
|
|
16
|
+
|
|
17
|
+
### ERROR\_TYPE\_MAP
|
|
18
|
+
|
|
19
|
+
> `readonly` `static` **ERROR\_TYPE\_MAP**: `object`
|
|
20
|
+
|
|
21
|
+
Mapping of error types to status codes.
|
|
22
|
+
|
|
23
|
+
#### Index Signature
|
|
24
|
+
|
|
25
|
+
\[`id`: `string`\]: `HttpStatusCode`
|
|
26
|
+
|
|
15
27
|
## Methods
|
|
16
28
|
|
|
17
29
|
### processError()
|