Catching errors in Messagehandler

Asked

Viewed 65 times

2

Hello I am trying to deal with errors in my application by making use of DelegatingHandler. But I have a problem, I used a try-catch to deal with errors. The try-catch encompasses the SendAsync, happens that when launching any exception of the Webapi Controller it is treated somewhere that I do not know. It returns me only the status 500 in Handler.

Example: Responsehandler.Cs

public class ResponseHandler : DelegatingHandler
{
   protected async override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
   {
        HttpResponseMessage response = null;
        try
        {
              response = await base.SendAsync(request, cancellationToken);
        }
        catch(MinhaException ex)
        {
             // faz algo
        }
        catch(MinhaException2 ex)
        {
             // faz algo
        }
        catch(MinhaException3 ex)
        {
             // faz algo
        }
        catch(Exception ex)
        {
             // faz algo
        }

        return response;
   }
}

Example: Meucontroller.Cs

public class MeuController : ApiController
{
   [HttpGet]
   public string TesteErrorHandler(int erro)
   {
        switch(erro)
        {
              case 1:
                 throw new MinhaException("Erro 1");
              case 2:
                 throw new MinhaException1("Erro 2");
              case 3:
                 throw new MinhaException2("Erro 3");
              case 4:
                 throw new MinhaException3("Erro 4");
        }
        return "Sem erros..";
   }
}

Where is this exception being dealt with? It does not fall into my blocks try-catch of MessageHandler for what reason? How could I circumvent to arrive at the expected result in that way, or in a similar way.

  • 2

    You’re learning that the exception is the most poorly used programming mechanism today, far worse than the goto that people speak badly, precisely why you are seeing now, she is a goto that you don’t know where you’re going, especially in asynchronous code.

  • I understood @Maniero that boring situation, it seems that it is treated in some layer of Webapi before reaching the Messagehandlers. I do not know if the treatment I wish to perform is correct by making use of Handlers, would indicate me an outline solution so that I achieve an equivalent result?

  • Use no exception. I have very little experience with code async, I only made the rice with beans, may have a solution with exception for this, but I find it easier to report errors otherwise as I speak in https://answall.com/q/21767/101

  • I read the reply @Maniero, congratulations, very well explained! My post was an example, today the application I’m working on is already implemented, it is huge and continues to grow. Controllers are passed to rule classes and rule classes throw exceptions to business rules that are violated (and controllers have their Try-catch). The treatments in the controllers are the same for all cases, which generates an over head of over 40 thousand unnecessary lines of code. My goal with this solution is to decrease this over head.

  • There I do not speak so directly ,but exception to business rule is simply a mistake. If something is equal create an abstraction that generalizes it. If you do it right without using an exception it has to be about the same size. That’s where the developer’s work comes in, analyze the problem and find the best solution, this is already clear that is problematic. I don’t even know if this async should exist, it seems so, but I’m not sure, this is the second most misused feature of programming today. Before he existed everyone was well without.

1 answer

0

Hello, I would do something like that, see if it helps you

 public class ResponseHandler : DelegatingHandler
    {
       private static readonly ILog _logger = LogManager.GetLogger(typeof(ResponseHandler));

       protected async override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
       {
            HttpResponseMessage response = null;
            try
            {
                  response = await base.SendAsync(request, cancellationToken);
            }
            catch(MinhaException ex)
            {
                 _logger.Error("seu erro": {0}");
                throw;
            }
            catch(MinhaException2 ex)
            {
                _logger.Error("seu erro": {0}");
                throw;
            }
            catch(MinhaException3 ex)
            {
                 _logger.Error("seu erro": {0}");
                throw;`insira o código aqui`
            }
            catch(Exception ex)
            {
                _logger.Error("seu erro": {0}");
                throw;
            }
    
            return response;
       }
    }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.