████████ ██████ ██ ██ █████ ███████
██ ██ ██ ██ ██ ██ ██ ██
██ ██████ ████ █████ ███████ ███████
██ ██ ██ ██ ██ ██ ██
██ ██ ██ ██ ██ ██ ███████
AssemblyScript - v0.2.4
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.
npm install try-asAdd the --transform to your asc command (e.g. in package.json)
--transform try-as/transformAlternatively, 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
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);
}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());
}
}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");
}
}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");
}This project is distributed under an open source license. You can view the full license using the following link: License
Please send all issues to GitHub Issues and to converse, please send me an email at [email protected]
- Email: Send me inquiries, questions, or requests at [email protected]
- GitHub: Visit the official GitHub repository Here
- Website: Visit my official website at jairus.dev
- Discord: Converse with me on My Discord or on the AssemblyScript Discord Server