Skip to content

Commit f49d0cc

Browse files
committed
Update error handling
1 parent b206ed0 commit f49d0cc

File tree

1 file changed

+28
-16
lines changed

1 file changed

+28
-16
lines changed

src/theme/book.js

+28-16
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@ function playground_text(playground) {
6060
mac: "Ctrl-Enter"
6161
},
6262
exec: _editor => {
63-
console.log(code_block.classList.contains("wasm"));
6463
if (code_block.classList.contains("wasm")) {
6564
run_wasm_pack_code(playground_block);
6665
} else {
@@ -161,18 +160,16 @@ function playground_text(playground) {
161160

162161
result_block.innerText = "Running...";
163162

164-
prepareSandbox(params).then(src => processHTML(src)).then(html => {
165-
result_block.innerText = "";
166-
var iframe = document.createElement('iframe');
167-
iframe.style.height = "100%";
168-
iframe.style.width = "100%";
169-
iframe.style.padding = 0;
170-
iframe.style.margin = 0;
171-
iframe.style.border = 0;
172-
iframe.src = createObjectURL(html, "text/html");
173-
result_block.appendChild(iframe);
174-
});
163+
prepareSandbox(params)
164+
.then(src => processHTML(src))
165+
.then(html => {
166+
result_block.innerText = "";
167+
var iframe = createIFrame(html);
168+
result_block.appendChild(iframe);
169+
})
170+
.catch(error => result_block.innerText = "Playground Communication: " + error.message);
175171
}
172+
176173
// Greatly inspired from WebAssemblyStudio
177174
async function prepareSandbox(params) {
178175
var wasmResult = fetch_with_timeout("http://127.0.0.1:9999/wasm-pack", {
@@ -184,22 +181,25 @@ function playground_text(playground) {
184181
body: JSON.stringify(params)
185182
})
186183
.then(response => response.json())
187-
.then(({ wasm_js, wasm_bg }) => {
184+
.then(({ wasm_js, wasm_bg, error }) => {
185+
if (error) {
186+
throw new Error(error);
187+
}
188+
188189
return {
189190
wasm_js: atob(wasm_js),
190191
wasm_bg: base64ToByteArray(wasm_bg)
191192
}
192193
})
193-
.catch(error => result_block.innerText = "Playground Communication: " + error.message);
194194

195195
var htmlSrc = fetch(new Request("iframe.html"))
196196
.then(response => response.text());
197197
var jsSrc = fetch(new Request("wasm-entry.mjs"))
198198
.then(response => response.text());
199199

200200
return Promise.all([htmlSrc, jsSrc, wasmResult])
201-
.catch(error => console.log(error));
202201
}
202+
203203
function base64ToByteArray(src) {
204204
var decode = atob(src);
205205
const byteNumbers = new Array(decode.length);
@@ -208,6 +208,7 @@ function playground_text(playground) {
208208
}
209209
return new Uint8Array(byteNumbers);
210210
}
211+
211212
async function processHTML([htmlSrc, jsSrc, { wasm_js, wasm_bg }]) {
212213
var src = rewriteJS(jsSrc, wasm_js, wasm_bg);
213214
var jsBlob = createObjectURL(src, "application/javascript");
@@ -230,11 +231,22 @@ function playground_text(playground) {
230231
})
231232
return src
232233
}
233-
234+
234235
function createObjectURL(src, mime) {
235236
return URL.createObjectURL(new Blob([src], { type: mime }));
236237
}
237238

239+
function createIFrame(src) {
240+
var iframe = document.createElement('iframe');
241+
iframe.style.height = "100%";
242+
iframe.style.width = "100%";
243+
iframe.style.padding = 0;
244+
iframe.style.margin = 0;
245+
iframe.style.border = 0;
246+
iframe.src = createObjectURL(src, "text/html");
247+
return iframe
248+
}
249+
238250
// Syntax highlighting Configuration
239251
hljs.configure({
240252
tabReplace: ' ', // 4 spaces

0 commit comments

Comments
 (0)