haxetink / tink_await

Haxe async/await
MIT License
58 stars 15 forks source link

Not sure how error handling works #26

Closed andyli closed 5 years ago

andyli commented 5 years ago

Tested with Haxe 3.4.7 and tink_await 0.4.0.

import tink.core.Future;
import tink.core.Outcome;

@await class Test {
    static function test():Surprise<String, String> {
        return Surprise.async(function(resolve){
            resolve(Failure("failed")); //or throw "failed";
        });
    }
    @async static function main() {
        try {
            var v = @await test();
            trace("v: " + v);
        } catch(err:Dynamic) {
            trace("err: " + err);
        }
    }
}

The program outputs err: null but I expect it to be err: failed.

kevinresol commented 5 years ago

https://github.com/haxetink/tink_await/pull/17

A few things here:

  1. Maybe we need better support for Surprise<T, NonError>. Because we currently use Promise as the return type, where Promise<T> = Surprise<T, Error>;
  2. tink_await now expects the actual error content embed in tink.core.Error#data, so the transformed code actually tries to access "failed".data which gives you null (undefined)

A quick fix looks like this:

using tink.CoreApi;

@await class Main {
    static function test():Promise<String> {
        return Surprise.async(function(resolve){
            resolve(Failure(Error.withData('', 'failed')));
        });
    }
    @async static function main() {
        try {
            var v = @await test();
            trace("v: " + v);
        } catch(err:Dynamic) {
            trace("err: " + err);
        }
    }
}
kevinresol commented 5 years ago

Please try git head

andyli commented 5 years ago

Confirm it works as expected, thanks for the quick fix!