Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(cactus-core): catch block get-open-api-spec-v1-endpoint-base #2979

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading