开发者

Handling Error Messages

Recently I had to standarize communication between the client and the server in a rich web application composed mostly of widgets. Responses to the client are in a json format. When it was time to decide about handling error messages two ideas popped up.

  1. Send the errors message part of the response such as

    {"success":"false","errors":["field1":"message1","field2":"message2"]}

  2. Send the errors as a header and simply return false to client

response.addHeader('X-Application-Error','["field1":"message1","field2":"message2"]')

The second option seems to be neat; the user only checks for errors in the headers when they want to, the errors seems this way to be part of the protocol rather than be part of the response.

Is 开发者_如何学编程this a good practice ? Is one way better than the other.


I would send error messages as part of the response.

Adding custom headers is what you'd do if you wanted to report things on the HTTP protocol level; HTTP already has established methods of reporting errors (via HTTP status codes). I would leave anything that's going to be shown to the client (or used by Javascript) in the body of the response.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜