Skip to content

Commit

Permalink
refactor(plugin-object-store-ipfs): correct way to refactor catch and…
Browse files Browse the repository at this point in the history
… else
  • Loading branch information
Leeyoungone committed Sep 30, 2021
1 parent d619bef commit e390bc4
Show file tree
Hide file tree
Showing 11 changed files with 501 additions and 457 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import expressJwt from "express-jwt";
import { v4 as uuidv4 } from "uuid";
import { JWK, JWT } from "jose";
import { StatusCodes } from "http-status-codes";
import axios from "axios";

import {
AuthorizationProtocol,
Expand All @@ -30,6 +31,7 @@ import {
CarbonAccountingApp,
ICarbonAccountingAppOptions,
} from "../../../main/typescript/carbon-accounting-app";
import { RuntimeError } from "run-time-error";

const testCase = "can enroll new admin users onto the Fabric org";
const logLevel: LogLevelDesc = "TRACE";
Expand Down Expand Up @@ -103,9 +105,11 @@ test(testCase, async (t: Test) => {

try {
await carbonAccountingApp.start();
} catch (ex) {
log.error(`CarbonAccountingApp crashed. failing test...`, ex);
throw ex;
} catch (ex: unknown) {
if (axios.isAxiosError(ex)) {
log.error(`CarbonAccountingApp crashed. failing test...`, ex);
throw ex;
}
}

const jwtPayload = {
Expand Down Expand Up @@ -155,19 +159,22 @@ test(testCase, async (t: Test) => {
try {
await apiClientBad.enrollAdminV1({ orgName: "does-not-matter" });
t.fail("enroll admin response status === 403 FAIL");
} catch (out) {
t.ok(out, "error thrown for forbidden endpoint truthy OK");
t.ok((out as any).response, "enroll admin response truthy OK");
t.equal(
(out as any).response.status,
StatusCodes.FORBIDDEN,
"enroll admin response status === 403 OK",
);
t.notok((out as any).response.data.data, "out.response.data.data falsy OK");
t.notok(
(out as any).response.data.success,
"out.response.data.success falsy OK",
);
} catch (out: unknown) {
if (!out) {
const errorMessage = `out is falsy`;
throw new RuntimeError(errorMessage);
}
if (axios.isAxiosError(out)) {
t.ok(out, "error thrown for forbidden endpoint truthy OK");
t.ok(out.response, "enroll admin response truthy OK");
t.equal(
out.response?.status,
StatusCodes.FORBIDDEN,
"enroll admin response status === 403 OK",
);
t.notok(out.response?.data.data, "out.response.data.data falsy OK");
t.notok(out.response?.data.success, "out.response.data.success falsy OK");
}
}

t.end();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { registerWebServiceEndpoint } from "@hyperledger/cactus-core";
import { DaoTokenGetAllowanceRequest } from "../../../generated/openapi/typescript-axios";
import { CarbonAccountingPlugin } from "../../carbon-accounting-plugin";
import OAS from "../../../../json/openapi.json";
import axios from "axios";

export interface IGetAllowanceEndpointOptions {
logLevel?: LogLevelDesc;
Expand Down Expand Up @@ -82,9 +83,11 @@ export class GetAllowanceEndpoint implements IWebServiceEndpoint {
res.status(200);
res.json(resBody);
} catch (ex) {
this.log.debug(`${tag} Failed to serve request:`, ex);
res.status(500);
res.json({ error: (ex as Error).stack });
if (axios.isAxiosError(ex)) {
this.log.debug(`${tag} Failed to serve request:`, ex);
res.status(500);
res.json({ error: ex.stack });
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { registerWebServiceEndpoint } from "@hyperledger/cactus-core";
import { EnrollAdminV1Request } from "../../../generated/openapi/typescript-axios";
import { CarbonAccountingPlugin } from "../../carbon-accounting-plugin";
import OAS from "../../../../json/openapi.json";
import axios from "axios";

export interface IEnrollAdminV1EndpointOptions {
logLevel?: LogLevelDesc;
Expand Down Expand Up @@ -76,9 +77,11 @@ export class EnrollAdminV1Endpoint implements IWebServiceEndpoint {
res.status(200);
res.json(resBody);
} catch (ex) {
this.log.debug(`${tag} Failed to serve request:`, ex);
res.status(500);
res.json({ error: (ex as Error).stack });
if (axios.isAxiosError(ex)) {
this.log.debug(`${tag} Failed to serve request:`, ex);
res.status(500);
res.json({ error: ex.stack });
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {

import OAS from "../../../json/openapi.json";
import { InsertBambooHarvestRequest } from "../../generated/openapi/typescript-axios";
import axios from "axios";

export interface IInsertBambooHarvestEndpointOptions {
logLevel?: LogLevelDesc;
Expand Down Expand Up @@ -128,9 +129,11 @@ export class InsertBambooHarvestEndpoint implements IWebServiceEndpoint {
res.status(200);
res.json(body);
} catch (ex) {
this.log.debug(`${tag} Failed to serve request:`, ex);
res.status(500);
res.json({ error: (ex as Error).stack });
if (axios.isAxiosError(ex)) {
this.log.debug(`${tag} Failed to serve request:`, ex);
res.status(500);
res.json({ error: (ex as Error).stack });
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {

import OAS from "../../../json/openapi.json";
import { InsertBookshelfRequest } from "../../generated/openapi/typescript-axios/index";
import axios from "axios";

export interface IInsertBookshelfEndpointOptions {
logLevel?: LogLevelDesc;
Expand Down Expand Up @@ -115,9 +116,11 @@ export class InsertBookshelfEndpoint implements IWebServiceEndpoint {
res.status(200);
res.json(body);
} catch (ex) {
this.log.debug(`${tag} Failed to serve request:`, ex);
res.status(500);
res.json({ error: (ex as Error).stack });
if (axios.isAxiosError(ex)) {
this.log.debug(`${tag} Failed to serve request:`, ex);
res.status(500);
res.json({ error: ex.stack });
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
} from "@hyperledger/cactus-plugin-ledger-connector-fabric";

import OAS from "../../../json/openapi.json";
import axios from "axios";

export interface IInsertShipmentEndpointOptions {
logLevel?: LogLevelDesc;
Expand Down Expand Up @@ -115,9 +116,11 @@ export class InsertShipmentEndpoint implements IWebServiceEndpoint {
res.json(body);
res.status(200);
} catch (ex) {
this.log.debug(`${tag} Failed to serve request:`, ex);
res.status(500);
res.json({ error: (ex as Error).stack });
if (axios.isAxiosError(ex)) {
this.log.debug(`${tag} Failed to serve request:`, ex);
res.status(500);
res.json({ error: (ex as Error).stack });
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import {

import OAS from "../../../json/openapi.json";
import { BambooHarvestConverter } from "../../model/converter/bamboo-harvest-converter";

import axios from "axios";
export interface IListBambooHarvestEndpointOptions {
logLevel?: LogLevelDesc;
contractName: string;
Expand Down Expand Up @@ -117,9 +117,11 @@ export class ListBambooHarvestEndpoint implements IWebServiceEndpoint {
res.status(200);
res.json(body);
} catch (ex) {
this.log.debug(`${tag} Failed to serve request:`, ex);
res.status(500);
res.json({ error: (ex as Error).stack });
if (axios.isAxiosError(ex)) {
this.log.debug(`${tag} Failed to serve request:`, ex);
res.status(500);
res.json({ error: ex.stack });
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {

import OAS from "../../../json/openapi.json";
import { BookshelfConverter } from "../../model/converter/bookshelf-converter";
import axios from "axios";

export interface IListBookshelfEndpointOptions {
logLevel?: LogLevelDesc;
Expand Down Expand Up @@ -115,9 +116,11 @@ export class ListBookshelfEndpoint implements IWebServiceEndpoint {
res.status(200);
res.json(body);
} catch (ex) {
this.log.debug(`${tag} Failed to serve request:`, ex);
res.status(500);
res.json({ error: (ex as Error).stack });
if (axios.isAxiosError(ex)) {
this.log.debug(`${tag} Failed to serve request:`, ex);
res.status(500);
res.json({ error: ex.stack });
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
} from "@hyperledger/cactus-plugin-ledger-connector-fabric";

import OAS from "../../../json/openapi.json";
import axios from "axios";

export interface IListShipmentEndpointOptions {
readonly logLevel?: LogLevelDesc;
Expand Down Expand Up @@ -113,9 +114,11 @@ export class ListShipmentEndpoint implements IWebServiceEndpoint {
res.status(200);
res.json(body);
} catch (ex) {
this.log.debug(`${tag} Failed to serve request:`, ex);
res.status(500);
res.json({ error: (ex as Error).stack });
if (axios.isAxiosError(ex)) {
this.log.debug(`${tag} Failed to serve request:`, ex);
res.status(500);
res.json({ error: ex.stack });
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import { SetObjectEndpointV1 } from "./web-services/set-object-endpoint-v1";
import { HasObjectEndpointV1 } from "./web-services/has-object-endpoint-v1";
import type { IIpfsHttpClient } from "./i-ipfs-http-client";
import { isIpfsHttpClientOptions } from "./i-ipfs-http-client";
import axios from "axios";

export const K_IPFS_JS_HTTP_ERROR_FILE_DOES_NOT_EXIST =
"HTTPError: file does not exist";
Expand Down Expand Up @@ -155,17 +156,24 @@ export class PluginObjectStoreIpfs implements IPluginObjectStore {
const statResult = await this.ipfs.files.stat(keyPath);
this.log.debug(`StatResult for ${req.key}: %o`, statResult);
return { key: req.key, checkedAt, isPresent: true };
} catch (ex) {
if (
(ex as Error)?.stack?.includes(K_IPFS_JS_HTTP_ERROR_FILE_DOES_NOT_EXIST)
) {
const msg = `Stat ${req.key} failed with error message containing phrase "${K_IPFS_JS_HTTP_ERROR_FILE_DOES_NOT_EXIST}" Returning isPresent=false ...`;
this.log.debug(msg);
return { key: req.key, checkedAt, isPresent: false };
} catch (ex: unknown) {
if (axios.isAxiosError(ex)) {
if (ex.stack?.includes(K_IPFS_JS_HTTP_ERROR_FILE_DOES_NOT_EXIST)) {
const msg = `Stat ${req.key} failed with error message containing phrase "${K_IPFS_JS_HTTP_ERROR_FILE_DOES_NOT_EXIST}" Returning isPresent=false ...`;
this.log.debug(msg);
return { key: req.key, checkedAt, isPresent: false };
} else {
throw new RuntimeError(
`Checking presence of ${req.key} crashed:`,
ex,
);
}
} else if (ex instanceof Error) {
throw new RuntimeError(`unexpected exception`, ex);
} else {
throw new RuntimeError(
`Checking presence of ${req.key} crashed:`,
ex as Error,
"unexpected exception with incorrect type ",
JSON.stringify(ex),
);
}
}
Expand All @@ -180,11 +188,18 @@ export class PluginObjectStoreIpfs implements IPluginObjectStore {
create: true,
parents: true,
});
} catch (ex) {
throw new RuntimeError(
`Can't set object ${keyPath}. Write failed:`,
ex as Error,
);
} catch (ex: unknown) {
if (axios.isAxiosError(ex)) {
throw new RuntimeError(
`Can't set object ${keyPath}. Write failed:`,
ex as Error,
);
} else {
throw new RuntimeError(
"expected exception with incorrect type",
JSON.stringify(ex),
);
}
}
return {
key: req.key,
Expand Down
Loading

0 comments on commit e390bc4

Please sign in to comment.