Skip to content

Commit

Permalink
fix: fix log-helper for generic exception handling
Browse files Browse the repository at this point in the history
Introduce public getExceptionStack and getExceptionMessage functions
with corresponding unit tests and include the new log-helper into
api-server as usage example

Closes: hyperledger#1702
Signed-off-by: Michael Courtin <michael.courtin@accenture.com>
  • Loading branch information
m-courtin committed Jan 4, 2022
1 parent 093629a commit c499e0c
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import {
Bools,
Logger,
LoggerProvider,
LogHelper,
Servers,
} from "@hyperledger/cactus-common";

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

return { addressInfoCockpit, addressInfoApi, addressInfoGrpc };
} catch (ex) {
const errorMessage = `Failed to start ApiServer: ${ex.stack}`;
const stack = LogHelper.getExceptionStack(ex);
const errorMessage = `Failed to start ApiServer: ${stack}`;
this.log.error(errorMessage);
this.log.error(`Attempting shutdown...`);
try {
Expand Down Expand Up @@ -296,9 +298,10 @@ 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}`;
const stack = LogHelper.getExceptionStack(ex);
const errorMessage = `Failed init PluginRegistry: ${stack}`;
this.log.error(errorMessage);
throw new Error(errorMessage);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export class LogHelper {
static getExceptionStack(exception: unknown): string {
public static getExceptionStack(exception: unknown): string {
// handle unknown exception input
const defaultStack = "";
const defaultStack = "NO_STACK_INFORMATION_INCLUDED_IN_EXCEPTION";
const invalidStack = "INVALID_STACK_INFORMATION";
let stack = defaultStack;

Expand All @@ -26,9 +26,9 @@ export class LogHelper {
return stack;
}

static getExceptionMessage(exception: unknown): string {
public static getExceptionMessage(exception: unknown): string {
// handle unknown exception input
const defaultMessage = "";
const defaultMessage = "NO_MESSAGE_INCLUDED_IN_EXCEPTION";
const invalidMessage = "INVALID_EXCEPTION_MESSAGE";
let message = defaultMessage;

Expand Down
1 change: 1 addition & 0 deletions packages/cactus-common/src/main/typescript/public-api.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export { LoggerProvider } from "./logging/logger-provider";
export { LogHelper } from "./logging/log-helper";
export { Logger, ILoggerOptions } from "./logging/logger";
export { LogLevelDesc } from "loglevel";
export { Objects } from "./objects";
Expand Down

0 comments on commit c499e0c

Please sign in to comment.