1
1
// PyScript Error Plugin
2
+ import { buffered } from "polyscript/exports" ;
2
3
import { hooks } from "../core.js" ;
3
4
5
+ let dontBotherDOM = false ;
6
+ export function notOnDOM ( ) {
7
+ dontBotherDOM = true ;
8
+ }
9
+
4
10
hooks . main . onReady . add ( function override ( pyScript ) {
5
11
// be sure this override happens only once
6
12
hooks . main . onReady . delete ( override ) ;
7
13
8
14
// trap generic `stderr` to propagate to it regardless
9
15
const { stderr } = pyScript . io ;
10
16
11
- // override it with our own logic
12
- pyScript . io . stderr = ( error , ...rest ) => {
17
+ const cb = ( error , ...rest ) => {
13
18
notify ( error . message || error ) ;
14
19
// let other plugins or stderr hook, if any, do the rest
15
20
return stderr ( error , ...rest ) ;
16
21
} ;
17
22
23
+ // override it with our own logic
24
+ pyScript . io . stderr = pyScript . type === "py" ? cb : buffered ( cb ) ;
25
+
18
26
// be sure uncaught Python errors are also visible
19
27
addEventListener ( "error" , ( { message } ) => {
20
28
if ( message . startsWith ( "Uncaught PythonError" ) ) notify ( message ) ;
@@ -30,6 +38,7 @@ hooks.main.onReady.add(function override(pyScript) {
30
38
* @param {string } message
31
39
*/
32
40
export function notify ( message ) {
41
+ if ( dontBotherDOM ) return ;
33
42
const div = document . createElement ( "div" ) ;
34
43
div . className = "py-error" ;
35
44
div . textContent = message ;
0 commit comments