Search code examples
c#exceptiongithubaggregateexceptionoctokit.net

Strange try/catch block behavior


I am working with Github API via Octokit.net. Now I am writing code that responsible for TwoFactorAuth. When I send request for token, if it is 2FA account, than I must get "TwoFactorRequiredException". I am trying to catch it, but insteand of it I get "AggregateException". But when I look in InnerExceptions of Agg..Exc.. then I have only one, and it is "TwoFactorRequiredException". Here is my code:

try
{
    return client.Authorization.Create(newAuthorization).Result.Token;
}
catch ( Octokit.TwoFactorRequiredException )
{
    _navigationService.NavigateAsync($"{nameof(TwoFactorAuthPage)}", animated: false);
}
//catch ( AggregateException ex )
//{
//    foreach ( var exception in ex.InnerExceptions )
//    {
//        Debug.WriteLine(exception.GetType());
//    }
//}

When it is commented, then I have AggregateExceptions, when it is uncommented, then I have AggExc and inside it I have TwoFactor..Exception.


Solution

  • That's because client.Authorization.Create(newAuthorization) returns a Task<T>, where T is your result type. Inside a Task, multiple exceptions can occur, that is why the Task Parallel Library returns an AggregateException capturing all the exceptions.

    Instead of synchronously blocking on an async method, you should await on it, which asynchronously waits for it's completion. You gain two things from this. The first is that you don't synchronously block and can free the thread, and the second is that await unwraps the exception for you:

    public async Task<Token> CallSomethingAsync()
    {
        try
        {
            var result = await client.Authorization.Create(newAuthorization);
            result.Token;
        }
        catch ( Octokit.TwoFactorRequiredException )
        {
            _navigationService.NavigateAsync($"{nameof(TwoFactorAuthPage)}", animated: false);
        }
    }
    

    Although I'm not sure what NavigateAsync does, I think you may also want to await on it's completion as well.