Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve error handling #58

Open
mattjohnsonpint opened this issue Dec 6, 2023 · 7 comments
Open

Improve error handling #58

mattjohnsonpint opened this issue Dec 6, 2023 · 7 comments

Comments

@mattjohnsonpint
Copy link
Contributor

If serialization or deserialization fails, this library throws an error.

In other languages this would be fine, because you could use try/catch to handle the error. Unfortunately, AssemblyScript doesn't yet support try/catch.

Even worse, if you throw an error, the AssemblyScript compiler calls abort, then writes a wasm unreachable statement. That means even if you override abort, you are still screwed because memory can become corrupted if you continue to use a module after an unreachable. See AssemblyScript/assemblyscript#302 (comment)

Instead of throwing, can you please return null, or perhaps return an Optional or Result type as a wrapper, etc.

@mattjohnsonpint
Copy link
Contributor Author

Note, this would be a breaking change, but since you're still in version 0.x range, I think it's fine.

@JairusSW
Copy link
Owner

JairusSW commented Dec 7, 2023

https://github.com/yjhmelody/as-container

@mattjohnsonpint
Copy link
Contributor Author

mattjohnsonpint commented Dec 7, 2023

Yes. Returning a Result object from that library would be acceptable.

@mattjohnsonpint
Copy link
Contributor Author

Any update on this? Thanks.

@JairusSW
Copy link
Owner

@mattjohnsonpint I made a near zero-cost class to handle errors. Its inspired by Rust's Result
https://github.com/JairusSW/as-json/blob/develop/assembly/product.ts

Most of the work on this is happening in the develop branch, but I can move the error handling over to master

@mattjohnsonpint
Copy link
Contributor Author

Thanks! I'll review soon.

@JairusSW
Copy link
Owner

JairusSW commented May 18, 2024

@mattjohnsonpint I think this will be a better option
https://github.com/JairusSW/as-try
I'll expand on the library eventually. It'll have the option to wrap anything in a Result<T>, manage and handle unreachable() instructions, and delegate throws and aborts to the host as an option

To try it, just add the transform to your asc command and use your normal try/catch syntax.
I still need to support throw. abort is supported atm.

try {
    // Do something
    abort("Failed to execute!");
} catch (e) {
    console.log("Got an error: " + e);
} finally {
    console.log("Gracefully shutting down...");
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants