0

I'm developing web application and here is how I handle exceptions:

void Application_Error(object sender, EventArgs e)
{
    Exception ex = Server.GetLastError();
    LogManager.GetCurrentClassLogger().Error(ex);
    Response.Clear();
    Server.ClearError();

    HttpException httpEx = ex as HttpException;

    if (httpEx == null)
        httpEx = new HttpException(400, ex.Message, ex);

    RouteData routeData = new RouteData();
    routeData.Values.Add("controller", "Error");
    routeData.Values.Add("action", "Handler");
    routeData.Values.Add("exception", httpEx);
    Response.TrySkipIisCustomErrors = true;
    var rc = new RequestContext(new HttpContextWrapper(Context), routeData);
    var c = ControllerBuilder.Current.GetControllerFactory().CreateController(rc, "Error");
    c.Execute(rc);
}

When an Exception occurs (ex: throw new ArgumentException("Generator with given Id does not exist.");) user receive Error view with details about what happened.

The problem is, the error message is not sent to the user in HttpResponseMessage (as ReasonPhrase or anything else).

private async void DeleteGenerator(Guid id)
{
    var response = await dataService.RemoveGenerator(id);
    if ((int)response.StatusCode >= 300)
        MessageBox.Show( /* Error message from response */ );  
}

Here I should receive box with "Generator with given [...]" in it, but I have no clue how to achieve that. I've tried this but the "HttpError" is missing, this but I really don't know how to implement it to my code (how to send actual HttpResponseMessage by Application_Error) and this but again I have no clue how it should be changed.


EDIT

This is my regular error handler controller:

public ActionResult Handler(HttpException exception)
{
    Response.ContentType = "text/html";
    if (exception != null)
    {
        Response.StatusCode = exception.GetHttpCode();
        ViewBag.StatusString = (HttpStatusCode)exception.GetHttpCode();
        return View("Handler", exception);
    }
    return View("Internal");
}

And I've tried this for test purposes, and it's also not working (the client receive HttpResponseMessage with "Bad Request" as ReasonPhrase.

public HttpResponseMessage Handler(HttpException exception)
{
    Response.ContentType = "text/html";
    if (exception != null)
    {
        return new HttpResponseMessage
        {
            Content = new StringContent("[]", new UTF8Encoding(), "application/json"),
            StatusCode = HttpStatusCode.NotFound,
            ReasonPhrase = "TEST"
        };
    }
    return null;
}
erexo
  • 503
  • 1
  • 6
  • 24
  • have you set breakpoints and checked where along the way the message is dropped or if its ever generated at all? – MaCron Aug 28 '17 at 17:48
  • The problem is, the message is propably not sent at all. I've look all through HttpStatusResponse and did not find the message. I guess it's becouse HttpException is simply not sending it in a way I handle it – erexo Aug 28 '17 at 19:58
  • You cannot use `MessageBox.Show` in a web application. That messagebox would show on the server, where there is no-one to see or dismiss it. (It may seem to work during development, because your machine is both server and client) – Hans Kesting Aug 29 '17 at 08:03
  • I'm not using MessageBox on the web, thats my desktop WPF application that send request to the actual ASP.NET application. And the box is only for debug purposes, the main topic here is how to retrieve exception message from ASP.NET application using HttpClient – erexo Aug 29 '17 at 08:52

1 Answers1

0

So in my Controller that is responsible of showing the error view (which is created and triggered in Application_Error method) I've added the StatusDescription into the Response. It's not ideal and it override the actual StatusCode string, but it works.

public class ErrorController : Controller
{
    public ActionResult Handler(HttpException exception)
    {
        Response.ContentType = "text/html";
        if (exception != null)
        {
            Response.StatusCode = exception.GetHttpCode();
            Response.StatusDescription = exception.Message;
            ViewBag.StatusString = (HttpStatusCode)exception.GetHttpCode();

            return View("Handler", exception);
        }

        return View("Internal");
    }
}
erexo
  • 503
  • 1
  • 6
  • 24