开发者

Error handling in web service

How do you communicate an error to the web service consumer ?

For example, my web service has a function to insert employee into db.Suppose while inserting the data , the database gave an error,what should be the best way to inform the user about the error.

One way is to depict it through the return value of the web service method but what to do when the function is supposed to return a complex object like employee when there is no error ,and w开发者_C百科ill send an error string when there is an error ?


For SOAP: That is what faults are used for

SOAP faults are used to carry error information within a SOAP message. The fault is for SOAP what an exception is for a programming language.

When your client's request succeeds, you send back a valid response with the Employee structure, when it fails, you send back a fault with details of what went wrong.

For REST: Use HTTP Error Codes

Unlike SOAP, REST web services do not have a convention for returning errors but the simplest is to use the ones everybody understands.

For example you might send a HTTP 404 Not Found when a record is missing, a HTTP 500 Server Error when something happend on the database etc, and return HTTP 200 OK with your result when everything is fine and dandy.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜