This project is read-only.

Error handling in API

Topics: Web Api
Oct 16, 2011 at 9:02 AM

This post: shows how to trap requests to invalid endpoints. Below is a test service called Hello.

    Public Function [Get](name As String) As HttpResponseMessage

        ' Create the response.
        Dim response = New HttpResponseMessage()
        response.Content = New StringContent(String.Format("Hello, {0}!", name), Encoding.UTF8, "text/plain")
        response.StatusCode = HttpStatusCode.OK
        Return response

    End Function

Accessing it via: http://localhost/some-service/Hello/name=Poo works as expected.
Accessing it via: http://localhost/some-service/Hello/names=Poo gives a generic error "Endpoint not found."

How do I handle it like the other forum post? It may be rather simple, but I'm dim & haven't done touched WCF stuff for months. Any ideas?

Oct 16, 2011 at 10:09 PM

In that post that you referred to, Glenn was setting both the Method and UriTemplate to "*":

 [WebInvoke(Method="*", UriTemplate="*")]

Anyway, in preview 5, you can now use the ErrorHandler, which is the receommend way to do error handling.  Check out this AddErrorHandler HttpConfiguration scenarios:


    public class AlwaysOkErrorHandler : HttpErrorHandler
        protected override bool OnTryProvideResponse(Exception exception, ref HttpResponseMessage message)
          //logic goes here.. such as this always returns 200 in this case
            message = new HttpResponseMessage(HttpStatusCode.OK)
                Content = new StringContent(exception.Message)
            return true;



                var config = new HttpConfiguration
                    // Adding an error handler that always returns 200 with the error message in the body 
                    ErrorHandlers = (handlers, endpoint, descriptions) =>
                        handlers.Add(new AlwaysOkErrorHandler());

Maggie Ying

Oct 16, 2011 at 10:55 PM

Thanks Maggie - I missed those.

Oct 17, 2011 at 1:36 AM

Quick question, how do you add the handler to the ErrorHandlers in the config? I keep getting "Expression does not produce a value" when using the code above?

Oct 17, 2011 at 2:24 AM

Hmm, VB isn't my forte, but is that a compile error you are getting?

Do you have something like this?

New HttpConfiguration() _
         With {.ErrorHandlers = Sub(handlers, endpoint, descriptions)
                          handlers.Add(New HelloWorld.AlwaysOkErrorHandler())
         End Sub}

Oct 17, 2011 at 4:36 AM

Yep, compile error .

Dim config As New WebApiConfiguration()
config.ErrorHandlers = Function(handlers, endpoint, descriptions) {handlers.Add(New EndpointErrorHandler())}
C# isn't my forte ;) Not sure if I borked up the conversion.
Oct 17, 2011 at 5:03 AM

Try this:

        config.ErrorHandlers = Sub(handlers, endpoint, descriptions) handlers.Add(New EndpointErrorHandler())

Maggie Ying

Oct 17, 2011 at 5:51 AM

Thanks - did the trick fine.

Oct 20, 2011 at 6:14 AM

All works, but instead of showing a message, is it possible to just have the default web broswer page? How would I go about that?

Oct 20, 2011 at 6:54 PM

Instead of using a custom error handler, you can simply turn on the IncludeExceptionDetail if you want to see teh default web browser page with the exception call stack:

'config.ErrorHandlers = Sub(handlers, endpoint, descriptions) handlers.Add(New EndpointErrorHandler())
config.IncludeExceptionDetail = True

Maggie Ying