JairusSW / as-json

The only JSON library you'll need for AssemblyScript. SIMD enabled
MIT License
80 stars 16 forks source link

Improve error handling #58

Open mattjohnsonpint opened 11 months ago

mattjohnsonpint commented 11 months ago

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 https://github.com/AssemblyScript/assemblyscript/issues/302#issuecomment-491933817

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

mattjohnsonpint commented 11 months ago

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

JairusSW commented 11 months ago

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

mattjohnsonpint commented 11 months ago

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

mattjohnsonpint commented 10 months ago

Any update on this? Thanks.

JairusSW commented 6 months ago

@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 commented 6 months ago

Thanks! I'll review soon.

JairusSW commented 6 months ago

@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...");
}
JairusSW commented 5 months ago

TODO

JairusSW commented 5 months ago

Going to be released in v1.0.0