Thanks to visit codestin.com
Credit goes to GitHub.com

Skip to content

Exception Handling for AssemblyScript

License

Notifications You must be signed in to change notification settings

JairusSW/try-as

Repository files navigation

╔╦╗╦═╗╦ ╦  ╔═╗╔═╗
 ║ ╠╦╝╚╦╝══╠═╣╚═╗
 ╩ ╩╚═ ╩   ╩ ╩╚═╝

Table of Contents

Installation

npm install try-as

Add the transform to your asc build and load it last.

asc assembly/index.ts --transform try-as/transform

Or in asconfig.json:

{
  "options": {
    "transform": ["try-as/transform"]
  }
}

If you use multiple transforms, keep try-as/transform last.

Usage

try-as rewrites try/catch/finally, throw, abort, and selected stdlib throw paths so they can be handled through a consistent Exception object.

import { Exception } from "try-as";

try {
  throw new Error("boom");
} catch (e) {
  const err = e as Exception;
  console.log(err.toString()); // Error: boom
} finally {
  console.log("done");
}

Exception API

import { Exception, ExceptionType } from "try-as";
  • Exception.type: ExceptionType
  • Exception.toString(): string
  • Exception.is<T>(): bool
  • Exception.as<T>(): T
  • Exception.clone(): Exception
  • Exception.rethrow(): void

ExceptionType:

  • None
  • Abort
  • Throw
  • Unreachable

Examples

Catch abort and throw

import { Exception, ExceptionType } from "try-as";

try {
  abort("fatal");
} catch (e) {
  const err = e as Exception;
  if (err.type == ExceptionType.Abort) {
    console.log(err.toString()); // abort: fatal
  }
}

Type-safe custom errors

import { Exception } from "try-as";

class MyError extends Error {
  constructor(message: string) {
    super(message);
  }
}

try {
  throw new MyError("typed");
} catch (e) {
  const err = e as Exception;
  if (err.is<MyError>()) {
    const typed = err.as<MyError>();
    console.log(typed.message);
  }
}

Rethrow behavior

import { Exception } from "try-as";

try {
  // risky code
} catch (e) {
  const err = e as Exception;
  if (!err.is<Error>()) {
    err.rethrow();
  }
}

Catching stdlib exceptions

Stdlib exceptions such as missing map keys, empty array pops, out-of-range string access, and malformed URI decode errors are catchable.

import { Exception } from "try-as";

try {
  new Map<string, string>().get("missing");
} catch (e) {
  const err = e as Exception;
  console.log(err.toString()); // Error: Key does not exist
}

Limitations

  • Runtime/internal trap paths are intentionally not rewritten.
  • Exceptions from these internals are not catchable by try-as:
    • ~lib/rt
    • ~lib/shared
    • ~lib/wasi_
    • ~lib/performance
  • This library handles transformed throw/abort flows, not low-level Wasm traps like out-of-bounds memory faults.

Debugging

  • DEBUG=1 enables transform diagnostics.
  • WRITE=pathA,pathB writes transformed source snapshots as *.tmp.ts.

Example:

DEBUG=1 WRITE=./assembly/test.ts,~lib/map asc assembly/test.ts --transform try-as/transform

Contributing

npm run build:transform
npm test
npm run format

License

This project is distributed under the MIT license.

Contact

About

Exception Handling for AssemblyScript

Topics

Resources

License

Stars

Watchers

Forks

Sponsor this project

 

Packages

No packages published