Is HttpErrorHandler supported?

Topics: Web Api
Apr 25, 2011 at 1:41 PM
Edited Apr 25, 2011 at 1:42 PM

I am doing something like

public class MyHttpErrorHandler : HttpErrorHandler
    protected override bool OnHandleError(Exception error)
        return error is MyException;

    protected override HttpResponseMessage OnProvideResponse(Exception error)
        var response = new HttpResponseMessage(HttpStatusCode.BadRequest, "BadRequest")
                           Content =  new StringContent(content)
 return response; } }

But I get a 500 Internal server error exception always even if the exception is handled by this artifact.
I don't have any other exception except for the one i am throwing from my code.

Thank you.

Apr 25, 2011 at 4:23 PM

It's not working right. I filed a bug already. It will work though :-)

Apr 25, 2011 at 6:19 PM

Thank you. On the other hand, I think it will be useful to be able to register multiples ErrorHandler for an EndPoint.I wrote an ticket for that.

May 18, 2011 at 5:57 PM

Is there any hack (or extra code) that can be applied to get this functionality working?

May 18, 2011 at 6:21 PM

well.. i'm using a castle interceptor for now:

public class RestBucksHttpErrorHandler : IInterceptor
        public void Intercept(IInvocation invocation)
            }catch(InvalidOrderStatusException ex)
                invocation.ReturnValue = new HttpResponseMessage(HttpStatusCode.InternalServerError, "InternalServerError") { Content =  new StringContent(ex.Message) };

Jul 8, 2011 at 1:45 PM

What's the status of this?  Seems like a very basic feature and blocking issue for any production deployment.  I looked in the Issue Tracking and I did not find an issue for this.  If there is one please point me to it so I can vote it up.  Or should I create one?

I just got the latest from NuGet and added a custom error handler like the one in jfromaniello's original post.  OnProvideResponse is invoked as expected, but has no effect, as shown by this cURL.  Besides not returning the correct response, the media type is incorrect -- I asked for json.

C:\Program Files\Console2>curl -XPOST -v -H "Accept:application/json" http://localhost:59253/recognize
* About to connect() to localhost port 59253 (#0)
*   Trying connected
* Connected to localhost ( port 59253 (#0)
> POST /recognize HTTP/1.1
> User-Agent: curl/7.21.6 (i386-pc-win32) libcurl/7.21.6 zlib/1.2.5
> Host: localhost:59253
> Accept:application/json
< HTTP/1.1 500 Internal Server Error
< Server: ASP.NET Development Server/
< Date: Fri, 08 Jul 2011 13:35:10 GMT
< X-AspNet-Version: 4.0.30319
< Content-Length: 1647
< Cache-Control: private
< Content-Type: text/html; charset=utf-8
< Connection: Close
&#8745;&#9559;&#9488;<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
<html xmlns="">
    <title>Request Error</title>
    <style>BODY { color: #000000; background-color: white; font-family: Verdana; margin-left: 0px; margin-top: 0px; } #content { margin-left: 30px; font-size: .70em; padding-bottom: 2em; } A:link { color: #336699; font-weight: bold; text-decoration: underline; } A:visited { color: #6699cc; font-weight: bold; text-decoration: underline; } A:active { color: #336699; font-weight: bold; text-decoration: underline; } .heading1 { background-color: #003366; border-bottom: #336699 6px solid; color: #ffffff; font-family: Tahoma; font-size: 26px; font-weight: normal;margin: 0em 0em 10px -20px; padding-bottom: 8px; padding-left: 30px;padding-top: 16px;} pre { font-size:small; background-color: #e5e5cc; padding: 5px; font-family: Courier New; margin-top: 0px; border: 1px #f0f0e0 solid; white-space: pre-wrap; white-space: -pre-wrap; word-wrap: break-word; } table { border-collapse: collapse; border-spacing: 0px; font-family: Verdana;} table th { border-right: 2px white solid; border-bottom: 2px white solid; font-weight: bold; background-color: #cecf9c;} table td { border-right: 2px white solid; border-bottom: 2px white solid; background-color: #e5e5cc;}</style>
    <div id="content">
      <p class="heading1">Request Error</p>
      <p>The server encountered an error processing the request. See server logs for more details.</p>
</html>* Closing connection #0

Jul 8, 2011 at 2:06 PM

We are working on it. It is on the list of things we are currently fixing. Agreed that it is important.


Jul 8, 2011 at 2:07 PM

Great.  Thanks.

Jul 8, 2011 at 2:31 PM

I noticed something else that might be helpful in fixing this.  I added the following to OnProvideResponse:

if (error is HttpResponseException)
    return ((HttpResponseException) error).Response;

When I break on the return I can see that the response code is 401 (Unauthorized).  But the client receives 301 (Found).

(Not sure if I should post these types of observations here or if there is an Issue I can add comments to?)

Jul 8, 2011 at 2:36 PM

Okay, never mind about this.  First of all the code was 302, not 301.  The reason is not a mystery and not a bug.  My web.config specified FormsAuth so it was redirecting to login, which is normal forms auth behavior.

Jul 8, 2011 at 2:39 PM


The known issue is that plugging in your own custom error handler which modifies the response does not seem to work. Obviously that is not the way it is intended to work ;-)

Jul 8, 2011 at 2:45 PM

I was asking about process.  Should conversation should move from Discussions to the actual issue (in the Issue Tracker) once it is created?  Whatever is most convenient for the dev team.

Jul 8, 2011 at 2:46 PM

Aaah right forgot that. There should be a workitem on this, if not you can certainly add one. 

Jul 8, 2011 at 3:08 PM

Removing Forms Auth has completely solved this issue for me.  Makes sense because FormsAuth was developed for webforms websites.  It sits further down the pipeline and decides that most errors should be transformed to 500.  That's what it looks like.

I don't know if you plan to support FormsAuth in Web API, but it seems to me ill suited for web services.  I consider this "no error"

Jul 8, 2011 at 6:24 PM

Except I guess it's still a problem that when HttpErrorHandler is invoked, the Accept header of the request is ignored and media type of the response is always text/html.

Jul 8, 2011 at 6:39 PM

In my Restbucks example i used AOP from the container, something like this:


public class RestBucksHttpErrorHandler : IInterceptor
    public void Intercept(IInvocation invocation)
        }catch(InvalidOrderOperationException ex)
            invocation.ReturnValue = Responses.BadRequest(content: ex.Message);


                       .Where(t => Attribute.IsDefined(t, typeof (ServiceContractAttribute)))
                       .Configure(c => c.Interceptors(new[] { typeof(RestBucksHttpErrorHandler) }).LifeStyle.Transient));


It is just a workarround until this get fixed, but it works for me.

Jul 28, 2011 at 7:18 PM

Vote up this issue: