diff --git a/packages/cactus-plugin-ledger-connector-fabric/src/main/typescript/deploy-contract-go-source/deploy-contract-go-source-endpoint-v1.ts b/packages/cactus-plugin-ledger-connector-fabric/src/main/typescript/deploy-contract-go-source/deploy-contract-go-source-endpoint-v1.ts index f327b6cef6a..8ad42cddf9e 100644 --- a/packages/cactus-plugin-ledger-connector-fabric/src/main/typescript/deploy-contract-go-source/deploy-contract-go-source-endpoint-v1.ts +++ b/packages/cactus-plugin-ledger-connector-fabric/src/main/typescript/deploy-contract-go-source/deploy-contract-go-source-endpoint-v1.ts @@ -16,7 +16,10 @@ import { IEndpointAuthzOptions, } from "@hyperledger/cactus-core-api"; -import { registerWebServiceEndpoint } from "@hyperledger/cactus-core"; +import { + handleRestEndpointException, + registerWebServiceEndpoint, +} from "@hyperledger/cactus-core"; import { PluginLedgerConnectorFabric } from "../plugin-ledger-connector-fabric"; import { DeployContractGoSourceV1Request } from "../generated/openapi/typescript-axios/index"; @@ -106,8 +109,8 @@ export class DeployContractGoSourceEndpointV1 implements IWebServiceEndpoint { async handleRequest(req: Request, res: Response): Promise { const fnTag = `${this.className}#handleRequest()`; const verbUpper = this.getVerbLowerCase().toUpperCase(); - this.log.debug(`${verbUpper} ${this.getPath()}`); - + const reqTag = `${verbUpper} ${this.getPath()}`; + this.log.debug(reqTag); try { const { connector } = this.opts; const reqBody = req.body as DeployContractGoSourceV1Request; @@ -116,10 +119,8 @@ export class DeployContractGoSourceEndpointV1 implements IWebServiceEndpoint { res.status(HttpStatus.OK); res.json(resBody); } catch (ex) { - this.log.error(`${fnTag} failed to serve contract deploy request`, ex); - res.status(HttpStatus.INTERNAL_SERVER_ERROR); - res.statusMessage = ex.message; - res.json({ error: ex.stack }); + const errorMsg = `${fnTag} request handler fn crashed for: ${reqTag}`; + await handleRestEndpointException({ errorMsg, log: this.log, error: ex, res }); } } } diff --git a/packages/cactus-plugin-ledger-connector-fabric/src/main/typescript/deploy-contract/deploy-contract-endpoint-v1.ts b/packages/cactus-plugin-ledger-connector-fabric/src/main/typescript/deploy-contract/deploy-contract-endpoint-v1.ts index 55a8d5d4d79..124cde84793 100644 --- a/packages/cactus-plugin-ledger-connector-fabric/src/main/typescript/deploy-contract/deploy-contract-endpoint-v1.ts +++ b/packages/cactus-plugin-ledger-connector-fabric/src/main/typescript/deploy-contract/deploy-contract-endpoint-v1.ts @@ -15,7 +15,10 @@ import { IEndpointAuthzOptions, } from "@hyperledger/cactus-core-api"; -import { registerWebServiceEndpoint } from "@hyperledger/cactus-core"; +import { + handleRestEndpointException, + registerWebServiceEndpoint, +} from "@hyperledger/cactus-core"; import { PluginLedgerConnectorFabric } from "../plugin-ledger-connector-fabric"; import { DeployContractV1Request } from "../generated/openapi/typescript-axios/index"; @@ -87,7 +90,8 @@ export class DeployContractEndpointV1 implements IWebServiceEndpoint { async handleRequest(req: Request, res: Response): Promise { const fnTag = `${this.className}#handleRequest()`; const verbUpper = this.getVerbLowerCase().toUpperCase(); - this.log.debug(`${verbUpper} ${this.getPath()}`); + const reqTag = `${verbUpper} ${this.getPath()}`; + this.log.debug(reqTag); try { const { connector } = this.opts; @@ -96,10 +100,8 @@ export class DeployContractEndpointV1 implements IWebServiceEndpoint { res.status(HttpStatus.OK); res.json(resBody); } catch (ex) { - this.log.error(`${fnTag} failed to serve contract deploy request`, ex); - res.status(HttpStatus.INTERNAL_SERVER_ERROR); - res.statusMessage = ex.message; - res.json({ error: ex.stack }); + const errorMsg = `${fnTag} request handler fn crashed for: ${reqTag}`; + await handleRestEndpointException({ errorMsg, log: this.log, error: ex, res }); } } } diff --git a/packages/cactus-plugin-ledger-connector-fabric/src/main/typescript/get-block/get-block-endpoint-v1.ts b/packages/cactus-plugin-ledger-connector-fabric/src/main/typescript/get-block/get-block-endpoint-v1.ts index b9399d5f515..6b0bf8ddc6e 100644 --- a/packages/cactus-plugin-ledger-connector-fabric/src/main/typescript/get-block/get-block-endpoint-v1.ts +++ b/packages/cactus-plugin-ledger-connector-fabric/src/main/typescript/get-block/get-block-endpoint-v1.ts @@ -28,10 +28,16 @@ export interface IGetBlockEndpointV1Options { } export class GetBlockEndpointV1 implements IWebServiceEndpoint { + public static readonly CLASS_NAME = "GetBlockEndpointV1"; + private readonly log: Logger; + public get className(): string { + return GetBlockEndpointV1.CLASS_NAME; + } + constructor(public readonly opts: IGetBlockEndpointV1Options) { - const fnTag = "GetBlockEndpointV1#constructor()"; + const fnTag = `${this.className}#constructor()`; Checks.truthy(opts, `${fnTag} options`); Checks.truthy(opts.connector, `${fnTag} options.connector`); @@ -84,14 +90,17 @@ export class GetBlockEndpointV1 implements IWebServiceEndpoint { } async handleRequest(req: Request, res: Response): Promise { - const fnTag = "GetBlockEndpointV1#handleRequest()"; - this.log.debug(`POST ${this.getPath()}`); + const fnTag = `${this.className}#handleRequest()`; + const verbUpper = this.getVerbLowerCase().toUpperCase(); + const reqTag = `${verbUpper} ${this.getPath()}`; + this.log.debug(reqTag); try { res.status(200).send(await this.opts.connector.getBlock(req.body)); - } catch (error) { - const errorMsg = `Crash while serving ${fnTag}`; - handleRestEndpointException({ errorMsg, log: this.log, error, res }); + } catch (ex) { + const errorMsg = `${fnTag} request handler fn crashed for: ${reqTag}`; + await handleRestEndpointException({ errorMsg, log: this.log, error: ex, res }); } } } + diff --git a/packages/cactus-plugin-ledger-connector-fabric/src/main/typescript/get-prometheus-exporter-metrics/get-prometheus-exporter-metrics-endpoint-v1.ts b/packages/cactus-plugin-ledger-connector-fabric/src/main/typescript/get-prometheus-exporter-metrics/get-prometheus-exporter-metrics-endpoint-v1.ts index 4886332e7ea..2aea6157844 100644 --- a/packages/cactus-plugin-ledger-connector-fabric/src/main/typescript/get-prometheus-exporter-metrics/get-prometheus-exporter-metrics-endpoint-v1.ts +++ b/packages/cactus-plugin-ledger-connector-fabric/src/main/typescript/get-prometheus-exporter-metrics/get-prometheus-exporter-metrics-endpoint-v1.ts @@ -16,7 +16,10 @@ import { import OAS from "../../json/openapi.json"; -import { registerWebServiceEndpoint } from "@hyperledger/cactus-core"; +import { + handleRestEndpointException, + registerWebServiceEndpoint, +} from "@hyperledger/cactus-core"; import { PluginLedgerConnectorFabric } from "../plugin-ledger-connector-fabric"; @@ -26,14 +29,19 @@ export interface IGetPrometheusExporterMetricsEndpointV1Options { } export class GetPrometheusExporterMetricsEndpointV1 - implements IWebServiceEndpoint -{ + implements IWebServiceEndpoint { + public static readonly CLASS_NAME = "GetPrometheusExporterMetricsEndpointV1"; + private readonly log: Logger; + public get className(): string { + return GetPrometheusExporterMetricsEndpointV1.CLASS_NAME; + } + constructor( public readonly opts: IGetPrometheusExporterMetricsEndpointV1Options, ) { - const fnTag = "GetPrometheusExporterMetricsEndpointV1#constructor()"; + const fnTag = `${this.className}#constructor()`; Checks.truthy(opts, `${fnTag} options`); Checks.truthy(opts.connector, `${fnTag} options.connector`); @@ -83,19 +91,18 @@ export class GetPrometheusExporterMetricsEndpointV1 } async handleRequest(req: Request, res: Response): Promise { - const fnTag = "GetPrometheusExporterMetrics#handleRequest()"; + const fnTag = `${this.className}#handleRequest()`; const verbUpper = this.getVerbLowerCase().toUpperCase(); - this.log.debug(`${verbUpper} ${this.getPath()}`); + const reqTag = `${verbUpper} ${this.getPath()}`; + this.log.debug(reqTag); try { const resBody = await this.opts.connector.getPrometheusExporterMetrics(); res.status(200); res.send(resBody); } catch (ex) { - this.log.error(`${fnTag} failed to serve request`, ex); - res.status(500); - res.statusMessage = ex.message; - res.json({ error: ex.stack }); + const errorMsg = `${fnTag} request handler fn crashed for: ${reqTag}`; + await handleRestEndpointException({ errorMsg, log: this.log, error: ex, res }); } } } diff --git a/packages/cactus-plugin-ledger-connector-fabric/src/main/typescript/get-transaction-receipt/get-transaction-receipt-by-txid-endpoint-v1.ts b/packages/cactus-plugin-ledger-connector-fabric/src/main/typescript/get-transaction-receipt/get-transaction-receipt-by-txid-endpoint-v1.ts index 7f05408ea4d..289b3dd4dcd 100644 --- a/packages/cactus-plugin-ledger-connector-fabric/src/main/typescript/get-transaction-receipt/get-transaction-receipt-by-txid-endpoint-v1.ts +++ b/packages/cactus-plugin-ledger-connector-fabric/src/main/typescript/get-transaction-receipt/get-transaction-receipt-by-txid-endpoint-v1.ts @@ -14,7 +14,10 @@ import { IEndpointAuthzOptions, } from "@hyperledger/cactus-core-api"; -import { registerWebServiceEndpoint } from "@hyperledger/cactus-core"; +import { + handleRestEndpointException, + registerWebServiceEndpoint, +} from "@hyperledger/cactus-core"; import { PluginLedgerConnectorFabric } from "../plugin-ledger-connector-fabric"; import { RunTransactionRequest } from "../generated/openapi/typescript-axios"; @@ -26,12 +29,17 @@ export interface IRunTransactionEndpointV1Options { } export class GetTransactionReceiptByTxIDEndpointV1 - implements IWebServiceEndpoint -{ + implements IWebServiceEndpoint { + public static readonly CLASS_NAME = "GetTransactionReceiptByTxIDEndpointV1"; + private readonly log: Logger; + public get className(): string { + return GetTransactionReceiptByTxIDEndpointV1.CLASS_NAME; + } + constructor(public readonly opts: IRunTransactionEndpointV1Options) { - const fnTag = "GetTransactionReceiptByTxIDEndpointV1#constructor()"; + const fnTag = `${this.className}#constructor()`; Checks.truthy(opts, `${fnTag} options`); Checks.truthy(opts.connector, `${fnTag} options.connector`); @@ -84,8 +92,10 @@ export class GetTransactionReceiptByTxIDEndpointV1 } async handleRequest(req: Request, res: Response): Promise { - const fnTag = "GetTransactionReceiptByTxIDEndpointV1#handleRequest()"; - this.log.debug(`POST ${this.getPath()}`); + const fnTag = `${this.className}#handleRequest()`; + const verbUpper = this.getVerbLowerCase().toUpperCase(); + const reqTag = `${verbUpper} ${this.getPath()}`; + this.log.debug(reqTag); try { const reqBody = req.body as RunTransactionRequest; @@ -94,10 +104,8 @@ export class GetTransactionReceiptByTxIDEndpointV1 res.status(200); res.json(resBody); } catch (ex) { - this.log.error(`${fnTag} failed to serve request`, ex); - res.status(500); - res.statusMessage = ex.message; - res.json({ error: ex.stack }); + const errorMsg = `${fnTag} request handler fn crashed for: ${reqTag}`; + await handleRestEndpointException({ errorMsg, log: this.log, error: ex, res }); } } } diff --git a/packages/cactus-plugin-ledger-connector-fabric/src/main/typescript/run-transaction/run-delegated-sign-transaction-endpoint-v1.ts b/packages/cactus-plugin-ledger-connector-fabric/src/main/typescript/run-transaction/run-delegated-sign-transaction-endpoint-v1.ts index 257150c2e39..14cd38b194b 100644 --- a/packages/cactus-plugin-ledger-connector-fabric/src/main/typescript/run-transaction/run-delegated-sign-transaction-endpoint-v1.ts +++ b/packages/cactus-plugin-ledger-connector-fabric/src/main/typescript/run-transaction/run-delegated-sign-transaction-endpoint-v1.ts @@ -6,7 +6,6 @@ import { LogLevelDesc, Checks, IAsyncProvider, - safeStringifyException, } from "@hyperledger/cactus-common"; import { @@ -15,7 +14,11 @@ import { IEndpointAuthzOptions, } from "@hyperledger/cactus-core-api"; -import { registerWebServiceEndpoint } from "@hyperledger/cactus-core"; +import { + handleRestEndpointException, + registerWebServiceEndpoint, +} from "@hyperledger/cactus-core"; + import { PluginLedgerConnectorFabric } from "../plugin-ledger-connector-fabric"; import OAS from "../../json/openapi.json"; @@ -26,15 +29,19 @@ export interface IRunDelegatedSignTransactionEndpointV1Options { } export class RunDelegatedSignTransactionEndpointV1 - implements IWebServiceEndpoint -{ + implements IWebServiceEndpoint { + public static readonly CLASS_NAME = "RunDelegatedSignTransactionEndpointV1"; + private readonly log: Logger; + public get className(): string { + return RunDelegatedSignTransactionEndpointV1.CLASS_NAME; + } + constructor( public readonly opts: IRunDelegatedSignTransactionEndpointV1Options, ) { - const fnTag = "RunDelegatedSignTransactionEndpointV1#constructor()"; - + const fnTag = `${this.className}#constructor()`; Checks.truthy(opts, `${fnTag} options`); Checks.truthy(opts.connector, `${fnTag} options.connector`); @@ -84,19 +91,18 @@ export class RunDelegatedSignTransactionEndpointV1 } async handleRequest(req: Request, res: Response): Promise { - const fnTag = "RunDelegatedSignTransactionEndpointV1#handleRequest()"; - this.log.debug(`POST ${this.getPath()}`); + const fnTag = `${this.className}#handleRequest()`; + const verbUpper = this.getVerbLowerCase().toUpperCase(); + const reqTag = `${verbUpper} ${this.getPath()}`; + this.log.debug(reqTag); try { res .status(200) .json(await this.opts.connector.transactDelegatedSign(req.body)); - } catch (error) { - this.log.error(`Crash while serving ${fnTag}`, error); - res.status(500).json({ - message: "Internal Server Error", - error: safeStringifyException(error), - }); + } catch (ex) { + const errorMsg = `${fnTag} request handler fn crashed for: ${reqTag}`; + await handleRestEndpointException({ errorMsg, log: this.log, error: ex, res }); } } } diff --git a/packages/cactus-plugin-ledger-connector-fabric/src/main/typescript/run-transaction/run-transaction-endpoint-v1.ts b/packages/cactus-plugin-ledger-connector-fabric/src/main/typescript/run-transaction/run-transaction-endpoint-v1.ts index 38173ef24db..b3b2dba8cab 100644 --- a/packages/cactus-plugin-ledger-connector-fabric/src/main/typescript/run-transaction/run-transaction-endpoint-v1.ts +++ b/packages/cactus-plugin-ledger-connector-fabric/src/main/typescript/run-transaction/run-transaction-endpoint-v1.ts @@ -6,7 +6,6 @@ import { LogLevelDesc, Checks, IAsyncProvider, - safeStringifyException, } from "@hyperledger/cactus-common"; import { @@ -15,7 +14,10 @@ import { IEndpointAuthzOptions, } from "@hyperledger/cactus-core-api"; -import { registerWebServiceEndpoint } from "@hyperledger/cactus-core"; +import { + handleRestEndpointException, + registerWebServiceEndpoint, +} from "@hyperledger/cactus-core"; import { PluginLedgerConnectorFabric } from "../plugin-ledger-connector-fabric"; import OAS from "../../json/openapi.json"; @@ -26,10 +28,16 @@ export interface IRunTransactionEndpointV1Options { } export class RunTransactionEndpointV1 implements IWebServiceEndpoint { + public static readonly CLASS_NAME = "RunTransactionEndpointV1"; + private readonly log: Logger; + public get className(): string { + return RunTransactionEndpointV1.CLASS_NAME; + } + constructor(public readonly opts: IRunTransactionEndpointV1Options) { - const fnTag = "RunTransactionEndpointV1#constructor()"; + const fnTag = `${this.className}#constructor()`; Checks.truthy(opts, `${fnTag} options`); Checks.truthy(opts.connector, `${fnTag} options.connector`); @@ -80,17 +88,16 @@ export class RunTransactionEndpointV1 implements IWebServiceEndpoint { } async handleRequest(req: Request, res: Response): Promise { - const fnTag = "RunTransactionEndpointV1#handleRequest()"; - this.log.debug(`POST ${this.getPath()}`); + const fnTag = `${this.className}#handleRequest()`; + const verbUpper = this.getVerbLowerCase().toUpperCase(); + const reqTag = `${verbUpper} ${this.getPath()}`; + this.log.debug(reqTag); try { res.status(200).json(await this.opts.connector.transact(req.body)); - } catch (error) { - this.log.error(`Crash while serving ${fnTag}`, error); - res.status(500).json({ - message: "Internal Server Error", - error: safeStringifyException(error), - }); + } catch (ex) { + const errorMsg = `${fnTag} request handler fn crashed for: ${reqTag}`; + await handleRestEndpointException({ errorMsg, log: this.log, error: ex, res }); } } }