8000 [chore] Improve current Error extends (#1467) · pyscript/pyscript@61b3154 · GitHub
[go: up one dir, main page]

Skip to content

Commit 61b3154

Browse files
[chore] Improve current Error extends (#1467)
1 parent fb9b30d commit 61b3154

File tree

1 file changed

+16
-32
lines changed

1 file changed

+16
-32
lines changed

pyscriptjs/src/exceptions.ts

Lines changed: 16 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,10 @@ const CLOSEBUTTON = `<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16'
22

33
type MessageType = 'text' | 'html';
44

5-
/*
6-
These error codes are used to identify the type of error that occurred.
7-
The convention is:
8-
* PY0 - errors that occur when fetching
9-
* PY1 - errors that occur in config
10-
* Py2 - errors that occur in plugins
11-
* PY9 - Deprecation errors
12-
*/
13-
5+
/**
6+
* These error codes are used to identify the type of error that occurred.
7+
* @see https://docs.pyscript.net/latest/reference/exceptions.html?highlight=errors
8+
*/
149
export enum ErrorCode {
1510
GENERIC = 'PY0000', // Use this only for development then change to a more specific error code
1611
FETCH_ERROR = 'PY0001',
@@ -29,34 +24,27 @@ export enum ErrorCode {
2924
}
3025

3126
export class UserError extends Error {
32-
messageType: MessageType;
33-
errorCode: ErrorCode;
3427
/**
3528
* `isinstance` doesn't work correctly across multiple realms.
3629
* Hence, `$$isUserError` flag / marker is used to identify a `UserError`.
3730
*/
3831
$$isUserError: boolean;
3932

40-
constructor(errorCode: ErrorCode, message: string, t: MessageType = 'text') {
41-
super(message);
42-
this.errorCode = errorCode;
33+
constructor(public errorCode: ErrorCode, message: string, public messageType: MessageType = 'text') {
34+
super(`(${errorCode}): ${message}`);
4335
this.name = 'UserError';
44-
this.messageType = t;
45-
this.message = `(${errorCode}): ${message}`;
4636
this.$$isUserError = true;
4737
}
4838
}
4939

5040
export class FetchError extends UserError {
51-
errorCode: ErrorCode;
5241
constructor(errorCode: ErrorCode, message: string) {
5342
super(errorCode, message);
5443
this.name = 'FetchError';
5544
}
5645
}
5746

5847
export class InstallError extends UserError {
59-
errorCode: ErrorCode;
6048
constructor(errorCode: ErrorCode, message: string) {
6149
super(errorCode, message);
6250
this.name = 'InstallError';
@@ -78,25 +66,21 @@ export function _createAlertBanner(
7866
break;
7967
}
8068

81-
const banner = document.createElement('div');
82-
banner.className = `alert-banner py-${level}`;
83-
84-
if (messageType === 'html') {
85-
banner.innerHTML = message;
86-
} else {
87-
banner.textContent = message;
88-
}
69+
const content = messageType === 'html' ? 'innerHTML' : 'textContent';
70+
const banner = Object.assign(document.createElement('div'), {
71+
className: `alert-banner py-${level}`,
72+
[content]: message,
73+
});
8974

9075
if (level === 'warning') {
91-
const closeButton = document.createElement('button');
76+
const closeButton = Object.assign(document.createElement('button'), {
77+
id: 'alert-close-button',
78+
innerHTML: CLOSEBUTTON,
79+
});
9280

93-
closeButton.id = 'alert-close-button';
94-
closeButton.addEventListener('click', () => {
81+
banner.appendChild(closeButton).addEventListener('click', () => {
9582
banner.remove();
9683
});
97-
closeButton.innerHTML = CLOSEBUTTON;
98-
99-
banner.appendChild(closeButton);
10084
}
10185

10286
document.body.prepend(banner);

0 commit comments

Comments
 (0)
0