Refactor error logging
Signed-off-by: trivernis <trivernis@protonmail.com>pull/6/head
parent
26817cfec9
commit
dd6107d1fd
@ -1,53 +0,0 @@
|
||||
import {Injectable} from "@angular/core";
|
||||
import {listen} from "@tauri-apps/api/event";
|
||||
|
||||
@Injectable({
|
||||
providedIn: "root"
|
||||
})
|
||||
export class ErrorBrokerService {
|
||||
|
||||
errorCb: Function | undefined;
|
||||
infoCb: Function | undefined;
|
||||
|
||||
constructor() {
|
||||
this.registerListener().catch(err => console.error(err));
|
||||
}
|
||||
|
||||
async registerListener() {
|
||||
const _unlisten = await listen("error", event => {
|
||||
const payload: any = event.payload;
|
||||
if (payload.message) {
|
||||
this.showError(payload);
|
||||
} else {
|
||||
this.showError(payload.toString());
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
async try<T>(fn: () => Promise<T>): Promise<T | undefined> {
|
||||
try {
|
||||
return await fn();
|
||||
} catch (err) {
|
||||
this.showError(err);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
showInfo(info: string) {
|
||||
console.log(info);
|
||||
if (this.infoCb) {
|
||||
this.infoCb(info);
|
||||
}
|
||||
}
|
||||
|
||||
showError(error: { message: string } | any) {
|
||||
console.error(error);
|
||||
if (this.errorCb) {
|
||||
if (!error.message) {
|
||||
this.errorCb({ message: error });
|
||||
} else {
|
||||
this.errorCb({ ...error });
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,24 @@
|
||||
export enum LogLevel {
|
||||
Trace,
|
||||
Debug,
|
||||
Info,
|
||||
Warn,
|
||||
Error,
|
||||
}
|
||||
|
||||
export class LogEntry {
|
||||
constructor(private message: string, private level: LogLevel, private error?: Error) {
|
||||
}
|
||||
|
||||
public getMessage(): string {
|
||||
return this.message;
|
||||
}
|
||||
|
||||
public getLevel(): LogLevel {
|
||||
return this.level;
|
||||
}
|
||||
|
||||
public getError(): Error | undefined {
|
||||
return this.error;
|
||||
}
|
||||
}
|
@ -1,13 +1,13 @@
|
||||
import {TestBed} from "@angular/core/testing";
|
||||
|
||||
import {ErrorBrokerService} from "./error-broker.service";
|
||||
import {LoggingService} from "./logging.service";
|
||||
|
||||
describe("ErrorBrokerService", () => {
|
||||
let service: ErrorBrokerService;
|
||||
let service: LoggingService;
|
||||
|
||||
beforeEach(() => {
|
||||
TestBed.configureTestingModule({});
|
||||
service = TestBed.inject(ErrorBrokerService);
|
||||
service = TestBed.inject(LoggingService);
|
||||
});
|
||||
|
||||
it("should be created", () => {
|
@ -0,0 +1,60 @@
|
||||
import {Injectable} from "@angular/core";
|
||||
import {listen} from "@tauri-apps/api/event";
|
||||
import {BehaviorSubject} from "rxjs";
|
||||
import {LogEntry, LogLevel} from "./LogEntry";
|
||||
|
||||
@Injectable({
|
||||
providedIn: "root"
|
||||
})
|
||||
export class LoggingService {
|
||||
|
||||
logs = new BehaviorSubject<LogEntry>(new LogEntry("Log initialized", LogLevel.Trace));
|
||||
|
||||
constructor() {
|
||||
this.registerListener().catch(err => console.error(err));
|
||||
}
|
||||
|
||||
async registerListener() {
|
||||
const _unlisten = await listen("error", event => {
|
||||
const payload: any = event.payload;
|
||||
if (payload.message) {
|
||||
this.error(payload);
|
||||
} else {
|
||||
this.error(payload.toString());
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
async try<T>(fn: () => Promise<T>): Promise<T | undefined> {
|
||||
try {
|
||||
return await fn();
|
||||
} catch (err: any) {
|
||||
this.error(err);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
trace(message: string) {
|
||||
this.log(LogLevel.Trace, message);
|
||||
}
|
||||
|
||||
debug(message: string) {
|
||||
this.log(LogLevel.Debug, message);
|
||||
}
|
||||
|
||||
info(message: string) {
|
||||
this.log(LogLevel.Info, message);
|
||||
}
|
||||
|
||||
warn(message: string) {
|
||||
this.log(LogLevel.Warn, message);
|
||||
}
|
||||
|
||||
error(error: Error, message?: string) {
|
||||
this.log(LogLevel.Error, message ?? error.message ?? error.toString(), error);
|
||||
}
|
||||
|
||||
public log(level: LogLevel, message: string, error?: Error) {
|
||||
this.logs.next(new LogEntry(message, level, error));
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue