Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I am designing an application that interacts with a RESTful API.

When returning error messages like Unable to sign up user

Should the error message be generated by the app? or by the API? I am looking for best practices.

share|improve this question
1  
Don't you need both? The API needs to display an error, and the app needs to handle the error. Are you just worried about who generates the error wording? –  Will Eddins Jul 10 '13 at 19:39
    
Yes, I am worried about who generates the error message wording. –  Sheehan Alam Jul 10 '13 at 21:10

3 Answers 3

up vote 12 down vote accepted

At the very least, the REST api needs to return an error code (e.g. 123) and/or an error key (e.g. "cannotRegister") which the application can then pick up in order to display a related message. This also helps when you're dealing with an application that is available in multiple languages. The app can then pick the related message from the appropriate resource bundle (translation). The API doesn't need to know in what language the user is viewing the application.

share|improve this answer
2  
globalization is the end-all reason literation should always be in the UI. Even outside of globalization though, the UI should be the owner of all things that matter only to users and not to logic. –  Jimmy Hoffa Jul 10 '13 at 20:53

I try to handle as many errors as possible by the API. In environments with multiple clients this helps to keep things consistent and easier to maintain.

When performing the error handling via the API the mere status code is defintely not enough. The approach I take here, is to provide an additional error response body that also provides a human readable message as well as a more detailed message for the developer and a link to further resources that provide some extra explanation. In JSON this could look something like this:

{
    "status": 404,
    "code": 404-07,
    "message": "Ooops! Seems like the file does not exist.",
    "developerMessage": "File resource for path /uploads/foobar.txt does not exist.",
    "moreInfo": "http://www.mycompany.com/errors/404-07"
}

Example taken from stormpath.

The clients then use the human readable message and directly present them to the end user.

To handle i18n we use the HTTP Accept-Language and Content-Language headers. The error messages then get delivered in the desired language.

share|improve this answer

Simply put an error page url in your configuration file (web.config if asp.net), that displays the error page in case if an error has occured.

Using the api? never heard of it, but if it is global enough to trigger a message on any error, then may be it is good enough..

Use the app's configuration to trigger a custome error. If you cant use your own code, but put it somewhere that responds to any request , no matter what type of request, otherwise you will have errors slipping by!

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.