Skip to content

Commit

Permalink
refactor: typecast to adapt to typescript v4.4
Browse files Browse the repository at this point in the history
Signed-off-by: Youngone Lee <youngone.lee@accenture.com>
  • Loading branch information
Leeyoungone committed Sep 28, 2021
1 parent 6945c80 commit 26f15e1
Show file tree
Hide file tree
Showing 99 changed files with 269 additions and 216 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -157,14 +157,17 @@ test(testCase, async (t: Test) => {
t.fail("enroll admin response status === 403 FAIL");
} catch (out) {
t.ok(out, "error thrown for forbidden endpoint truthy OK");
t.ok(out.response, "enroll admin response truthy OK");
t.ok((out as any).response, "enroll admin response truthy OK");
t.equal(
out.response.status,
(out as any).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.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",
);
}

t.end();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ export class GetAllowanceEndpoint implements IWebServiceEndpoint {
} catch (ex) {
this.log.debug(`${tag} Failed to serve request:`, ex);
res.status(500);
res.json({ error: ex.stack });
res.json({ error: (ex as Error).stack });
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ export class EnrollAdminV1Endpoint implements IWebServiceEndpoint {
} catch (ex) {
this.log.debug(`${tag} Failed to serve request:`, ex);
res.status(500);
res.json({ error: ex.stack });
res.json({ error: (ex as Error).stack });
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ export class InsertBambooHarvestEndpoint implements IWebServiceEndpoint {
} catch (ex) {
this.log.debug(`${tag} Failed to serve request:`, ex);
res.status(500);
res.json({ error: ex.stack });
res.json({ error: (ex as Error).stack });
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ export class InsertBookshelfEndpoint implements IWebServiceEndpoint {
} catch (ex) {
this.log.debug(`${tag} Failed to serve request:`, ex);
res.status(500);
res.json({ error: ex.stack });
res.json({ error: (ex as Error).stack });
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ export class InsertShipmentEndpoint implements IWebServiceEndpoint {
} catch (ex) {
this.log.debug(`${tag} Failed to serve request:`, ex);
res.status(500);
res.json({ error: ex.stack });
res.json({ error: (ex as Error).stack });
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ export class ListBambooHarvestEndpoint implements IWebServiceEndpoint {
} catch (ex) {
this.log.debug(`${tag} Failed to serve request:`, ex);
res.status(500);
res.json({ error: ex.stack });
res.json({ error: (ex as Error).stack });
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ export class ListBookshelfEndpoint implements IWebServiceEndpoint {
} catch (ex) {
this.log.debug(`${tag} Failed to serve request:`, ex);
res.status(500);
res.json({ error: ex.stack });
res.json({ error: (ex as Error).stack });
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ export class ListShipmentEndpoint implements IWebServiceEndpoint {
} catch (ex) {
this.log.debug(`${tag} Failed to serve request:`, ex);
res.status(500);
res.json({ error: ex.stack });
res.json({ error: (ex as Error).stack });
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -156,12 +156,17 @@ export class PluginObjectStoreIpfs implements IPluginObjectStore {
this.log.debug(`StatResult for ${req.key}: %o`, statResult);
return { key: req.key, checkedAt, isPresent: true };
} catch (ex) {
if (ex?.stack?.includes(K_IPFS_JS_HTTP_ERROR_FILE_DOES_NOT_EXIST)) {
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 };
} else {
throw new RuntimeError(`Checking presence of ${req.key} crashed:`, ex);
throw new RuntimeError(
`Checking presence of ${req.key} crashed:`,
ex as Error,
);
}
}
}
Expand All @@ -176,7 +181,10 @@ export class PluginObjectStoreIpfs implements IPluginObjectStore {
parents: true,
});
} catch (ex) {
throw new RuntimeError(`Can't set object ${keyPath}. Write failed:`, ex);
throw new RuntimeError(
`Can't set object ${keyPath}. Write failed:`,
ex as Error,
);
}
return {
key: req.key,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ export class GetObjectEndpointV1 implements IWebServiceEndpoint {
} catch (ex) {
this.log.error(`${tag} Failed to serve request:`, ex);
res.status(500);
res.json({ error: ex.stack });
res.json({ error: (ex as Error).stack });
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ export class HasObjectEndpointV1 implements IWebServiceEndpoint {
} catch (ex) {
this.log.error(`${tag} Failed to serve request:`, ex);
res.status(500);
res.json({ error: ex.stack });
res.json({ error: (ex as Error).stack });
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ export class SetObjectEndpointV1 implements IWebServiceEndpoint {
} catch (ex) {
this.log.error(`${tag} Failed to serve request:`, ex);
res.status(500);
res.json({ error: ex.stack });
res.json({ error: (ex as Error).stack });
}
}
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@
"tape-promise": "4.0.0",
"ts-loader": "9.2.5",
"ts-node": "10.2.0",
"typescript": "4.3.5",
"typescript": "4.4.3",
"webpack": "5.50.0",
"webpack-bundle-analyzer": "4.4.2",
"webpack-cli": "4.7.2"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ export class DefaultConsortiumProvider
const res = await this.options.apiClient.getConsortiumJwsV1();
return this.parseConsortiumJws(res.data);
} catch (ex) {
const innerException = (ex.toJSON && ex.toJSON()) || ex;
const innerException = ((ex as any).toJSON && (ex as any).toJSON()) || ex;
this.log.error(`Request for Consortium JWS failed: `, innerException);
throw ex;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,16 @@ test("Reports failures with meaningful information", async (t: Test) => {
t2.fail("Provider.get() did not throw despite API errors.");
} catch (ex) {
t2.ok(ex, "Thrown error truthy OK");
t2.ok(ex.message, "Thrown error.message truthy OK");
t2.ok((ex as Error).message, "Thrown error.message truthy OK");
t2.equal(
typeof ex.message,
typeof (ex as Error).message,
"string",
"Thrown error.message type string OK",
);
t2.true(ex.message.includes("timeout"), "Has timeout in msg OK");
t2.true(
(ex as Error).message.includes("timeout"),
"Has timeout in msg OK",
);
}
t2.end();
});
Expand All @@ -58,14 +61,14 @@ test("Reports failures with meaningful information", async (t: Test) => {
t2.fail("Provider.get() did not throw despite API errors.");
} catch (ex) {
t2.ok(ex, "Thrown error truthy OK");
t2.ok(ex.message, "Thrown error.message truthy OK");
t2.ok((ex as Error).message, "Thrown error.message truthy OK");
t2.equal(
typeof ex.message,
typeof (ex as Error).message,
"string",
"Thrown error.message type string OK",
);
t2.true(
ex.message.includes("status code 404"),
(ex as Error).message.includes("status code 404"),
"Has Status Code in msg OK",
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ export class ApiServer {

return { addressInfoCockpit, addressInfoApi, addressInfoGrpc };
} catch (ex) {
const errorMessage = `Failed to start ApiServer: ${ex.stack}`;
const errorMessage = `Failed to start ApiServer: ${(ex as Error).stack}`;
this.log.error(errorMessage);
this.log.error(`Attempting shutdown...`);
try {
Expand Down Expand Up @@ -332,7 +332,7 @@ export class ApiServer {
} catch (ex) {
const errorMessage =
"Could not create plugin installation directory, check the file-system permissions.";
throw new RuntimeError(errorMessage, ex);
throw new RuntimeError(errorMessage, ex as Error);
}
try {
lmify.setPackageManager("npm");
Expand All @@ -355,7 +355,10 @@ export class ApiServer {
}
this.log.info(`Installed ${pkgName} OK`);
} catch (ex) {
throw new RuntimeError(`${fnTag} plugin install fail: ${pkgName}`, ex);
throw new RuntimeError(
`${fnTag} plugin install fail: ${pkgName}`,
ex as Error,
);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -624,7 +624,9 @@ export class ConfigService {
try {
JWS.verify(jws, keyPair);
} catch (ex) {
throw new Error(`${fnTag} Invalid key pair PEM: ${ex && ex.stack}`);
throw new Error(
`${fnTag} Invalid key pair PEM: ${ex && (ex as Error).stack}`,
);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export class WatchHealthcheckV1Endpoint {
socket.emit(WatchHealthcheckV1.Next, next);
} catch (ex) {
log.error(`Failed to construct health check response:`, ex);
socket.emit(WatchHealthcheckV1.Error, ex);
socket.emit(WatchHealthcheckV1.Error, ex as Error);
clearInterval(timerId);
}
}, 1000);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ export class DeployContractEndpoint implements IWebServiceEndpoint {
this.log.error(`Crash while serving ${reqTag}`, ex);
res.status(500).json({
message: "Internal Server Error",
error: ex?.stack || ex?.message,
error: (ex as Error)?.stack || (ex as Error)?.message,
});
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ export class RunTransactionEndpoint implements IWebServiceEndpoint {
this.log.error(`Crash while serving ${reqTag}`, ex);
res.status(500).json({
message: "Internal Server Error",
error: ex?.stack || ex?.message,
error: (ex as Error)?.stack || (ex as Error)?.message,
});
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ export class UnprotectedActionEndpoint implements IWebServiceEndpoint {
this.log.error(`Crash while serving ${reqTag}`, ex);
res.status(500).json({
message: "Internal Server Error",
error: ex?.stack || ex?.message,
error: (ex as Error)?.stack || (ex as Error)?.message,
});
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,14 +143,20 @@ test(testCase, async (t: Test) => {
t.fail("deploy contract response status === 403 FAIL");
} catch (out) {
t.ok(out, "error thrown for forbidden endpoint truthy OK");
t.ok(out.response, "deploy contract response truthy OK");
t.ok((out as any).response, "deploy contract response truthy OK");
t.equal(
out.response.status,
(out as any).response.status,
StatusCodes.FORBIDDEN,
"deploy contract 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.notok(
(out as any).response.data.data,
"(out as any).response.data.data falsy OK",
);
t.notok(
(out as any).response.data.success,
"(out as any).response.data.success falsy OK",
);
}
t.end();
} catch (ex) {
Expand Down
2 changes: 1 addition & 1 deletion packages/cactus-common/src/main/typescript/servers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ export class Servers {
return server;
} catch (ex) {
// if something else went wrong we still want to just give up
if (!ex.message.includes("EADDRINUSE")) {
if (ex instanceof Error && !ex.message.includes("EADDRINUSE")) {
throw ex;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export async function registerWebServiceEndpoint(
} catch (ex) {
throw new Error(
`${fnTag} Express verb method ${httpVerb} threw ` +
` while registering endpoint: ${ex.message}`,
` while registering endpoint: ${(ex as Error).message}`,
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,8 @@ export class GetConsortiumEndpointV1 implements IWebServiceEndpoint {
} catch (ex) {
this.log.error(`${fnTag} failed to serve request`, ex);
res.status(500);
res.statusMessage = ex.message;
res.json({ error: ex.stack });
res.statusMessage = (ex as Error).message;
res.json({ error: (ex as Error).stack });
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,8 @@ export class GetNodeJwsEndpoint implements IWebServiceEndpoint {
} catch (ex) {
this.log.error(`${fnTag} failed to serve request`, ex);
res.status(500);
res.statusMessage = ex.message;
res.json({ error: ex.stack });
res.statusMessage = (ex as Error).message;
res.json({ error: (ex as Error).stack });
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,8 @@ export class GetPrometheusExporterMetricsEndpointV1
} catch (ex) {
this.log.error(`${fnTag} failed to serve request`, ex);
res.status(500);
res.statusMessage = ex.message;
res.json({ error: ex.stack });
res.statusMessage = (ex as Error).message;
res.json({ error: (ex as Error).stack });
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ export class GetSingleStatusEndpoint implements IWebServiceEndpoint {
this.log.error(`${fnTag} failed to serve request`, ex);
res.status(400).json({
message: "Bad request",
error: ex?.stack || ex?.message,
error: (ex as Error)?.stack || (ex as Error)?.message,
});
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ export class GetStatusEndpoint implements IWebServiceEndpoint {
this.log.error(`${fnTag} failed to serve request`, ex);
res.status(400).json({
message: "Bad request",
error: ex?.stack || ex?.message,
error: (ex as Error)?.stack || (ex as Error)?.message,
});
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ export class InitializeEndpoint implements IWebServiceEndpoint {
this.log.error(`${fnTag} failed to serve request`, ex);
res.status(400).json({
message: "Bad request",
error: ex?.stack || ex?.message,
error: (ex as Error)?.stack || (ex as Error)?.message,
});
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ export class NewContractEndpoint implements IWebServiceEndpoint {
this.log.error(`${fnTag} failed to serve request`, ex);
res.status(400).json({
message: "Bad request",
error: ex?.stack || ex?.message,
error: (ex as Error)?.stack || (ex as Error)?.message,
});
}
}
Expand Down
Loading

0 comments on commit 26f15e1

Please sign in to comment.