Skip to content

Commit

Permalink
fix: plugin ledger connector fabric exception handling and upgrade TS
Browse files Browse the repository at this point in the history
Depends on hyperledger-cacti#1707
Fixes hyperledger-cacti#1735

Signed-off-by: Youngone Lee <[email protected]>
  • Loading branch information
Leeyoungone committed Jan 14, 2022
1 parent 0707950 commit c4e191c
Show file tree
Hide file tree
Showing 10 changed files with 221 additions and 120 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
Checks,
LogLevelDesc,
LoggerProvider,
LogHelper,
IAsyncProvider,
} from "@hyperledger/cactus-common";

Expand Down Expand Up @@ -95,11 +96,19 @@ export class DeployContractGoSourceEndpointV1 implements IWebServiceEndpoint {
const resBody = await connector.deployContractGoSourceV1(reqBody);
res.status(HttpStatus.OK);
res.json(resBody);
} catch (ex) {
} catch (ex: unknown) {
const stack = LogHelper.getExceptionStack(ex);
const messages = LogHelper.getExceptionMessage(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 });
if (ex instanceof Error) {
res.status(HttpStatus.INTERNAL_SERVER_ERROR);
res.statusMessage = messages;
res.json({ error: stack });
} else {
res.status(HttpStatus.INTERNAL_SERVER_ERROR);
res.statusMessage = messages;
res.json({ error: stack });
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
Checks,
LogLevelDesc,
LoggerProvider,
LogHelper,
IAsyncProvider,
} from "@hyperledger/cactus-common";

Expand Down Expand Up @@ -95,11 +96,19 @@ export class DeployContractEndpointV1 implements IWebServiceEndpoint {
const resBody = await connector.deployContract(reqBody);
res.status(HttpStatus.OK);
res.json(resBody);
} catch (ex) {
} catch (ex: unknown) {
const stack = LogHelper.getExceptionStack(ex);
const messages = LogHelper.getExceptionMessage(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 });
if (ex instanceof Error) {
res.status(HttpStatus.INTERNAL_SERVER_ERROR);
res.statusMessage = messages;
res.json({ error: stack });
} else {
res.status(HttpStatus.INTERNAL_SERVER_ERROR);
res.statusMessage = messages;
res.json({ error: stack });
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
Logger,
LoggerProvider,
LogLevelDesc,
LogHelper,
Checks,
IAsyncProvider,
} from "@hyperledger/cactus-common";
Expand Down Expand Up @@ -90,11 +91,19 @@ export class GetPrometheusExporterMetricsEndpointV1
const resBody = await this.opts.connector.getPrometheusExporterMetrics();
res.status(200);
res.send(resBody);
} catch (ex) {
} catch (ex: unknown) {
const stack = LogHelper.getExceptionStack(ex);
const messages = LogHelper.getExceptionMessage(ex);
this.log.error(`${fnTag} failed to serve request`, ex);
res.status(500);
res.statusMessage = ex.message;
res.json({ error: ex.stack });
if (ex instanceof Error) {
res.status(500);
res.statusMessage = messages;
res.json({ error: stack });
} else {
res.status(500);
res.statusMessage = messages;
res.json({ error: stack });
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
Logger,
LoggerProvider,
LogLevelDesc,
LogHelper,
Checks,
IAsyncProvider,
} from "@hyperledger/cactus-common";
Expand Down Expand Up @@ -93,11 +94,19 @@ export class GetTransactionReceiptByTxIDEndpointV1
);
res.status(200);
res.json(resBody);
} catch (ex) {
} catch (ex: unknown) {
const stack = LogHelper.getExceptionStack(ex);
const messages = LogHelper.getExceptionMessage(ex);
this.log.error(`${fnTag} failed to serve request`, ex);
res.status(500);
res.statusMessage = ex.message;
res.json({ error: ex.stack });
if (ex instanceof Error) {
res.status(500);
res.statusMessage = messages;
res.json({ error: stack });
} else {
res.status(500);
res.statusMessage = messages;
res.json({ error: stack });
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import {
Checks,
LogLevelDesc,
LoggerProvider,
LogHelper,
} from "@hyperledger/cactus-common";

import {
Expand Down Expand Up @@ -108,6 +109,7 @@ import {
getTransactionReceiptByTxID,
IGetTransactionReceiptByTxIDOptions,
} from "./common/get-transaction-receipt-by-tx-id";
import axios from "axios";
/**
* Constant value holding the default $GOPATH in the Fabric CLI container as
* observed on fabric deployments that are produced by the official examples
Expand Down Expand Up @@ -1039,11 +1041,18 @@ export class PluginLedgerConnectorFabric
JSON.stringify(transientMap[key]),
);
}
} catch (ex) {
} catch (ex: unknown) {
const messages = LogHelper.getExceptionMessage(ex);
this.log.error(`Building transient map crashed: `, ex);
throw new Error(
`${fnTag} Unable to build the transient map: ${ex.message}`,
);
if (axios.isAxiosError(ex)) {
throw new Error(
`${fnTag} Unable to build the transient map: ${messages}`,
);
} else {
throw new Error(
`${fnTag} Unable to build the transient map: ${messages}`,
);
}
}

const transactionProposal = await contract.createTransaction(fnName);
Expand Down Expand Up @@ -1074,9 +1083,14 @@ export class PluginLedgerConnectorFabric
this.prometheusExporter.addCurrentTransaction();

return res;
} catch (ex) {
} catch (ex: unknown) {
const messages = LogHelper.getExceptionMessage(ex);
this.log.error(`transact() crashed: `, ex);
throw new Error(`${fnTag} Unable to run transaction: ${ex.message}`);
if (axios.isAxiosError(ex)) {
throw new Error(`${fnTag} Unable to run transaction: ${messages}`);
} else {
throw new Error(`${fnTag} Unable to run transaction: ${messages}`);
}
}
}
public async getTransactionReceiptByTxID(
Expand Down Expand Up @@ -1110,9 +1124,14 @@ export class PluginLedgerConnectorFabric
this.log.debug(`createCaClient() caName=%o caUrl=%o`, caName, caUrl);
this.log.debug(`createCaClient() tlsOptions=%o`, tlsOptions);
return new FabricCAServices(caUrl, tlsOptions, caName);
} catch (ex) {
} catch (ex: unknown) {
const messages = LogHelper.getExceptionMessage(ex);
this.log.error(`createCaClient() Failure:`, ex);
throw new Error(`${fnTag} Inner Exception: ${ex?.message}`);
if (axios.isAxiosError(ex)) {
throw new Error(`${fnTag} Inner Exception: ${messages}`);
} else {
throw new Error(`${fnTag} Inner Exception: ${messages}`);
}
}
}

Expand Down Expand Up @@ -1146,9 +1165,14 @@ export class PluginLedgerConnectorFabric
await wallet.put(identityId, x509Identity);

return [x509Identity, wallet];
} catch (ex) {
} catch (ex: unknown) {
const messages = LogHelper.getExceptionMessage(ex);
this.log.error(`enrollAdmin() Failure:`, ex);
throw new Error(`${fnTag} Exception: ${ex?.message}`);
if (axios.isAxiosError(ex)) {
throw new Error(`${fnTag} Exception: ${messages}`);
} else {
throw new Error(`${fnTag} Exception: ${messages}`);
}
}
}
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
Logger,
LoggerProvider,
LogLevelDesc,
LogHelper,
Checks,
IAsyncProvider,
} from "@hyperledger/cactus-common";
Expand Down Expand Up @@ -88,11 +89,19 @@ export class RunTransactionEndpointV1 implements IWebServiceEndpoint {
const resBody = await this.opts.connector.transact(reqBody);
res.status(200);
res.json(resBody);
} catch (ex) {
} catch (ex: unknown) {
const stack = LogHelper.getExceptionStack(ex);
const messages = LogHelper.getExceptionMessage(ex);
this.log.error(`${fnTag} failed to serve request`, ex);
res.status(500);
res.statusMessage = ex.message;
res.json({ error: ex.stack });
if (ex instanceof Error) {
res.status(500);
res.statusMessage = messages;
res.json({ error: stack });
} else {
res.status(500);
res.statusMessage = messages;
res.json({ error: stack });
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import test, { Test } from "tape-promise/tape";
import { InternalIdentityClient } from "../../../main/typescript/identity/internal/client";
import { VaultTransitClient } from "../../../main/typescript/identity/vault-client";
import { WebSocketClient } from "../../../main/typescript/identity/web-socket-client";
import { LogLevelDesc } from "@hyperledger/cactus-common";
import { LogLevelDesc, LogHelper } from "@hyperledger/cactus-common";
import { createHash } from "crypto";
import { ECCurveType } from "../../../main/typescript/identity/internal/crypto-util";
import { KJUR } from "jsrsasign";
Expand Down Expand Up @@ -267,12 +267,13 @@ test("identity-clients", async (t: Test) => {
try {
await client.getPub(testNotFoundKey);
t.fail("Should not get here");
} catch (error) {
t.true(
(error as Error).message.includes(
`keyName = ${testNotFoundKey} not found`,
),
);
} catch (error: unknown) {
const messages = LogHelper.getExceptionMessage(error);
if (axios.isAxiosError(error)) {
t.true(messages.includes(`keyName = ${testNotFoundKey} not found`));
} else {
t.fail("expected an axios error, got something else");
}
}
}
t.end();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import { Configuration } from "@hyperledger/cactus-core-api";

import { installOpenapiValidationMiddleware } from "@hyperledger/cactus-core";
import OAS from "../../../../main/json/openapi.json";
import axios from "axios";

const testCase = "check openapi validation in fabric endpoints";
const logLevel: LogLevelDesc = "TRACE";
Expand Down Expand Up @@ -228,19 +229,23 @@ test(testCase, async (t: Test) => {
await apiClient.deployContractGoSourceV1(
(parameters as any) as DeployContractGoSourceV1Request,
);
} catch (e) {
t2.equal(
e.response.status,
400,
`Endpoint ${fDeployGo} without required targetPeerAddresses: response.status === 400 OK`,
);
const fields = e.response.data.map((param: any) =>
param.path.replace(".body.", ""),
);
t2.ok(
fields.includes("targetPeerAddresses"),
"Rejected because targetPeerAddresses is required",
);
} catch (e: unknown) {
if (axios.isAxiosError(e)) {
t2.equal(
e.response?.status,
400,
`Endpoint ${fDeployGo} without required targetPeerAddresses: response.status === 400 OK`,
);
const fields = e.response?.data.map((param: any) =>
param.path.replace(".body.", ""),
);
t2.ok(
fields.includes("targetPeerAddresses"),
"Rejected because targetPeerAddresses is required",
);
} else {
t2.fail("expected an axios error, got something else");
}
}

t2.end();
Expand Down Expand Up @@ -290,19 +295,23 @@ test(testCase, async (t: Test) => {
await apiClient.deployContractGoSourceV1(
(parameters as any) as DeployContractGoSourceV1Request,
);
} catch (e) {
t2.equal(
e.response.status,
400,
`Endpoint ${fDeployGo} with fake=4: response.status === 400 OK`,
);
const fields = e.response.data.map((param: any) =>
param.path.replace(".body.", ""),
);
t2.ok(
fields.includes("fake"),
"Rejected because fake is not a valid parameter",
);
} catch (e: unknown) {
if (axios.isAxiosError(e)) {
t2.equal(
e.response?.status,
400,
`Endpoint ${fDeployGo} with fake=4: response.status === 400 OK`,
);
const fields = e.response?.data.map((param: any) =>
param.path.replace(".body.", ""),
);
t2.ok(
fields.includes("fake"),
"Rejected because fake is not a valid parameter",
);
} else {
t2.fail("expected an axios error, got something else");
}
}

t2.end();
Expand Down
Loading

0 comments on commit c4e191c

Please sign in to comment.