redstone-dart / redstone

A metadata driven microframework for Dart.
http://redstone-dart.github.io/redstone
MIT License
342 stars 42 forks source link

Request never finish if error is thrown in async function #83

Closed cgarciae closed 9 years ago

cgarciae commented 9 years ago

Here is a minimal case

@app.Route ('/testErro1')
@Encode()
testError1 () async
{
    var resp = await testError2();

    return resp;
}

@app.Route ('/testErro2')
@Encode()
testError2 () async
{
    throw new app.ErrorResponse(400, {"error" : "invalid id"});

    return new BoolResp()
        ..value = true;
}
cgarciae commented 9 years ago

I created this plugin to handle this

class Catch {

  const Catch();

}

void ErrorCatchPlugin(app.Manager manager) {

    manager.addRouteWrapper(Catch, (metadata, Map<String,String> pathSegments, 
                                    injector, app.Request request, 
                                    app.RouteHandler route) async {

        try
        {
            var result = route(pathSegments, injector, request);

            if (result is Future)
                return await result;
            else
                return result;
        }
        catch (e, s)
        {
            return new Resp()
                ..error = "$e $s";
        }

  }, includeGroups: true);

I love plugins! However, Redstone should handle this by default.

luizmineo commented 9 years ago

Thanks for reporting.

luizmineo commented 9 years ago

I've released v0.5.21 with a fix for this. Please confirm.

cgarciae commented 9 years ago

Worked :)