🎓 A1Learner
🔍
0 of 50 lessons completed

WebAssembly Errors: The Field Guide

WebAssembly errors fall into classes by stage. The class tells you where to look.

Bad bytes give CompileError

Four wrong bytes fail to compile. The catch logs the class.

try {
  const bytes = new Uint8Array([0, 1, 2, 3]);
  await WebAssembly.instantiate(bytes, {});
} catch (error) {
  console.log(error.name);
}

A missing import gives LinkError

The namespace exists but the field is missing.

// bytes holds a module that imports env.missing.
try {
  await WebAssembly.instantiate(bytes, { env: {} });
} catch (error) {
  console.log(error.name);
}

Example

(async () => {
  const bytes = new Uint8Array([0, 1, 2, 3]);
  try {
    await WebAssembly.instantiate(bytes, {});
  } catch (error) {
    console.log(error.name);
  }
})();

Key points

  • TypeError comes from the loading stage, like fetch or MIME.
  • CompileError comes from bad bytes that fail to compile.
  • LinkError comes from imports that fail to link.
  • RuntimeError comes from a trap while the code runs.
  • A missing import namespace gives TypeError, a missing field gives LinkError.
  • Match the class to the stage, then fix that stage.

In this example, the code builds four wrong bytes and tries to instantiate them inside try and catch. The bytes fail to compile, so the catch logs the error name.

Note

this lesson names classes, not full engine messages.

Try it Yourself

Edit the code, then press Run.

Runs in your browser — the preview under the editor is the real result. Nothing is sent to a server. The code runner panel (right) does the same with more room.

Exercise

Bad module bytes fail at which stage?