assemblyscript icon indicating copy to clipboard operation
assemblyscript copied to clipboard

Custom error message not working

Open KieranP opened this issue 2 months ago • 1 comments

Bug description

Using either:

class LocationOccupied extends Error {
  constructor(message: string) {
    super(`LocationOccupied(${message})`);
  }
}

OR

class LocationOccupied extends Error {
  constructor(message: string) {
    this.message = `LocationOccupied(${message})`;
  }
}

OR

class LocationOccupied extends Error {
  toString(): string {
    return `LocationOccupied(${this.message})`
  }
}

and calling this:

throw new LocationOccupied(`${x}-${y}`)

I'm not getting a custom message:

› wasmer run build/release.wasm
⠁ Compiling to WebAssembly                                                                                                                                                                                                                                  abort: 0-0 in assembly/world.ts(107:7)

› wasmtime run build/release.wasm
abort: 0-0 in assembly/world.ts(107:7)
Error: failed to run main module `build/release.wasm`

Notice only "0-0" is getting output, which is what is passed into the throw.

The change of the message in the custom error class doesn't apply.

Steps to reproduce

See above

AssemblyScript version

v0.28.9

KieranP avatar Oct 27 '25 04:10 KieranP

I suspect something like throw (new LocationOccupied(...)) would work, due to a quirk/optimization in how throw statements are compiled (most likely to avoid allocating an object just so the program can subsequently abort).

CountBleck avatar Oct 27 '25 14:10 CountBleck