Skip to content

Commit

Permalink
prettier and endpoint catch handling fix
Browse files Browse the repository at this point in the history
  • Loading branch information
rwat17 committed Jun 26, 2024
1 parent c720990 commit 0be22af
Show file tree
Hide file tree
Showing 143 changed files with 1,129 additions and 768 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,13 @@ const log = LoggerProvider.getOrCreate({
level: logLevel,
});

interface ResponseError<T> extends AxiosError {
interface ResponseError extends AxiosError {
response: {
data: T;
data: any;
status: number;
statusText: string;
headers: any
config: any
headers: any;
config: any;
request?: any;
};
}
Expand Down Expand Up @@ -194,7 +194,7 @@ test.skip(testCase, async (t: Test) => {
await apiClientBad.enrollAdminV1({ orgName: "does-not-matter" });
t.fail("enroll admin response status === 403 FAIL");
} catch (err) {
const e = err as ResponseError<any>
const e = err as ResponseError;

t.ok(e, "error thrown for forbidden endpoint truthy OK");
t.ok(e.response, "enroll admin response truthy OK");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,12 @@ async function menuLoop(agent: AnoncredAgent) {
break;
}
} catch (error) {
if (error && typeof error === 'object' && 'isTtyError' in error && error.isTtyError) {
if (
error &&
typeof error === "object" &&
"isTtyError" in error &&
error.isTtyError
) {
log.error("Prompt couldn't be rendered in the current environment:");
isRunning = false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ import {
LogLevelDesc,
LoggerProvider,
IAsyncProvider,
safeStringifyException,
} from "@hyperledger/cactus-common";
import {
PluginRegistry,
handleRestEndpointException,
registerWebServiceEndpoint,
} from "@hyperledger/cactus-core";
import { CounterpartyHTLCRequest } from "../generated/openapi/typescript-axios";
Expand Down Expand Up @@ -109,10 +109,8 @@ export class CounterpartyHTLCEndpoint implements IWebServiceEndpoint {
res.json(resBody);
} catch (ex) {
this.log.error(`Crash while serving ${reqTag}`, ex);
res.status(500).json({
message: "Internal Server Error",
error: ex,
});
const errorMsg = "Internal Server Error";
handleRestEndpointException({ errorMsg, log: this.log, error: ex, res });
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
import {
registerWebServiceEndpoint,
PluginRegistry,
handleRestEndpointException,
} from "@hyperledger/cactus-core";
import { PluginHTLCCoordinatorBesu } from "../plugin-htlc-coordinator-besu";
import { OwnHTLCRequest } from "../generated/openapi/typescript-axios";
Expand Down Expand Up @@ -103,10 +104,8 @@ export class OwnHTLCEndpoint implements IWebServiceEndpoint {
res.json(resBody);
} catch (ex) {
this.log.error(`Crash while serving ${reqTag}`, ex);
res.status(500).json({
message: "Internal Server Error",
error: ex,
});
const errorMsg = "Internal Server Error";
handleRestEndpointException({ errorMsg, log: this.log, error: ex, res });
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ import {
IWebServiceEndpoint,
} from "@hyperledger/cactus-core-api";

import { registerWebServiceEndpoint } from "@hyperledger/cactus-core";
import {
handleRestEndpointException,
registerWebServiceEndpoint,
} from "@hyperledger/cactus-core";

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

Expand Down Expand Up @@ -93,10 +96,8 @@ export class GetObjectEndpointV1 implements IWebServiceEndpoint {
res.json(resBody);
} catch (ex) {
this.log.error(`${tag} Failed to serve request:`, ex);
res.status(500).json({
message: "Internal Server Error",
error: ex,
});
const errorMsg = "Internal Server Error";
handleRestEndpointException({ errorMsg, log: this.log, error: ex, res });
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ import {
IWebServiceEndpoint,
} from "@hyperledger/cactus-core-api";

import { registerWebServiceEndpoint } from "@hyperledger/cactus-core";
import {
handleRestEndpointException,
registerWebServiceEndpoint,
} from "@hyperledger/cactus-core";

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

Expand Down Expand Up @@ -93,10 +96,8 @@ export class HasObjectEndpointV1 implements IWebServiceEndpoint {
res.json(resBody);
} catch (ex) {
this.log.error(`${tag} Failed to serve request:`, ex);
res.status(500).json({
message: "Internal Server Error",
error: ex,
});
const errorMsg = "Internal Server Error";
handleRestEndpointException({ errorMsg, log: this.log, error: ex, res });
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ import {
IWebServiceEndpoint,
} from "@hyperledger/cactus-core-api";

import { registerWebServiceEndpoint } from "@hyperledger/cactus-core";
import {
handleRestEndpointException,
registerWebServiceEndpoint,
} from "@hyperledger/cactus-core";

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

Expand Down Expand Up @@ -93,10 +96,8 @@ export class SetObjectEndpointV1 implements IWebServiceEndpoint {
res.json(resBody);
} catch (ex) {
this.log.error(`${tag} Failed to serve request:`, ex);
res.status(500).json({
message: "Internal Server Error",
error: ex,
});
const errorMsg = "Internal Server Error";
handleRestEndpointException({ errorMsg, log: this.log, error: ex, res });
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { Express, Request, Response } from "express";

import { registerWebServiceEndpoint } from "@hyperledger/cactus-core";
import {
handleRestEndpointException,
registerWebServiceEndpoint,
} from "@hyperledger/cactus-core";

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

Expand All @@ -26,8 +29,7 @@ export interface IGetPrometheusExporterMetricsEndpointV1Options {
}

export class GetPrometheusExporterMetricsEndpointV1
implements IWebServiceEndpoint
{
implements IWebServiceEndpoint {
private readonly log: Logger;

constructor(
Expand Down Expand Up @@ -94,9 +96,8 @@ export class GetPrometheusExporterMetricsEndpointV1
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 = `Internal server Error`;
handleRestEndpointException({ errorMsg, log: this.log, error: ex, res });
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,23 @@ test("Reports failures with meaningful information", async (t: Test) => {
await provider.get();
t2.fail("Provider.get() did not throw despite API errors.");
} catch (ex) {
if (typeof ex === 'object' && ex !== null) {
if ('message' in ex && typeof ex.message === 'string') {
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 { t2.ok(ex, "Thrown error truthy OK"); }
if (
typeof ex === "object" &&
ex !== null &&
"message" in ex &&
typeof ex.message === "string"
) {
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 {
t2.ok(ex, "Thrown error truthy OK");
}
}
t2.end();
});
Expand All @@ -61,9 +66,8 @@ test("Reports failures with meaningful information", async (t: Test) => {
await provider.get();
t2.fail("Provider.get() did not throw despite API errors.");
} catch (ex) {

if (typeof ex === 'object' && ex !== null) {
if ('message' in ex && typeof ex.message === 'string') {
if (typeof ex === "object" && ex !== null) {
if ("message" in ex && typeof ex.message === "string") {
t2.ok(ex, "Thrown error truthy OK");
t2.ok(ex.message, "Thrown error.message truthy OK");
t2.equal(
Expand All @@ -76,8 +80,9 @@ test("Reports failures with meaningful information", async (t: Test) => {
"Has Status Code in msg OK",
);
}
} else { t2.ok(ex, "Thrown error truthy OK"); }

} else {
t2.ok(ex, "Thrown error truthy OK");
}
}
t2.end();
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { Logger, Checks } from "@hyperledger/cactus-common";
import { LogLevelDesc, LoggerProvider } from "@hyperledger/cactus-common";
import { HealthCheckResponse } from "../generated/openapi/typescript-axios";
import { WatchHealthcheckV1 } from "../generated/openapi/typescript-axios";
import { response } from "express";

export interface IWatchHealthcheckV1EndpointOptions {
logLevel?: LogLevelDesc;
Expand Down Expand Up @@ -54,7 +53,7 @@ export class WatchHealthcheckV1Endpoint {
socket.emit(WatchHealthcheckV1.Next, next);
} catch (ex) {
log.error(`Failed to construct health check response:`, ex);
let responseError = ex as HealthCheckResponse
const responseError = ex as HealthCheckResponse;
socket.emit(WatchHealthcheckV1.Error, responseError);
clearInterval(timerId);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@ import {
LogLevelDesc,
LoggerProvider,
IAsyncProvider,
safeStringifyException,
} from "@hyperledger/cactus-common";

import { registerWebServiceEndpoint } from "@hyperledger/cactus-core";
import {
handleRestEndpointException,
registerWebServiceEndpoint,
} from "@hyperledger/cactus-core";

import { PluginLedgerConnectorStub } from "../plugin-ledger-connector-stub";

Expand Down Expand Up @@ -102,10 +104,8 @@ export class DeployContractEndpoint implements IWebServiceEndpoint {
res.json(resBody);
} catch (ex) {
this.log.error(`Crash while serving ${reqTag}`, ex);
res.status(500).json({
message: "Internal Server Error",
error: safeStringifyException(ex),
});
const errorMsg = "Internal Server Error";
handleRestEndpointException({ errorMsg, log: this.log, error: ex, res });
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,16 @@ import {
LogLevelDesc,
LoggerProvider,
IAsyncProvider,
safeStringifyException,
} from "@hyperledger/cactus-common";
import {
IEndpointAuthzOptions,
IExpressRequestHandler,
IWebServiceEndpoint,
} from "@hyperledger/cactus-core-api";
import { registerWebServiceEndpoint } from "@hyperledger/cactus-core";
import {
handleRestEndpointException,
registerWebServiceEndpoint,
} from "@hyperledger/cactus-core";

import { PluginLedgerConnectorStub } from "../plugin-ledger-connector-stub";

Expand Down Expand Up @@ -100,10 +102,8 @@ export class RunTransactionEndpoint implements IWebServiceEndpoint {
res.json({ success: true, data: resBody });
} catch (ex) {
this.log.error(`Crash while serving ${reqTag}`, ex);
res.status(500).json({
message: "Internal Server Error",
error: safeStringifyException(ex),
});
const errorMsg = "Internal Server Error";
handleRestEndpointException({ errorMsg, log: this.log, error: ex, res });
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,16 @@ import {
LogLevelDesc,
LoggerProvider,
IAsyncProvider,
safeStringifyException,
} from "@hyperledger/cactus-common";
import {
IEndpointAuthzOptions,
IExpressRequestHandler,
IWebServiceEndpoint,
} from "@hyperledger/cactus-core-api";
import { registerWebServiceEndpoint } from "@hyperledger/cactus-core";
import {
handleRestEndpointException,
registerWebServiceEndpoint,
} from "@hyperledger/cactus-core";

import { PluginLedgerConnectorStub } from "../plugin-ledger-connector-stub";

Expand Down Expand Up @@ -106,10 +108,8 @@ export class UnprotectedActionEndpoint implements IWebServiceEndpoint {
res.json({ success: true, data });
} catch (ex) {
this.log.error(`Crash while serving ${reqTag}`, ex);
res.status(500).json({
message: "Internal Server Error",
error: safeStringifyException(ex),
});
const errorMsg = "Internal Server Error";
handleRestEndpointException({ errorMsg, log: this.log, error: ex, res });
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,9 @@ router.post("/", (req: Request, res: Response, next: NextFunction) => {
res.status(201);
res.send(respData);
} catch (err) {

if (typeof err === 'object' && err !== null) { logger.error(`##err name: ${err.constructor.name}`); }



if (typeof err === "object" && err !== null) {
logger.error(`##err name: ${err.constructor.name}`);
}

if (err instanceof RIFError) {
logger.debug(`##catch RIFError, ${err.statusCode}, ${err.message}`);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,7 @@ export class GetConsortiumEndpointV1 implements IWebServiceEndpoint {
} catch (ex) {
this.log.error(`${fnTag} failed to serve request`, ex);
const errorMsg = `${fnTag} request handler fn crashed for: ${reqTag}`;
handleRestEndpointException({ errorMsg, log: this.log, error: ex, res })


handleRestEndpointException({ errorMsg, log: this.log, error: ex, res });
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ export class GetNodeJwsEndpoint implements IWebServiceEndpoint {
} catch (ex) {
this.log.error(`${fnTag} failed to serve request`, ex);
const errorMsg = `${fnTag} request handler fn crashed for: ${reqTag}`;
handleRestEndpointException({ errorMsg, log: this.log, error: ex, res })
handleRestEndpointException({ errorMsg, log: this.log, error: ex, res });
}
}
}
Loading

0 comments on commit 0be22af

Please sign in to comment.