Skip to content

Commit

Permalink
fix: provide generic exception handling functionality
Browse files Browse the repository at this point in the history
Introduce a log helper with static functions to get exception message /
exception stack whatever is thrown or provided.

Closes: hyperledger-cacti#1702
Signed-off-by: Michael Courtin <michael.courtin@accenture.com>
  • Loading branch information
m-courtin authored and petermetz committed Feb 23, 2022
1 parent 1ae6f72 commit c2682ba
Show file tree
Hide file tree
Showing 7 changed files with 829 additions and 38 deletions.
49 changes: 20 additions & 29 deletions packages/cactus-cmd-api-server/src/main/typescript/api-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import {
Bools,
Logger,
LoggerProvider,
ExceptionHelper,
Servers,
} from "@hyperledger/cactus-common";

Expand Down Expand Up @@ -241,16 +242,16 @@ export class ApiServer {

return { addressInfoCockpit, addressInfoApi, addressInfoGrpc };
} catch (ex) {
const errorMessage = `Failed to start ApiServer: ${ex.stack}`;
this.log.error(errorMessage);
const context = "Failed to start ApiServer";
this.log.exception(ex, context);
this.log.error(`Attempting shutdown...`);
try {
await this.shutdown();
this.log.info(`Server shut down after crash OK`);
} catch (ex) {
this.log.error(ApiServer.E_POST_CRASH_SHUTDOWN, ex);
this.log.exception(ex, ApiServer.E_POST_CRASH_SHUTDOWN);
}
throw new Error(errorMessage);
throw ExceptionHelper.newRuntimeError(ex, context);
}
}

Expand Down Expand Up @@ -296,11 +297,11 @@ export class ApiServer {
await this.getPluginImportsCount(),
);
return this.pluginRegistry;
} catch (e) {
} catch (ex) {
this.pluginRegistry = new PluginRegistry({ plugins: [] });
const errorMessage = `Failed init PluginRegistry: ${e.stack}`;
this.log.error(errorMessage);
throw new Error(errorMessage);
const context = "Failed to init PluginRegistry";
this.log.exception(ex, context);
throw ExceptionHelper.newRuntimeError(ex, context);
}
}

Expand Down Expand Up @@ -357,15 +358,10 @@ export class ApiServer {
await plugin.onPluginInit();

return plugin;
} catch (error) {
const errorMessage = `${fnTag} failed instantiating plugin '${packageName}' with the instanceId '${options.instanceId}'`;
this.log.error(errorMessage, error);

if (error instanceof Error) {
throw new RuntimeError(errorMessage, error);
} else {
throw new RuntimeError(errorMessage, JSON.stringify(error));
}
} catch (ex) {
const context = `${fnTag} failed instantiating plugin '${packageName}' with the instanceId '${options.instanceId}'`;
this.log.exception(ex, context);
throw ExceptionHelper.newRuntimeError(ex, context);
}
}

Expand All @@ -387,9 +383,9 @@ export class ApiServer {
await fs.mkdirp(pluginPackageDir);
this.log.debug(`${pkgName} plugin package dir: %o`, pluginPackageDir);
} catch (ex) {
const errorMessage =
const context =
"Could not create plugin installation directory, check the file-system permissions.";
throw new RuntimeError(errorMessage, ex);
throw ExceptionHelper.newRuntimeError(ex, context);
}
try {
lmify.setPackageManager("npm");
Expand All @@ -408,18 +404,13 @@ export class ApiServer {
]);
this.log.debug("%o install result: %o", pkgName, out);
if (out.exitCode !== 0) {
throw new RuntimeError("Non-zero exit code: ", JSON.stringify(out));
throw ExceptionHelper.newRuntimeError(out, "Non-zero exit code");
}
this.log.info(`Installed ${pkgName} OK`);
} catch (ex) {
const errorMessage = `${fnTag} failed installing plugin '${pkgName}`;
this.log.error(errorMessage, ex);

if (ex instanceof Error) {
throw new RuntimeError(errorMessage, ex);
} else {
throw new RuntimeError(errorMessage, JSON.stringify(ex));
}
const context = `${fnTag} failed installing plugin '${pkgName}`;
this.log.exception(ex, context);
throw ExceptionHelper.newRuntimeError(ex, context);
}
}

Expand Down Expand Up @@ -456,7 +447,7 @@ export class ApiServer {
await new Promise<void>((resolve, reject) => {
this.grpcServer.tryShutdown((ex?: Error) => {
if (ex) {
this.log.error("Failed to shut down gRPC server: ", ex);
this.log.exception(ex, "Failed to shut down gRPC server");
reject(ex);
} else {
resolve();
Expand Down
6 changes: 6 additions & 0 deletions packages/cactus-common/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@
"name": "Peter Somogyvari",
"email": "peter.somogyvari@accenture.com",
"url": "https://accenture.com"
},
{
"name": "Michael Courtin",
"email": "michael.courtin@accenture.com",
"url": "https://accenture.com"
}
],
"license": "Apache-2.0",
Expand All @@ -60,6 +65,7 @@
},
"homepage": "https://github.com/hyperledger/cactus#readme",
"dependencies": {
"fast-safe-stringify": "2.1.1",
"json-stable-stringify": "1.0.1",
"key-encoder": "2.0.3",
"loglevel": "1.7.1",
Expand Down
Loading

0 comments on commit c2682ba

Please sign in to comment.