Skip to content

Commit

Permalink
refactor(cactus-core): catch block get-open-api-spec-v1-endpoint-base
Browse files Browse the repository at this point in the history
1. The GetOpenApiSpecV1EndpointBase<S, P> 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 <peter.somogyvari@accenture.com>

Signed-off-by: ArchanaArige <nigam_archana@yahoo.co.in>
Signed-off-by: Peter Somogyvari <peter.somogyvari@accenture.com>
  • Loading branch information
ArchanaArige authored and petermetz committed Jan 22, 2024
1 parent f4373a9 commit 81c93f9
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 30 deletions.
2 changes: 1 addition & 1 deletion packages/cactus-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand All @@ -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;
}
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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<S, P> {
logLevel?: LogLevelDesc;
Expand Down Expand Up @@ -181,16 +180,8 @@ export class GetOpenApiSpecV1EndpointBase<S, P> 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 });
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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);
}
}
10 changes: 5 additions & 5 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down

0 comments on commit 81c93f9

Please sign in to comment.