locked
Error Messages in REST services RRS feed

  • Question

  • I am curious to know the best way to implement error handling/error messages in REST based WCF services. I was thinking of having a custom response object which contains the actual payload and the custom error object(errorcode,errordescription). But this will not help the clients when it comes to identifying the type. For e-g,

    1.)WCF method signature public User GetUser(int id);
    2.)Client code would be User obj = proxy.GetUser(1);

    In the above scenario, response object would not help the client as the client would not know the type of the object.

    Another approach would be to add the error code,error description,status as properties of User object itself.

    Do you have any other idea.

    Thanks,
    Ravi

    Monday, December 28, 2009 8:18 AM

All replies

  • Helpful info:

    Windows Communication Foundation (WCF) REST error handling enables you to return errors from WCF REST services that specify an HTTP status code and return error details using the same format as the operation (for example, XML or JSON).

    Link:
    http://msdn.microsoft.com/en-us/library/ee513227(VS.100).aspx

    Regards,
    Jai
    Monday, December 28, 2009 10:01 AM
  • Really not the case. With WCF approach you can define a Fault Contract and then on the client side you will have a try-catch block which will allow you to trap the errors. Another approach is to have a error object as part of the every message response. So you can then write
    User obj = proxy.GetUser(1);
    if (obj.Error != null ) print(obj.Error.Message); print(obj.Error.ErrorCode);

    and the repsonse could be in any form like XML which can be serialized to object.
    Tuesday, December 29, 2009 7:11 AM
  • Really not the case. With WCF approach you can define a Fault Contract and then on the client side you will have a try-catch block which will allow you to trap the errors. Another approach is to have a error object as part of the every message response. So you can then write
    User obj = proxy.GetUser(1);
    if (obj.Error != null ) print(obj.Error.Message); print(obj.Error.ErrorCode);

    and the repsonse could be in any form like XML which can be serialized to object.

    Although I have no experience with REST based services, I completely agree with your assessment that that an error object should be a part of every response.  In all of the serious web services / SOA environments I've seen the response DTO always contains an error object, which is the first thing checked in the response.
    Tuesday, December 29, 2009 2:47 PM