as-json
as-json copied to clipboard
Improve error handling
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.
Note, this would be a breaking change, but since you're still in version 0.x
range, I think it's fine.
https://github.com/yjhmelody/as-container
Yes. Returning a Result
object from that library would be acceptable.
Any update on this? Thanks.
@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
Thanks! I'll review soon.
@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...");
}
TODO
Going to be released in v1.0.0