as-json icon indicating copy to clipboard operation
as-json copied to clipboard

Improve error handling

Open mattjohnsonpint opened this issue 1 year ago • 7 comments

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 avatar Dec 06 '23 20:12 mattjohnsonpint

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

mattjohnsonpint avatar Dec 06 '23 20:12 mattjohnsonpint

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

JairusSW avatar Dec 07 '23 02:12 JairusSW

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

mattjohnsonpint avatar Dec 07 '23 17:12 mattjohnsonpint

Any update on this? Thanks.

mattjohnsonpint avatar Jan 04 '24 23:01 mattjohnsonpint

@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

JairusSW avatar May 14 '24 14:05 JairusSW

Thanks! I'll review soon.

mattjohnsonpint avatar May 14 '24 14:05 mattjohnsonpint

@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 avatar May 18 '24 04:05 JairusSW

TODO

JairusSW avatar Jun 17 '24 23:06 JairusSW

Going to be released in v1.0.0

JairusSW avatar Jun 19 '24 20:06 JairusSW