From 81c93f902d9034f7ea3951d6c1c6a85765ccd393 Mon Sep 17 00:00:00 2001 From: ArchanaArige Date: Tue, 16 Jan 2024 13:15:31 +0000 Subject: [PATCH] refactor(cactus-core): catch block get-open-api-spec-v1-endpoint-base 1. The GetOpenApiSpecV1EndpointBase class now has the request handler migrated to the new error handling mechanism that automatically differentiates between HTTP statuses of 4xx and 5xx. 2. The register-web-service-endpoint utility function uses the new error handling utilities as well, but on the lower level, e.g. it coerces unknown errors to RuntimeError instances to minimize the risk of information loss when re-throwing the inner exception with more context attached to it. 3. The consensus-has-transaction-finality file was similarly migrated to the new error handling mechanisms. 4. A dependency to the cactus-core package was added for the easier handling of HTTP specific errors. The http-errors-enhanced-cjs package contains useful shorthands for HTTP-aware error handling and can be used in common JS environments instead of being constrainted to ESM only like the original library. Co-authored-by: Peter Somogyvari Signed-off-by: ArchanaArige Signed-off-by: Peter Somogyvari --- packages/cactus-core/package.json | 2 +- .../consensus-has-transaction-finality.ts | 27 ++++++++++++++----- .../get-open-api-spec-v1-endpoint-base.ts | 15 +++-------- .../handle-rest-endpoint-exception.ts | 2 +- .../register-web-service-endpoint.ts | 10 +++---- yarn.lock | 10 +++---- 6 files changed, 36 insertions(+), 30 deletions(-) diff --git a/packages/cactus-core/package.json b/packages/cactus-core/package.json index 1913d39c06..efce4f755a 100644 --- a/packages/cactus-core/package.json +++ b/packages/cactus-core/package.json @@ -56,7 +56,7 @@ "express-jwt-authz": "2.4.1", "express-openapi-validator": "5.0.4", "http-errors": "2.0.0", - "http-errors-enhanced": "1.1.2", + "http-errors-enhanced-cjs": "2.0.1", "run-time-error-cjs": "1.4.0", "safe-stable-stringify": "2.4.3", "typescript-optional": "2.0.1" diff --git a/packages/cactus-core/src/main/typescript/consensus-has-transaction-finality.ts b/packages/cactus-core/src/main/typescript/consensus-has-transaction-finality.ts index f4e2765900..db8be75e16 100644 --- a/packages/cactus-core/src/main/typescript/consensus-has-transaction-finality.ts +++ b/packages/cactus-core/src/main/typescript/consensus-has-transaction-finality.ts @@ -4,9 +4,21 @@ import { ConsensusAlgorithmFamiliesWithOutTxFinality, } from "@hyperledger/cactus-core-api"; +import { BadRequestError } from "http-errors-enhanced-cjs"; + export function consensusHasTransactionFinality( consensusAlgorithmFamily: ConsensusAlgorithmFamily, ): boolean { + const woTxFinalityValues = Object.values( + ConsensusAlgorithmFamiliesWithOutTxFinality, + ); + const withTxFinalityValues = Object.values( + ConsensusAlgorithmFamiliesWithTxFinality, + ); + + const acceptedValues = [...woTxFinalityValues, ...withTxFinalityValues]; + const acceptedValuesCsv = acceptedValues.join(","); + const isInConsensusAlgorithmFamiliesWithTxFinality = ( Object.values(ConsensusAlgorithmFamiliesWithTxFinality) as string[] ).includes(consensusAlgorithmFamily.toString()); @@ -19,10 +31,13 @@ export function consensusHasTransactionFinality( !isInConsensusAlgorithmFamiliesWithTxFinality && !isInConsensusAlgorithmFamiliesWithOutTxFinality; - if (unrecognizedConsensusAlgorithmFamily) { - throw new Error( - `Unrecognized consensus algorithm family: ${consensusAlgorithmFamily}`, - ); - } - return isInConsensusAlgorithmFamiliesWithTxFinality; + if (unrecognizedConsensusAlgorithmFamily) { + throw new BadRequestError( + `Unrecognized consensus algorithm family: ${consensusAlgorithmFamily}`, + { + acceptedValuesCsv, + }, + ); + } + return isInConsensusAlgorithmFamiliesWithTxFinality; } diff --git a/packages/cactus-core/src/main/typescript/web-services/get-open-api-spec-v1-endpoint-base.ts b/packages/cactus-core/src/main/typescript/web-services/get-open-api-spec-v1-endpoint-base.ts index 42df17b5ac..0a699ca6df 100644 --- a/packages/cactus-core/src/main/typescript/web-services/get-open-api-spec-v1-endpoint-base.ts +++ b/packages/cactus-core/src/main/typescript/web-services/get-open-api-spec-v1-endpoint-base.ts @@ -1,6 +1,4 @@ import type { Express, Request, Response } from "express"; -import { RuntimeError } from "run-time-error-cjs"; -import { stringify } from "safe-stable-stringify"; import { Logger, @@ -19,6 +17,7 @@ import { import { PluginRegistry } from "../plugin-registry"; import { registerWebServiceEndpoint } from "./register-web-service-endpoint"; +import { handleRestEndpointException } from "./handle-rest-endpoint-exception"; export interface IGetOpenApiSpecV1EndpointBaseOptions { logLevel?: LogLevelDesc; @@ -181,16 +180,8 @@ export class GetOpenApiSpecV1EndpointBase implements IWebServiceEndpoint { res.status(200); res.json(oas); } catch (ex: unknown) { - const eMsg = `${fnTag} failed to serve request: ${reqMeta}`; - this.log.debug(eMsg, ex); - - const cause = ex instanceof Error ? ex : stringify(ex); - const error = new RuntimeError(eMsg, cause); - - res.status(500).json({ - message: "Internal Server Error", - error, - }); + const errorMsg = `${fnTag} request handler fn crashed for: ${reqMeta}`; + handleRestEndpointException({ errorMsg, log: this.log, error: ex, res }); } } } diff --git a/packages/cactus-core/src/main/typescript/web-services/handle-rest-endpoint-exception.ts b/packages/cactus-core/src/main/typescript/web-services/handle-rest-endpoint-exception.ts index 56b02462bc..56ffb0dbc3 100644 --- a/packages/cactus-core/src/main/typescript/web-services/handle-rest-endpoint-exception.ts +++ b/packages/cactus-core/src/main/typescript/web-services/handle-rest-endpoint-exception.ts @@ -42,7 +42,7 @@ export async function handleRestEndpointException( const errorAsSanitizedJson = safeStringifyException(ctx.error); const { identifierByCodes, INTERNAL_SERVER_ERROR } = await import( - "http-errors-enhanced" + "http-errors-enhanced-cjs" ); if (createHttpError.isHttpError(ctx.error)) { diff --git a/packages/cactus-core/src/main/typescript/web-services/register-web-service-endpoint.ts b/packages/cactus-core/src/main/typescript/web-services/register-web-service-endpoint.ts index 88b6745a4e..6771175400 100644 --- a/packages/cactus-core/src/main/typescript/web-services/register-web-service-endpoint.ts +++ b/packages/cactus-core/src/main/typescript/web-services/register-web-service-endpoint.ts @@ -3,6 +3,8 @@ import { Express } from "express"; import { IWebServiceEndpoint } from "@hyperledger/cactus-core-api"; +import { createRuntimeErrorWithCause } from "@hyperledger/cactus-common"; + /** * Hooks up an endpoint instance to an ExpressJS web app object. * @@ -40,10 +42,8 @@ export async function registerWebServiceEndpoint( } else { registrationMethod(httpPath, requestHandler); } - } catch (ex) { - throw new Error( - `${fnTag} Express verb method ${httpVerb} threw ` + - ` while registering endpoint: ${ex.message}`, - ); + } catch (ex: unknown) { + const errorMessage = `${fnTag} Express verb method ${httpVerb} threw while registering endpoint on path ${httpPath}`; + throw createRuntimeErrorWithCause(errorMessage, ex); } } diff --git a/yarn.lock b/yarn.lock index 333145d3fd..94ad34edbd 100644 --- a/yarn.lock +++ b/yarn.lock @@ -7118,7 +7118,7 @@ __metadata: express-jwt-authz: 2.4.1 express-openapi-validator: 5.0.4 http-errors: 2.0.0 - http-errors-enhanced: 1.1.2 + http-errors-enhanced-cjs: 2.0.1 node-mocks-http: 1.14.0 run-time-error-cjs: 1.4.0 safe-stable-stringify: 2.4.3 @@ -28885,10 +28885,10 @@ __metadata: languageName: node linkType: hard -"http-errors-enhanced@npm:1.1.2": - version: 1.1.2 - resolution: "http-errors-enhanced@npm:1.1.2" - checksum: 1bc38968cc4a0dcb6a2d25e557d0321f3fa17da2f1a6b898158fa3b9bb103fd5374b38e60d87926e5adf9310fd7b6136023754b0db1489a6e80d6d242375ba0f +"http-errors-enhanced-cjs@npm:2.0.1": + version: 2.0.1 + resolution: "http-errors-enhanced-cjs@npm:2.0.1" + checksum: b339f934c37c352a12ac3d2e0d5a07e74f1346322bafd11ea9ff21805c62208815f258ce92dc85d8ebb151339e7fcd006dbfbc98af07eca76acae7310c92cfd2 languageName: node linkType: hard