feat: error handler.

This commit is contained in:
Rintim 2024-01-11 03:24:26 +08:00
parent 0dd72eef89
commit ea07727f68
No known key found for this signature in database
GPG Key ID: BE9E1EA615BACFCF
8 changed files with 133 additions and 0 deletions

15
noname/util/browser.js Normal file
View File

@ -0,0 +1,15 @@
import { ErrorHandler } from './struct/index.js';
/**
* @typedef {import('./struct/interface/error-handler').ErrorHandler} ErrorHandler
*/
/**
* @type {Record<"firefox" | "chrome" | "safari" | "other", new () => ErrorHandler>}
*/
export const errorHandlerMap = {
'chrome': ErrorHandler.ChromeErrorHandler,
'firefox': ErrorHandler.FirefoxErrorHandler,
'safari': ErrorHandler.UnknownErrorHandler,
'other': ErrorHandler.UnknownErrorHandler
};

View File

@ -0,0 +1,45 @@
/**
* @typedef {import('../interface/error-handler').ErrorHandler} ErrorHandler
*/
/**
* @implements {ErrorHandler}
*/
export class ChromeErrorHandler {
#errorList;
constructor() {
this.#errorList = [];
}
onLoad() {
Error.prepareStackTrace = (error, stackTraces) => {
this.#errorList.push([error, stackTraces]);
};
}
/**
*
* @param {PromiseRejectionEvent} event
*/
onHandle(event) {
event.promise.catch((error) => {
const result = this.#errorList.find(savedError => savedError[0] === error);
if (result) {
// @ts-ignore
window.onerror(
result[0].message,
result[1][0].getScriptNameOrSourceURL() || void 0,
result[1][0].getLineNumber() || void 0,
result[1][0].getColumnNumber() || void 0,
result[0]
);
}
});
}
onErrorPrepare() {
this.#errorList.length = 0;
}
}

View File

@ -0,0 +1,32 @@
/**
* @typedef {import('../interface/error-handler').ErrorHandler} ErrorHandler
*/
/**
* @implements {ErrorHandler}
*/
export class FirefoxErrorHandler {
/**
*
* @param {PromiseRejectionEvent} event
*/
onHandle(event) {
event.promise.catch((error) => {
if (typeof error === 'object' && error instanceof Error) {
// Firefox在大环境下默认情况必须要那么多ts-ignore
// @ts-ignore
window.onerror(
error.message,
// @ts-ignore
error.fileName,
// @ts-ignore
error.lineNumber,
// @ts-ignore
error.columnNumber,
error
);
}
});
}
}

View File

@ -0,0 +1,4 @@
export { ChromeErrorHandler } from './chrome.js';
export { FirefoxErrorHandler } from './firefox.js';
export { UnknownErrorHandler } from './unknown.js';

View File

@ -0,0 +1,23 @@
/**
* @typedef {import('../interface/error-handler').ErrorHandler} ErrorHandler
*/
/**
* @implements {ErrorHandler}
*/
export class UnknownErrorHandler {
/**
*
* @param {PromiseRejectionEvent} event
*/
onHandle(event) {
event.promise.catch((error) => {
if (typeof error === 'object' && error instanceof Error) {
// 非chrome和firefox就自生自灭吧
// Safari也是反正没办法解决问题就解决提问的人
throw error;
}
});
}
}

View File

@ -0,0 +1,2 @@
export * as ErrorHandler from './error-handler/index.js';

View File

@ -0,0 +1,10 @@
export interface ErrorHandler {
onLoad?(): void | Promise<void>
onHandle?(event: PromiseRejectionEvent): void | Promise<void>
onErrorPrepare?(): void
onErrorFinish?(): void
}

View File

@ -0,0 +1,2 @@
export { ErrorHandler } from './error-handler'