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

████████ ██████  ██    ██        █████  ███████ 
   ██    ██   ██  ██  ██        ██   ██ ██      
   ██    ██████    ████   █████ ███████ ███████ 
   ██    ██   ██    ██          ██   ██      ██ 
   ██    ██   ██    ██          ██   ██ ███████ 
    AssemblyScript - v0.2.4
  

📚 Contents

📝 About

This library is an addon for AssemblyScript that brings JavaScript-like exception handling to the language, allowing you to use familiar try/catch syntax with a custom state management system. This allows AssemblyScript developers to write more readable, maintainable code, while retaining the performance benefits of WebAssembly.

💾 Installation

npm install try-as

Add the --transform to your asc command (e.g. in package.json)

--transform try-as/transform

Alternatively, add it to your asconfig.json

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

NOTE: Make sure to load try-as/transform last!

If you'd like to see the code that the transform generates, run the build step with DEBUG=true

🪄 Usage

This library does all the work behind-the-scenes, so you, the developer, can use the classic try/catch/finally syntax with no changes!

try {
  abort("Failed to execute!");
  console.log("This should not execute");
} catch (e) {
  console.log("Got an error: " + e.toString());
} finally {
  console.log("Gracefully shutting down...");
  process.exit(0);
}

🔍 Examples

✅ Type-safe Error Handling

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

try {
  // something dangerous
} catch (e) {
  const err = e as Exception; // Notice we cast to Exception
  if (err.type == ExceptionType.Throw) {
    console.log("Throw: " + err.toString());
  } else if (err.type == ExceptionType.Abort) {
    console.log("Abort: " + err.toString());
  } else if (err.type == ExceptionType.Unreachable) {
    console.log("Unreachable: " + err.toString());
  }
}

⚠️ Working with Custom Errors

import { Exception } from "try-as";

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

try {
  throw new MyError("This is my custom error!");
} catch (e) {
  const err = e as Exception;

  if (err.is<MyError>()) {
    console.log("Caught MyError: " + err.as<MyError>().message);
  } else {
    console.log("Unknown error type");
  }
}

🔁 Re-throwing Errors

Sometimes, you want to catch a certain kind of error, handle it, and re-throw it if needed:

try {
  // something dangerous
} catch (e) {
  const err = e as Exception;

  if (!err.is<MyError>()) {
    console.log("Rethrowing error: " + err.toString());
    err.rethrow();
    // or
    throw err;
  }

  console.log("Got MyError, but handled it gracefully");
}

📃 License

This project is distributed under an open source license. You can view the full license using the following link: License

📫 Contact

Please send all issues to GitHub Issues and to converse, please send me an email at [email protected]

About

Exception Handling for AssemblyScript

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Sponsor this project

 

Packages

No packages published