4

I have simple service stack web service that takes Name as input parameter. From this thread, I understand ResponseStatus property is no longer required when using new API. But when I tried to compose a request with no Name using fiddler, it returns 400 response code as expected but didn't contain any information regarding the exception. So, does new API provide error description out-of-box especially for non .NET clients. If it doesn't, is there possible way to provide this information.

public object Any(CustomerRequest request)
{
   if (request.Name == null)
   {
     throw new ArgumentException("Name is required");
   }

   var objCustomer = //get customer from DB

   return objCustomer;
}

public class CustomerRequest
{
   public string Name {get;set;}
   public bool IsActive {get;set;}
}
Community
  • 1
  • 1
Sunny
  • 4,765
  • 5
  • 37
  • 72
  • What is your service class inheriting from? – mickfold Mar 20 '13 at 18:18
  • class CustomerService : Service – Sunny Mar 20 '13 at 18:28
  • I wrote a quick test project based on the information in the question. I receive an 400 http code and an Error response in the body. A 500 error is for internal server errors so I think there could be a bug in your test project. – mickfold Mar 20 '13 at 19:01
  • I'm sorry, it returns 400 code. And I am testing the service using fiddler customer?format=json, does the response include error status as property. – Sunny Mar 20 '13 at 19:21
  • 1
    Using the fiddler composer or Firefox I get the JSON object: `{"ResponseStatus":{"ErrorCode":"ArgumentException","Message":"Name is required","Errors":[]}}`. For brevity I disabled the stacktrace. – mickfold Mar 20 '13 at 20:12
  • 1
    Also here is a link to the test code http://pastebin.com/vqZSRs2n – mickfold Mar 20 '13 at 20:17
  • When I debug, ArgumentException is thrown but the json is blank. Url is http://localhost:82/api/customer?format=json and GET operation. As mentioned in the OP, response object doesn't have ResponseStatus property. All I receive is blank with status code 400. – Sunny Mar 20 '13 at 22:40
  • But when I add ResponseStatus property to response object, it is getting populated. So, do I need it per new API? – Sunny Mar 20 '13 at 22:46
  • Can you upload your test project to dropbox or similar. I'll have a look at it and try to see what the problem is. – mickfold Mar 21 '13 at 08:45
  • Thanks very much for your followups. As the answer suggested, issue was with response name. It was CustomerResponse, so I had renamed it and it worked just fine. – Sunny Mar 21 '13 at 14:37
  • But I've problem with naming convention now, I've followed same as ServiceStack tests code. Now I am wondering how to name service, request and response. It is not like Todo's example, where they pass in Todo and receive Todo. My application has only GET operations, so request will have no relation with response. Any feedback appreciated. – Sunny Mar 21 '13 at 14:39
  • Ok, I've CustomerService, CustomerRequest, Customer (response), do you guys foresee any problems with these names. – Sunny Mar 21 '13 at 14:49

1 Answers1

3

You shouldn't need to add ResponseStaus with the new API. As per penfold's example, you should get {"ResponseStatus":{"ErrorCode":"ArgumentException","Message":"Name is required","Errors":[]}} as a response.

Do you still have a CustomerResponse class in your codebase? If so, try commenting it out or removing it. Even though you are returning object when an Exception occurs ServiceStack will still look for the conventionally-named Response type (see here) and try to fill the ResponseStatus property.

paaschpa
  • 4,816
  • 11
  • 15
  • Thanks for the catch. Now, I am left with naming convention problem posted in the comments above, can anyone please advise. – Sunny Mar 21 '13 at 14:40
  • Can you be more specific about the 'naming convention problem'? If you return 'object' for your Service(s) the CustomerResponse class (and other 'Response' classes) is dead code so remove it. Do you have a need for the CustomReponse that I'm missing? If so maybe post another question. – paaschpa Mar 21 '13 at 15:59
  • Yes, I am just sending customer as response so that solves the problem. But if I'd to send custom response, then it should also be the same right, I mean populate the customer into the custom object and return it. Since return type is object. – Sunny Mar 21 '13 at 16:06
  • I wish the [wiki](https://github.com/ServiceStack/ServiceStack/wiki/Error-Handling) included this! – Drew Freyling Nov 04 '13 at 23:09