JornWildt / Ramone

A C# client framework for consuming HTTP/REST services
Other
61 stars 11 forks source link

Make error handling easier #38

Closed JornWildt closed 7 years ago

JornWildt commented 7 years ago

It is rather cumbersome to extract data from error response body:

class Error
{
  ... error data ...
}

try
{
  ... do Ramone web stuff ...
}
catch (WebException ex)
{
  if (ex.Response is HttpWebResponse)
  {
    HttpWebResponse response = (HttpWebResponse)ex.Response;

    if (response.ContentType == "... prefered error response format ...")
    {
      try
      {
        using (Response<Error> r = new Response<Error>(response, Session, 0))
        {
          Error err = r.Body;
          ... do stuff with error data ...
        }
      }
      catch (Exception ex2)
      {
        ... decoding failed! Now what? ...
      }
    }
  }
  else
  {
    ... nothing known in the response body ...
  }
}

Can it be done easier? Like this:

class Error
{
  ... error data ...
}

try
{
  ... do Ramone web stuff ...
}
catch (WebException ex)
{
  using (var response = Session.ErrorResponse<Error>(ex))
  {
    Error err = response.Body;
    ... do stuff with error data ...
  }

  // OR
  Session.TryHandleError<Error>(ex,
    err =>
    {
      ... do stuff with error data ...
    });
}