Real-Serious-Games / C-Sharp-Promise

Promises library for C# for management of asynchronous operations.
MIT License
1.2k stars 149 forks source link

Default and custom error handler #106

Closed godhedin closed 4 years ago

godhedin commented 4 years ago

Your have a good feature default catcher If developer had mistake and not realized catcher all exception process in default catcher

 private void Promise_UnhandledException(object sender, ExceptionEventArgs e)

but in this way you can't write custom catch callback in done

public void Done(Action<PromisedT> onResolved, Action<Exception> onRejected)
{
    Then(onResolved, onRejected)
        .Catch(ex =>
            Promise.PropagateUnhandledException(this, ex)
        );
}

default handler will be processed anyway

I think that the behavior should be like this: if there are custom callback handlers then process there and if not, then process in default catcher

public void Done(Action<PromisedT> onResolved, Action<Exception> onRejected)
{
    Then(onResolved, onRejected)

}

and

public void Done(Action<PromisedT> onResolved)
{
    Then(onResolved)
                .Catch(ex =>
                    Promise.PropagateUnhandledException(this, ex)
                );
} 
RoryDungan commented 4 years ago

Having the default rejection handler is necessary even if you specify a custom rejection handler to handle the case where your custom rejection handler threw an exception itself. In the code you mention, if your custom rejection handler handled the exception then the default handler should not be called

    Then(onResolved, onRejected) // onRejected handles rejection
        .Catch(ex =>   // <- this callback should never be executed if the rejection was already handled.
            Promise.PropagateUnhandledException(this, ex)
        );

If that's not happening and Promise.PropagateUnhandeldException is still being called in addition to or instead of your custom onRejected callback, then that is a bug.