Error handling in API

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

This post: http://wcf.codeplex.com/discussions/257448 shows how to trap requests to invalid endpoints. Below is a test service called Hello.

<WebGet(UriTemplate:="name={name}")>
    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 9: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:

WCFWebApi\Http\Test\Microsoft.ApplicationServer.Http\Scenarios\HttpConfiguration\AddErrorHandler\AlwaysOkErrorHandler.cs

    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;
        }
    }

 

WCFWebApi\Http\Test\Microsoft.ApplicationServer.Http\Scenarios\HttpConfiguration\AddErrorHandler\AddErrorHandler.cs

                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());
                    }
                };

Thanks,
Maggie Ying

Oct 16, 2011 at 9:55 PM

Thanks Maggie - I missed those.

Oct 17, 2011 at 12: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 1: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 3: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 4:03 AM

Try this:

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

Thanks,
Maggie Ying

 
Oct 17, 2011 at 4:51 AM

Thanks - did the trick fine.

Oct 20, 2011 at 5: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 5: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


Thanks,
Maggie Ying