From bc36b6f84b1ffca8073ec29ca4be5ed8fb4b65fa Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Fri, 28 Sep 2018 13:54:29 -0700 Subject: [PATCH] Add `catch(console.error)` to all examples Some examples have been failing to load in some browsers, and this ensures that whenever the promise to load Rust code fails we log any errors happening instead of accidentally failing silently. This helped debug a bit in #897 --- examples/add/index.js | 4 +- examples/canvas/index.js | 6 +-- examples/char/index.js | 16 ++++---- examples/closures/index.js | 4 +- examples/console_log/index.js | 4 +- examples/dom/index.js | 4 +- examples/fetch/index.js | 17 +++++---- examples/hello_world/index.js | 4 +- examples/import_js/index.js | 4 +- examples/julia_set/index.js | 7 ++-- examples/paint/index.js | 6 +-- examples/performance/index.js | 4 +- examples/wasm-in-wasm/index.js | 4 +- examples/webaudio/index.js | 70 +++++++++++++++++----------------- examples/webgl/index.js | 6 +-- 15 files changed, 91 insertions(+), 69 deletions(-) diff --git a/examples/add/index.js b/examples/add/index.js index 5d94c1ec..bef9fbcc 100644 --- a/examples/add/index.js +++ b/examples/add/index.js @@ -1,4 +1,6 @@ // For more comments about what's going on here, check out the `hello_world` // example const rust = import('./add'); -rust.then(m => alert('1 + 2 = ' + m.add(1, 2))); +rust + .then(m => alert('1 + 2 = ' + m.add(1, 2))) + .catch(console.error); diff --git a/examples/canvas/index.js b/examples/canvas/index.js index 4372fcf6..3439a276 100644 --- a/examples/canvas/index.js +++ b/examples/canvas/index.js @@ -1,5 +1,5 @@ // For more comments about what's going on here, check out the `hello_world` // example. -import('./canvas').then(canvas => { - canvas.draw(); -}); +import('./canvas') + .then(canvas => canvas.draw()) + .catch(console.error); diff --git a/examples/char/index.js b/examples/char/index.js index bf2b371b..c1d0f4f5 100644 --- a/examples/char/index.js +++ b/examples/char/index.js @@ -4,13 +4,15 @@ let imp = import('./char.js'); let mod; let counters = []; -imp.then(wasm => { - mod = wasm; - addCounter(); - let b = document.getElementById('add-counter'); - if (!b) throw new Error('Unable to find #add-counter'); - b.addEventListener('click', ev => addCounter()); -}); +imp + .then(wasm => { + mod = wasm; + addCounter(); + let b = document.getElementById('add-counter'); + if (!b) throw new Error('Unable to find #add-counter'); + b.addEventListener('click', ev => addCounter()); + }) + .catch(console.error); function addCounter() { let ctr = mod.Counter.new(randomChar(), 0); diff --git a/examples/closures/index.js b/examples/closures/index.js index 6c2133e5..26cd9d48 100644 --- a/examples/closures/index.js +++ b/examples/closures/index.js @@ -1,4 +1,6 @@ // For more comments about what's going on here, check out the `hello_world` // example const rust = import('./closures'); -rust.then(m => m.run()); +rust + .then(m => m.run()) + .catch(console.error); diff --git a/examples/console_log/index.js b/examples/console_log/index.js index 09cab0de..39d597af 100644 --- a/examples/console_log/index.js +++ b/examples/console_log/index.js @@ -2,4 +2,6 @@ // example const rust = import('./console_log'); -rust.then(m => m.run()); +rust + .then(m => m.run()) + .catch(console.error); diff --git a/examples/dom/index.js b/examples/dom/index.js index 61fe7379..2447c51c 100644 --- a/examples/dom/index.js +++ b/examples/dom/index.js @@ -1,4 +1,6 @@ // For more comments about what's going on here, check out the `hello_world` // example const rust = import('./dom'); -rust.then(m => m.run()); +rust + .then(m => m.run()) + .catch(console.error); diff --git a/examples/fetch/index.js b/examples/fetch/index.js index 5cea1ca2..7a525f1c 100644 --- a/examples/fetch/index.js +++ b/examples/fetch/index.js @@ -1,11 +1,12 @@ const rust = import('./fetch'); +rust + .then(m => { + m.run().then((data) => { + console.log(data); -rust.then(m => { - m.run().then((data) => { - console.log(data); - - console.log("The latest commit to the wasm-bindgen %s branch is:", data.name); - console.log("%s, authored by %s <%s>", data.commit.sha, data.commit.commit.author.name, data.commit.commit.author.email); - }) -}); + console.log("The latest commit to the wasm-bindgen %s branch is:", data.name); + console.log("%s, authored by %s <%s>", data.commit.sha, data.commit.commit.author.name, data.commit.commit.author.email); + }) + }) + .catch(console.error); diff --git a/examples/hello_world/index.js b/examples/hello_world/index.js index 488fc468..99946b15 100644 --- a/examples/hello_world/index.js +++ b/examples/hello_world/index.js @@ -3,4 +3,6 @@ // will work here one day as well! const rust = import('./hello_world'); -rust.then(m => m.greet('World!')); +rust + .then(m => m.greet('World!')) + .catch(console.error); diff --git a/examples/import_js/index.js b/examples/import_js/index.js index e41c33aa..5b2ef4ec 100644 --- a/examples/import_js/index.js +++ b/examples/import_js/index.js @@ -2,4 +2,6 @@ // example const rust = import('./import_js'); -rust.then(m => m.run()); +rust + .then(m => m.run()) + .catch(console.error); diff --git a/examples/julia_set/index.js b/examples/julia_set/index.js index a35ad7a8..88104731 100644 --- a/examples/julia_set/index.js +++ b/examples/julia_set/index.js @@ -2,7 +2,7 @@ import('./julia_set') .then(wasm => { const canvas = document.getElementById('drawing'); const ctx = canvas.getContext('2d'); - + const realInput = document.getElementById('real'); const imaginaryInput = document.getElementById('imaginary'); const renderBtn = document.getElementById('render'); @@ -12,6 +12,7 @@ import('./julia_set') const imaginary = parseFloat(imaginaryInput.value) || 0; wasm.draw(ctx, 600, 600, real, imaginary); }); - + wasm.draw(ctx, 600, 600, -0.15, 0.65); - }); + }) + .catch(console.error); diff --git a/examples/paint/index.js b/examples/paint/index.js index 27187919..6d88e810 100644 --- a/examples/paint/index.js +++ b/examples/paint/index.js @@ -1,5 +1,5 @@ // For more comments about what's going on here, check out the `hello_world` // example. -import('./wasm_bindgen_paint').then(paint => { - paint.main(); -}); +import('./wasm_bindgen_paint') + .then(paint => paint.main()) + .catch(console.error); diff --git a/examples/performance/index.js b/examples/performance/index.js index 33b7561a..48e692d7 100644 --- a/examples/performance/index.js +++ b/examples/performance/index.js @@ -1,4 +1,6 @@ // For more comments about what's going on here, check out the `hello_world` // example const rust = import('./performance'); -rust.then(m => m.run()); +rust + .then(m => m.run()) + .catch(console.error); diff --git a/examples/wasm-in-wasm/index.js b/examples/wasm-in-wasm/index.js index e0111692..e68f8a60 100644 --- a/examples/wasm-in-wasm/index.js +++ b/examples/wasm-in-wasm/index.js @@ -1,4 +1,6 @@ // For more comments about what's going on here, check out the `hello_world` // example const rust = import('./wasm_in_wasm'); -rust.then(m => m.run()); +rust + .then(m => m.run()) + .catch(console.error); diff --git a/examples/webaudio/index.js b/examples/webaudio/index.js index 3801959e..db9c821e 100644 --- a/examples/webaudio/index.js +++ b/examples/webaudio/index.js @@ -1,38 +1,40 @@ -import('./webaudio').then(rust_module => { - let fm = null; +import('./webaudio') + .then(rust_module => { + let fm = null; - const play_button = document.getElementById("play"); - play_button.addEventListener("click", event => { - if (fm === null) { - fm = new rust_module.FmOsc(); - fm.set_note(50); - fm.set_fm_frequency(0); - fm.set_fm_amount(0); - fm.set_gain(0.8); - } else { - fm.free(); - fm = null; - } - }); + const play_button = document.getElementById("play"); + play_button.addEventListener("click", event => { + if (fm === null) { + fm = new rust_module.FmOsc(); + fm.set_note(50); + fm.set_fm_frequency(0); + fm.set_fm_amount(0); + fm.set_gain(0.8); + } else { + fm.free(); + fm = null; + } + }); - const primary_slider = document.getElementById("primary_input"); - primary_slider.addEventListener("input", event => { - if (fm) { - fm.set_note(event.target.value); - } - }); + const primary_slider = document.getElementById("primary_input"); + primary_slider.addEventListener("input", event => { + if (fm) { + fm.set_note(event.target.value); + } + }); - const fm_freq = document.getElementById("fm_freq"); - fm_freq.addEventListener("input", event => { - if (fm) { - fm.set_fm_frequency(event.target.value); - } - }); + const fm_freq = document.getElementById("fm_freq"); + fm_freq.addEventListener("input", event => { + if (fm) { + fm.set_fm_frequency(event.target.value); + } + }); - const fm_amount = document.getElementById("fm_amount"); - fm_amount.addEventListener("input", event => { - if (fm) { - fm.set_fm_amount(event.target.value); - } - }); -}); + const fm_amount = document.getElementById("fm_amount"); + fm_amount.addEventListener("input", event => { + if (fm) { + fm.set_fm_amount(event.target.value); + } + }); + }) + .catch(console.error); diff --git a/examples/webgl/index.js b/examples/webgl/index.js index 19a8762c..f717a293 100755 --- a/examples/webgl/index.js +++ b/examples/webgl/index.js @@ -1,5 +1,5 @@ // For more comments about what's going on here, check out the `hello_world` // example. -import('./webgl').then(webgl => { - webgl.draw(); -}); +import('./webgl') + .then(webgl => webgl.draw()) + .catch(console.error);