mirror of
https://github.com/pyscript/pyscript.git
synced 2025-12-19 18:27:29 -05:00
[next] Include most basic error plugin (#1677)
This commit is contained in:
committed by
GitHub
parent
264675d0c3
commit
1d015c7534
@@ -5,6 +5,7 @@ import { htmlDecode } from "./utils.js";
|
||||
import sync from "./sync.js";
|
||||
|
||||
import stdlib from "./stdlib.js";
|
||||
import plugins from "./plugins.js";
|
||||
|
||||
// TODO: this is not strictly polyscript related but handy ... not sure
|
||||
// we should factor this utility out a part but this works anyway.
|
||||
@@ -13,7 +14,7 @@ import { Hook } from "../node_modules/polyscript/esm/worker/hooks.js";
|
||||
|
||||
import { robustFetch as fetch } from "./fetch.js";
|
||||
|
||||
const { assign, defineProperty } = Object;
|
||||
const { assign, defineProperty, entries } = Object;
|
||||
|
||||
const getText = (body) => body.text();
|
||||
|
||||
@@ -163,6 +164,16 @@ define("py", {
|
||||
shouldRegister = false;
|
||||
registerModule(pyodide);
|
||||
}
|
||||
|
||||
// load plugins unless specified otherwise
|
||||
const toBeAwaited = [];
|
||||
for (const [key, value] of entries(plugins)) {
|
||||
if (!pyodide.config?.plugins?.includes(`!${key}`))
|
||||
toBeAwaited.push(value());
|
||||
}
|
||||
|
||||
if (toBeAwaited.length) await Promise.all(toBeAwaited);
|
||||
|
||||
// allows plugins to do whatever they want with the element
|
||||
// before regular stuff happens in here
|
||||
for (const callback of hooks.onInterpreterReady)
|
||||
|
||||
4
pyscript.core/src/plugins.js
Normal file
4
pyscript.core/src/plugins.js
Normal file
@@ -0,0 +1,4 @@
|
||||
// ⚠️ This file is an artifact: DO NOT MODIFY
|
||||
export default {
|
||||
error: () => import("./plugins/error.js"),
|
||||
};
|
||||
40
pyscript.core/src/plugins/error.js
Normal file
40
pyscript.core/src/plugins/error.js
Normal file
@@ -0,0 +1,40 @@
|
||||
// PyScript Error Plugin
|
||||
import { hooks } from "../core.js";
|
||||
|
||||
hooks.onBeforeRun.add(function override(pyScript) {
|
||||
// be sure this override happens only once
|
||||
hooks.onBeforeRun.delete(override);
|
||||
|
||||
// trap generic `stderr` to propagate to it regardless
|
||||
const { stderr } = pyScript.io;
|
||||
|
||||
// override it with our own logic
|
||||
pyScript.io.stderr = (...args) => {
|
||||
// grab the message of the first argument (Error)
|
||||
const [{ message }] = args;
|
||||
// show it
|
||||
notify(message);
|
||||
// still let other plugins or PyScript itself do the rest
|
||||
return stderr(...args);
|
||||
};
|
||||
});
|
||||
|
||||
// Error hook utilities
|
||||
|
||||
// Custom function to show notifications
|
||||
function notify(message) {
|
||||
const div = document.createElement("div");
|
||||
div.className = "py-error";
|
||||
div.textContent = message;
|
||||
div.style.cssText = `
|
||||
border: 1px solid red;
|
||||
background: #ffdddd;
|
||||
color: black;
|
||||
font-family: courier, monospace;
|
||||
white-space: pre;
|
||||
overflow-x: auto;
|
||||
padding: 8px;
|
||||
margin-top: 8px;
|
||||
`;
|
||||
document.body.append(div);
|
||||
}
|
||||
Reference in New Issue
Block a user