Skip to content

Commit

Permalink
refactor(error): adding else if and else statment to if
Browse files Browse the repository at this point in the history
  • Loading branch information
Leeyoungone committed Oct 7, 2021
1 parent e390bc4 commit df884df
Show file tree
Hide file tree
Showing 57 changed files with 1,115 additions and 394 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,13 @@ test(testCase, async (t: Test) => {
if (axios.isAxiosError(ex)) {
log.error(`CarbonAccountingApp crashed. failing test...`, ex);
throw ex;
} else if (ex instanceof Error) {
throw new RuntimeError("unexpected exception", ex);
} else {
throw new RuntimeError(
"unexpected expection with incorrect type",
JSON.stringify(ex),
);
}
}

Expand Down Expand Up @@ -174,6 +181,13 @@ test(testCase, async (t: Test) => {
);
t.notok(out.response?.data.data, "out.response.data.data falsy OK");
t.notok(out.response?.data.success, "out.response.data.success falsy OK");
} else if (out instanceof Error) {
throw new RuntimeError("unexpected exception", out);
} else {
throw new RuntimeError(
"unexpected exception with incorrect type",
JSON.stringify(out),
);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { DaoTokenGetAllowanceRequest } from "../../../generated/openapi/typescri
import { CarbonAccountingPlugin } from "../../carbon-accounting-plugin";
import OAS from "../../../../json/openapi.json";
import axios from "axios";
import { RuntimeError } from "run-time-error";

export interface IGetAllowanceEndpointOptions {
logLevel?: LogLevelDesc;
Expand Down Expand Up @@ -87,6 +88,13 @@ export class GetAllowanceEndpoint implements IWebServiceEndpoint {
this.log.debug(`${tag} Failed to serve request:`, ex);
res.status(500);
res.json({ error: ex.stack });
} else if (ex instanceof Error) {
throw new RuntimeError("unexpected exception", ex);
} else {
throw new RuntimeError(
"unexpected exception with incorrect type",
JSON.stringify(ex),
);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { EnrollAdminV1Request } from "../../../generated/openapi/typescript-axio
import { CarbonAccountingPlugin } from "../../carbon-accounting-plugin";
import OAS from "../../../../json/openapi.json";
import axios from "axios";
import { RuntimeError } from "run-time-error";

export interface IEnrollAdminV1EndpointOptions {
logLevel?: LogLevelDesc;
Expand Down Expand Up @@ -81,6 +82,13 @@ export class EnrollAdminV1Endpoint implements IWebServiceEndpoint {
this.log.debug(`${tag} Failed to serve request:`, ex);
res.status(500);
res.json({ error: ex.stack });
} else if (ex instanceof Error) {
throw new RuntimeError("unexpected exception", ex);
} else {
throw new RuntimeError(
"unexpected exception with incorrect type",
JSON.stringify(ex),
);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import {
import OAS from "../../../json/openapi.json";
import { InsertBambooHarvestRequest } from "../../generated/openapi/typescript-axios";
import axios from "axios";
import { RuntimeError } from "run-time-error";

export interface IInsertBambooHarvestEndpointOptions {
logLevel?: LogLevelDesc;
Expand Down Expand Up @@ -132,7 +133,14 @@ export class InsertBambooHarvestEndpoint implements IWebServiceEndpoint {
if (axios.isAxiosError(ex)) {
this.log.debug(`${tag} Failed to serve request:`, ex);
res.status(500);
res.json({ error: (ex as Error).stack });
res.json({ error: ex.stack });
} else if (ex instanceof Error) {
throw new RuntimeError("unexpected exception", ex);
} else {
throw new RuntimeError(
"unexpected exception with incorrect type",
JSON.stringify(ex),
);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
import OAS from "../../../json/openapi.json";
import { InsertBookshelfRequest } from "../../generated/openapi/typescript-axios/index";
import axios from "axios";
import { RuntimeError } from "run-time-error";

export interface IInsertBookshelfEndpointOptions {
logLevel?: LogLevelDesc;
Expand Down Expand Up @@ -120,6 +121,13 @@ export class InsertBookshelfEndpoint implements IWebServiceEndpoint {
this.log.debug(`${tag} Failed to serve request:`, ex);
res.status(500);
res.json({ error: ex.stack });
} else if (ex instanceof Error) {
throw new RuntimeError("unexpected exception", ex);
} else {
throw new RuntimeError(
"unexpected exception with incorrect type",
JSON.stringify(ex),
);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {

import OAS from "../../../json/openapi.json";
import axios from "axios";
import { RuntimeError } from "run-time-error";

export interface IInsertShipmentEndpointOptions {
logLevel?: LogLevelDesc;
Expand Down Expand Up @@ -120,6 +121,13 @@ export class InsertShipmentEndpoint implements IWebServiceEndpoint {
this.log.debug(`${tag} Failed to serve request:`, ex);
res.status(500);
res.json({ error: (ex as Error).stack });
} else if (ex instanceof Error) {
throw new RuntimeError("unexpected exception", ex);
} else {
throw new RuntimeError(
"unexpected exception with incorrect type",
JSON.stringify(ex),
);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import {
import { registerWebServiceEndpoint } from "@hyperledger/cactus-core";

import OAS from "../../json/openapi.json";
import axios from "axios";
import { RuntimeError } from "run-time-error";

export interface IGetObjectEndpointV1Options {
readonly logLevel?: LogLevelDesc;
Expand Down Expand Up @@ -92,9 +94,15 @@ export class GetObjectEndpointV1 implements IWebServiceEndpoint {
res.status(200);
res.json(resBody);
} catch (ex) {
this.log.error(`${tag} Failed to serve request:`, ex);
res.status(500);
res.json({ error: (ex as Error).stack });
if (axios.isAxiosError(ex)) {
this.log.error(`${tag} Failed to serve request:`, ex);
res.status(500);
res.json({ error: ex.stack });
} else if (ex instanceof Error) {
throw new RuntimeError(" unexpected expection", ex);
} else {
throw new RuntimeError("unexpected expection", JSON.stringify(ex));
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import {
import { registerWebServiceEndpoint } from "@hyperledger/cactus-core";

import OAS from "../../json/openapi.json";
import axios from "axios";
import { RuntimeError } from "run-time-error";

export interface IHasObjectEndpointV1Options {
readonly logLevel?: LogLevelDesc;
Expand Down Expand Up @@ -91,10 +93,19 @@ export class HasObjectEndpointV1 implements IWebServiceEndpoint {
const resBody = await this.plugin.has(reqBody);
res.status(200);
res.json(resBody);
} catch (ex) {
this.log.error(`${tag} Failed to serve request:`, ex);
res.status(500);
res.json({ error: (ex as Error).stack });
} catch (ex: unknown) {
if (axios.isAxiosError(ex)) {
this.log.error(`${tag} Failed to serve request:`, ex);
res.status(500);
res.json({ error: ex.stack });
} else if (ex instanceof Error) {
throw new RuntimeError("unexpected exception", ex);
} else {
throw new RuntimeError(
"unexpected exception with incorrect type",
JSON.stringify(ex),
);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import {
import { registerWebServiceEndpoint } from "@hyperledger/cactus-core";

import OAS from "../../json/openapi.json";
import axios from "axios";
import { RuntimeError } from "run-time-error";

export interface ISetObjectEndpointV1Options {
readonly logLevel?: LogLevelDesc;
Expand Down Expand Up @@ -92,9 +94,18 @@ export class SetObjectEndpointV1 implements IWebServiceEndpoint {
res.status(200);
res.json(resBody);
} catch (ex) {
this.log.error(`${tag} Failed to serve request:`, ex);
res.status(500);
res.json({ error: (ex as Error).stack });
if (axios.isAxiosError(ex)) {
this.log.error(`${tag} Failed to serve request:`, ex);
res.status(500);
res.json({ error: (ex as Error).stack });
} else if (ex instanceof Error) {
throw new RuntimeError("unexpected exception", ex);
} else {
throw new RuntimeError(
"unexpected exception with incorrect type",
JSON.stringify(ex),
);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ import {
GetConsortiumJwsResponse,
} from "@hyperledger/cactus-plugin-consortium-manual";

import axios from "axios";
import { RuntimeError } from "run-time-error";

export interface IDefaultConsortiumProviderOptions {
logLevel?: LogLevelDesc;
apiClient: ConsortiumManualApi;
Expand Down Expand Up @@ -64,9 +67,18 @@ export class DefaultConsortiumProvider
const res = await this.options.apiClient.getConsortiumJwsV1();
return this.parseConsortiumJws(res.data);
} catch (ex) {
const innerException = ((ex as any).toJSON && (ex as any).toJSON()) || ex;
this.log.error(`Request for Consortium JWS failed: `, innerException);
throw ex;
if (axios.isAxiosError(ex)) {
const innerException = (ex.toJSON && ex.toJSON()) || ex;
this.log.error(`Request for Consortium JWS failed: `, innerException);
throw ex;
} else if (ex instanceof Error) {
throw new RuntimeError("unexpected exception", ex);
} else {
throw new RuntimeError(
"unexpected exception with incorrect type",
JSON.stringify(ex),
);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ import { LogLevelDesc, Servers } from "@hyperledger/cactus-common";
import { DefaultConsortiumProvider } from "../../../main/typescript";
import { Configuration } from "@hyperledger/cactus-core-api";

import axios from "axios";
import { RuntimeError } from "run-time-error";

test("Reports failures with meaningful information", async (t: Test) => {
const logLevel: LogLevelDesc = "TRACE";

Expand All @@ -31,18 +34,24 @@ test("Reports failures with meaningful information", async (t: Test) => {
try {
await provider.get();
t2.fail("Provider.get() did not throw despite API errors.");
} catch (ex) {
t2.ok(ex, "Thrown error truthy OK");
t2.ok((ex as Error).message, "Thrown error.message truthy OK");
t2.equal(
typeof (ex as Error).message,
"string",
"Thrown error.message type string OK",
);
t2.true(
(ex as Error).message.includes("timeout"),
"Has timeout in msg OK",
);
} catch (ex: unknown) {
if (axios.isAxiosError(ex)) {
t2.ok(ex, "Thrown error truthy OK");
t2.ok(ex.message, "Thrown error.message truthy OK");
t2.equal(
typeof ex.message,
"string",
"Thrown error.message type string OK",
);
t2.true(ex.message.includes("timeout"), "Has timeout in msg OK");
} else if (ex instanceof Error) {
throw new RuntimeError("unexpected exception", ex);
} else {
throw new RuntimeError(
"unexpected exception with incorrect type",
JSON.stringify(ex),
);
}
}
t2.end();
});
Expand All @@ -59,18 +68,27 @@ test("Reports failures with meaningful information", async (t: Test) => {
try {
await provider.get();
t2.fail("Provider.get() did not throw despite API errors.");
} catch (ex) {
t2.ok(ex, "Thrown error truthy OK");
t2.ok((ex as Error).message, "Thrown error.message truthy OK");
t2.equal(
typeof (ex as Error).message,
"string",
"Thrown error.message type string OK",
);
t2.true(
(ex as Error).message.includes("status code 404"),
"Has Status Code in msg OK",
);
} catch (ex: unknown) {
if (axios.isAxiosError(ex)) {
t2.ok(ex, "Thrown error truthy OK");
t2.ok(ex.message, "Thrown error.message truthy OK");
t2.equal(
typeof ex.message,
"string",
"Thrown error.message type string OK",
);
t2.true(
ex.message.includes("status code 404"),
"Has Status Code in msg OK",
);
} else if (ex instanceof Error) {
throw new RuntimeError("unexpected exception", ex);
} else {
throw new RuntimeError(
"unexpected exception with incorrect type",
JSON.stringify(ex),
);
}
}
t2.end();
});
Expand Down
Loading

0 comments on commit df884df

Please sign in to comment.