@@ -2,15 +2,10 @@ const CLOSEBUTTON = `<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16'
2
2
3
3
type MessageType = 'text' | 'html' ;
4
4
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
+ */
14
9
export enum ErrorCode {
15
10
GENERIC = 'PY0000' , // Use this only for development then change to a more specific error code
16
11
FETCH_ERROR = 'PY0001' ,
@@ -29,34 +24,27 @@ export enum ErrorCode {
29
24
}
30
25
31
26
export class UserError extends Error {
32
- messageType : MessageType ;
33
- errorCode : ErrorCode ;
34
27
/**
35
28
* `isinstance` doesn't work correctly across multiple realms.
36
29
* Hence, `$$isUserError` flag / marker is used to identify a `UserError`.
37
30
*/
38
31
$$isUserError : boolean ;
39
32
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 } ` ) ;
43
35
this . name = 'UserError' ;
44
- this . messageType = t ;
45
- this . message = `(${ errorCode } ): ${ message } ` ;
46
36
this . $$isUserError = true ;
47
37
}
48
38
}
49
39
50
40
export class FetchError extends UserError {
51
- errorCode : ErrorCode ;
52
41
constructor ( errorCode : ErrorCode , message : string ) {
53
42
super ( errorCode , message ) ;
54
43
this . name = 'FetchError' ;
55
44
}
56
45
}
57
46
58
47
export class InstallError extends UserError {
59
- errorCode : ErrorCode ;
60
48
constructor ( errorCode : ErrorCode , message : string ) {
61
49
super ( errorCode , message ) ;
62
50
this . name = 'InstallError' ;
@@ -78,25 +66,21 @@ export function _createAlertBanner(
78
66
break ;
79
67
}
80
68
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
+ } ) ;
89
74
90
75
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
+ } ) ;
92
80
93
- closeButton . id = 'alert-close-button' ;
94
- closeButton . addEventListener ( 'click' , ( ) => {
81
+ banner . appendChild ( closeButton ) . addEventListener ( 'click' , ( ) => {
95
82
banner . remove ( ) ;
96
83
} ) ;
97
- closeButton . innerHTML = CLOSEBUTTON ;
98
-
99
- banner . appendChild ( closeButton ) ;
100
84
}
101
85
102
86
document . body . prepend ( banner ) ;
0 commit comments