Problems catching exception from http flurl client

805 Views Asked by At

I have a rest api with .net5, which has a global exception filter, and returns an ErrorMessage object in case of exceptions. I call the api from a WinForms .net framework 4.8 application and the badrequests from the server, from the client side I get a System.AggregateException exception and according to the flurl documentation it should be FlurlHttpException. I tried to make calls with HttpClient, and everything works as expected, but I find it easier to use flurl, so I want to find a solution to the problem, if someone has any idea how to do it and wants to share it would be great

        try
        {
            var result = (serverUrl)
                         .AppendPathSegment(endPoit)
                         .PostJsonAsync(new { Email = email, Password = password }).Result;
            if (result.ResponseMessage.IsSuccessStatusCode)
            {
                var aut = result.GetJsonAsync<Autorizacion>().Result;
            }
        }
        catch (FlurlHttpException ex)
        {
            var error = ex.GetResponseJsonAsync<ErrorMessage>();
        }
1

There are 1 best solutions below

3
On BEST ANSWER

Firstly, prefer using async await instead of blocking Result call. But if you have to go with blocking calls then use GetAwaiter().GetResult() to get the unwrapped exception.