mirror of
https://github.com/pyscript/pyscript.git
synced 2025-12-22 11:45:28 -05:00
[chore] Improve current Error extends (#1467)
This commit is contained in:
committed by
GitHub
parent
fb9b30d144
commit
61b3154461
@@ -2,15 +2,10 @@ const CLOSEBUTTON = `<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16'
|
|||||||
|
|
||||||
type MessageType = 'text' | 'html';
|
type MessageType = 'text' | 'html';
|
||||||
|
|
||||||
/*
|
/**
|
||||||
These error codes are used to identify the type of error that occurred.
|
* These error codes are used to identify the type of error that occurred.
|
||||||
The convention is:
|
* @see https://docs.pyscript.net/latest/reference/exceptions.html?highlight=errors
|
||||||
* PY0 - errors that occur when fetching
|
*/
|
||||||
* PY1 - errors that occur in config
|
|
||||||
* Py2 - errors that occur in plugins
|
|
||||||
* PY9 - Deprecation errors
|
|
||||||
*/
|
|
||||||
|
|
||||||
export enum ErrorCode {
|
export enum ErrorCode {
|
||||||
GENERIC = 'PY0000', // Use this only for development then change to a more specific error code
|
GENERIC = 'PY0000', // Use this only for development then change to a more specific error code
|
||||||
FETCH_ERROR = 'PY0001',
|
FETCH_ERROR = 'PY0001',
|
||||||
@@ -29,26 +24,20 @@ export enum ErrorCode {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export class UserError extends Error {
|
export class UserError extends Error {
|
||||||
messageType: MessageType;
|
|
||||||
errorCode: ErrorCode;
|
|
||||||
/**
|
/**
|
||||||
* `isinstance` doesn't work correctly across multiple realms.
|
* `isinstance` doesn't work correctly across multiple realms.
|
||||||
* Hence, `$$isUserError` flag / marker is used to identify a `UserError`.
|
* Hence, `$$isUserError` flag / marker is used to identify a `UserError`.
|
||||||
*/
|
*/
|
||||||
$$isUserError: boolean;
|
$$isUserError: boolean;
|
||||||
|
|
||||||
constructor(errorCode: ErrorCode, message: string, t: MessageType = 'text') {
|
constructor(public errorCode: ErrorCode, message: string, public messageType: MessageType = 'text') {
|
||||||
super(message);
|
super(`(${errorCode}): ${message}`);
|
||||||
this.errorCode = errorCode;
|
|
||||||
this.name = 'UserError';
|
this.name = 'UserError';
|
||||||
this.messageType = t;
|
|
||||||
this.message = `(${errorCode}): ${message}`;
|
|
||||||
this.$$isUserError = true;
|
this.$$isUserError = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export class FetchError extends UserError {
|
export class FetchError extends UserError {
|
||||||
errorCode: ErrorCode;
|
|
||||||
constructor(errorCode: ErrorCode, message: string) {
|
constructor(errorCode: ErrorCode, message: string) {
|
||||||
super(errorCode, message);
|
super(errorCode, message);
|
||||||
this.name = 'FetchError';
|
this.name = 'FetchError';
|
||||||
@@ -56,7 +45,6 @@ export class FetchError extends UserError {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export class InstallError extends UserError {
|
export class InstallError extends UserError {
|
||||||
errorCode: ErrorCode;
|
|
||||||
constructor(errorCode: ErrorCode, message: string) {
|
constructor(errorCode: ErrorCode, message: string) {
|
||||||
super(errorCode, message);
|
super(errorCode, message);
|
||||||
this.name = 'InstallError';
|
this.name = 'InstallError';
|
||||||
@@ -78,25 +66,21 @@ export function _createAlertBanner(
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
const banner = document.createElement('div');
|
const content = messageType === 'html' ? 'innerHTML' : 'textContent';
|
||||||
banner.className = `alert-banner py-${level}`;
|
const banner = Object.assign(document.createElement('div'), {
|
||||||
|
className: `alert-banner py-${level}`,
|
||||||
if (messageType === 'html') {
|
[content]: message,
|
||||||
banner.innerHTML = message;
|
});
|
||||||
} else {
|
|
||||||
banner.textContent = message;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (level === 'warning') {
|
if (level === 'warning') {
|
||||||
const closeButton = document.createElement('button');
|
const closeButton = Object.assign(document.createElement('button'), {
|
||||||
|
id: 'alert-close-button',
|
||||||
|
innerHTML: CLOSEBUTTON,
|
||||||
|
});
|
||||||
|
|
||||||
closeButton.id = 'alert-close-button';
|
banner.appendChild(closeButton).addEventListener('click', () => {
|
||||||
closeButton.addEventListener('click', () => {
|
|
||||||
banner.remove();
|
banner.remove();
|
||||||
});
|
});
|
||||||
closeButton.innerHTML = CLOSEBUTTON;
|
|
||||||
|
|
||||||
banner.appendChild(closeButton);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
document.body.prepend(banner);
|
document.body.prepend(banner);
|
||||||
|
|||||||
Reference in New Issue
Block a user