Skip to content

Commit

Permalink
Updated error handling for incl of errors for stack tracing etc...
Browse files Browse the repository at this point in the history
  • Loading branch information
mrinc committed Jan 31, 2024
1 parent 07a03af commit 066f004
Show file tree
Hide file tree
Showing 7 changed files with 124 additions and 42 deletions.
49 changes: 38 additions & 11 deletions nodejs/src/base/PluginLogger.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { SBLogging } from "../serviceBase";
import { DEBUG_MODE, SmartLogMeta, IPluginLogger } from "../interfaces";
import { BSBError } from "./errorMessages";

export class PluginLogger implements IPluginLogger {
private logging: SBLogging;
Expand All @@ -14,7 +15,7 @@ export class PluginLogger implements IPluginLogger {
}
public debug<T extends string>(message: T, ...meta: SmartLogMeta<T>): void {
if (this.canDebug)
this.logging.logBus.emit("debug", this.pluginName, message, meta[0]);
this.logging.logBus.emit("debug", this.pluginName, message, ...meta);
}
public reportStat(key: string, value: number): void {
this.logging.logBus.emit("reportStat", this.pluginName, key, value);
Expand All @@ -23,20 +24,46 @@ export class PluginLogger implements IPluginLogger {
message: T,
...meta: SmartLogMeta<T>
): void {
this.logging.logBus.emit(
"reportTextStat",
this.pluginName,
message,
meta[0]
);
this.logging.logBus.emit("reportTextStat", this.pluginName, message, meta);
}
public info<T extends string>(message: T, ...meta: SmartLogMeta<T>): void {
this.logging.logBus.emit("info", this.pluginName, message, meta[0]);
this.logging.logBus.emit("info", this.pluginName, message, meta);
}
public warn<T extends string>(message: T, ...meta: SmartLogMeta<T>): void {
this.logging.logBus.emit("warn", this.pluginName, message, meta[0]);
this.logging.logBus.emit("warn", this.pluginName, message, meta);
}
public error<T extends string>(message: T, ...meta: SmartLogMeta<T>): void {
this.logging.logBus.emit("error", this.pluginName, message, meta[0]);
public error<T extends string>(error: BSBError<T>): void;
public error<T extends string>(
message: T,
error: Error,
meta: SmartLogMeta<T>
): void;
public error<T extends string>(message: T, meta: SmartLogMeta<T>): void;
public error<T extends string>(
messageOrError: T | BSBError<T>,
errorOrMeta?: Error | SmartLogMeta<T>,
meta?: SmartLogMeta<T>
): void {
if (messageOrError instanceof BSBError) {
if (messageOrError.raw !== null) {
this.logging.logBus.emit(
"error",
this.pluginName,
messageOrError.raw.message,
messageOrError,
messageOrError.raw.meta
);
return;
}
this.logging.logBus.emit(
"error",
this.pluginName,
messageOrError.message,
messageOrError,
{}
);
return;
}
this.logging.logBus.emit("error", this.pluginName, messageOrError, errorOrMeta, meta);
}
}
20 changes: 20 additions & 0 deletions nodejs/src/base/errorMessages.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { LogFormatter } from "./index";
import { LogMeta } from "../interfaces";

export type EMetaDef<T extends string> = {
message: T;
meta: LogMeta<T>;
};
export class BSBError<T extends string> extends Error {
constructor(errorKey: string, message: T, meta: LogMeta<T>);
constructor(message: T, meta: LogMeta<T>);
Expand All @@ -13,11 +17,27 @@ export class BSBError<T extends string> extends Error {
if (meta === undefined && typeof messageOrMeta === "object") {
super(formatter.formatLog(errorKeyOrMessage, messageOrMeta));
this.name = "BSBError-Generic";
this.raw = {
message: errorKeyOrMessage,
meta: messageOrMeta,
};
} else if (typeof messageOrMeta === "string" && typeof meta === "object") {
super(formatter.formatLog(messageOrMeta, meta));
this.name = "BSBError-" + errorKeyOrMessage;
this.raw = {
message: messageOrMeta,
meta: meta,
};
} else {
super(errorKeyOrMessage);
this.name = "BSBError-Generic";
this.raw = {
message: errorKeyOrMessage,
meta: messageOrMeta ?? {},
};
}
}
public raw: EMetaDef<string> | null = null;

public toString(): string {
return this.message;
Expand Down
8 changes: 5 additions & 3 deletions nodejs/src/base/logging.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,8 @@ export abstract class BSBLogging<
*/
public abstract error<T extends string>(
plugin: string,
message: T,
messageOrError: T,
errorOrMeta?: Error | LogMeta<T>,
meta?: LogMeta<T>
): Promise<void> | void;
}
Expand Down Expand Up @@ -181,8 +182,9 @@ export class BSBLoggingRef extends BSBLogging<null> {
}
public error<T extends string>(
plugin: string,
message: T,
meta?: LogMeta<T> | undefined
messageOrError: T,
errorOrMeta?: Error | LogMeta<T>,
meta?: LogMeta<T>
): void {
throw BSB_ERROR_METHOD_NOT_IMPLEMENTED("BSBLoggingRef", "error");
}
Expand Down
12 changes: 11 additions & 1 deletion nodejs/src/interfaces/logging.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { ParamsFromString } from "@bettercorp/tools/lib/Interfaces";
import { BSBError } from "../base";

export type DEBUG_MODE = "production" | "production-debug" | "development";

Expand Down Expand Up @@ -31,7 +32,16 @@ export interface IPluginLogger {
info<T extends string>(message: T, ...meta: SmartLogMeta<T>): void;
warn<T extends string>(message: T, ...meta: SmartLogMeta<T>): void;
debug<T extends string>(message: T, ...meta: SmartLogMeta<T>): void;
error<T extends string>(message: T, ...meta: SmartLogMeta<T>): void;
error<T extends string>(
message: T,
...meta: SmartLogMeta<T>
): void;
error<T extends string>(
message: T,
error: Error,
...meta: SmartLogMeta<T>
): void;
error<T extends string>(error: BSBError<T>): void;
}

export const LoggingEventTypesBase = {
Expand Down
37 changes: 19 additions & 18 deletions nodejs/src/plugins/logging-default/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ export class Plugin extends BSBLogging {
level: LogLevels,
plugin: string,
message: T,
meta?: LogMeta<T>
meta?: LogMeta<T>,
additionalToConsole?: any
) {
let formattedMessage = this.logFormatter.formatLog<T>(message, meta);
formattedMessage = `[${plugin.toUpperCase()}] ${formattedMessage}`;
Expand Down Expand Up @@ -74,6 +75,12 @@ export class Plugin extends BSBLogging {
colour = [CONSOLE_COLOURS.BgRed, CONSOLE_COLOURS.FgBlack];
}
if (this._mockConsole) return this._mockedConsole!(level, formattedMessage);
if (additionalToConsole)
func(
colour.join("") + "%s" + CONSOLE_COLOURS.Reset,
formattedMessage,
additionalToConsole
);
func(colour.join("") + "%s" + CONSOLE_COLOURS.Reset, formattedMessage);
}

Expand Down Expand Up @@ -114,24 +121,18 @@ export class Plugin extends BSBLogging {
public error<T extends string>(
plugin: string,
message: T,
meta: LogMeta<T>
): void;
public error(plugin: string, error: Error): void;
public error<T extends string>(
plugin: string,
messageOrError: T | Error,
errorOrMeta?: Error | LogMeta<T>,
meta?: LogMeta<T>
): void {
const message =
typeof messageOrError === "string"
? messageOrError
: messageOrError.message;
this.logEvent<T>(LOG_LEVELS.ERROR, plugin, message as T, meta);
if (
typeof messageOrError !== "string" &&
messageOrError.stack !== undefined
) {
console.error(messageOrError.stack.toString());
}
const hasErrorDefinition = meta !== undefined;
const inclStack = errorOrMeta instanceof Error && errorOrMeta.stack;

this.logEvent<T>(
LOG_LEVELS.ERROR,
plugin,
message as T,
hasErrorDefinition ? meta : (errorOrMeta as LogMeta<T>),
inclStack ? errorOrMeta.stack : undefined
);
}
}
33 changes: 25 additions & 8 deletions nodejs/src/serviceBase/logging.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,17 @@ export class SBLogging {
this.logBus.on("warn", (plugin, message, meta) => {
this.triggerLogEvent("warn", plugin, message, meta);
});
this.logBus.on("error", (plugin, message, meta) => {
this.triggerLogEvent("error", plugin, message, meta);
});
this.logBus.on(
"error",
(
plugin,
message: string,
errorOrMeta: Error | LogMeta<string>,
meta?: LogMeta<string>
) => {
this.triggerLogEvent("error", plugin, message, errorOrMeta, meta);
}
);
}

public dispose() {
Expand Down Expand Up @@ -161,21 +169,23 @@ export class SBLogging {
logAs: LoggingEventTypesExlReportStat,
plugin: string,
message: T,
meta: LogMeta<T>
metaOrError: LogMeta<T> | Error,
meta?: LogMeta<T>
): Promise<void>;
private async triggerLogEvent<T extends string>(
logAs: LoggingEventTypes,
plugin: string,
messageOrKey: T | string,
metaOrValue: LogMeta<T> | number
metaOrValueOrError: LogMeta<T> | number | Error,
meta?: LogMeta<T>
): Promise<void> {
for (const logger of this.getPluginsMatchingLogEvent(logAs, plugin)) {
if (logAs === "reportStat") {
await this.triggerLogEventReportStat(
logger.plugin,
plugin,
messageOrKey as string,
metaOrValue as number
metaOrValueOrError as number
);
continue;
}
Expand All @@ -195,7 +205,14 @@ export class SBLogging {
method = logger.plugin.warn;
break;
case "error":
method = logger.plugin.error;
return await SmartFunctionCallAsync(
logger.plugin,
logger.plugin.error,
plugin,
messageOrKey as string,
metaOrValueOrError,
meta
);
break;
}

Expand All @@ -205,7 +222,7 @@ export class SBLogging {
method,
plugin,
messageOrKey as string,
metaOrValue as LogMeta<T>
metaOrValueOrError as LogMeta<T>
);
}
}
Expand Down
7 changes: 6 additions & 1 deletion nodejs/src/tests/plugins/logging-default/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -286,9 +286,14 @@ describe("plugins/logging-default", () => {
const plugin = new Plugin(getLoggingConstructorConfig());
storeConsole("error", undefined, [
"test-error",
"An error while stacking",
"src/tests/plugins/logging-default/plugin.ts:",
]);
await plugin.error("infW-DbG", new Error("test-error"));
plugin.error(
"infW-DbG",
"An error while stacking",
new Error("test-error")
);
restoreConsole();
});
});
Expand Down

0 comments on commit 066f004

Please sign in to comment.