Add error handler
This commit is contained in:
parent
fa103bcdf6
commit
c44a6858e0
6 changed files with 104 additions and 39 deletions
45
src/utils/error-handler.ts
Normal file
45
src/utils/error-handler.ts
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
export enum Severity {
|
||||
INFO = 'info',
|
||||
WARNING = 'warning',
|
||||
ERROR = 'error',
|
||||
}
|
||||
|
||||
export interface ErrorHandlerError {
|
||||
severity: Severity;
|
||||
message: string;
|
||||
}
|
||||
|
||||
export type ErrorMetadata = { [key: string]: any };
|
||||
|
||||
export class ErrorHandler {
|
||||
private static readonly errors: Array<ErrorHandlerError> = [];
|
||||
private static metadata: ErrorMetadata = {};
|
||||
private static onErrorListeners: Array<
|
||||
(error: ErrorHandlerError, metadata: ErrorMetadata) => void
|
||||
> = [];
|
||||
|
||||
public static addException(exception: Error) {
|
||||
ErrorHandler.addError(Severity.ERROR, exception.message);
|
||||
}
|
||||
|
||||
public static addError(severity: Severity, message: string) {
|
||||
ErrorHandler.errors.push({ severity, message });
|
||||
ErrorHandler.onErrorListeners.forEach((listener) =>
|
||||
listener({ severity, message }, ErrorHandler.metadata)
|
||||
);
|
||||
}
|
||||
|
||||
public static addMetadata(key: string, value: any) {
|
||||
const serialized = {};
|
||||
for (const k in value) {
|
||||
serialized[k] = value[k];
|
||||
}
|
||||
ErrorHandler.metadata[key] = serialized;
|
||||
}
|
||||
|
||||
public static addOnErrorListener(
|
||||
listener: (error: ErrorHandlerError, metadata: ErrorMetadata) => void
|
||||
) {
|
||||
ErrorHandler.onErrorListeners.push(listener);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue