I'm developing a web service (in asp.net) and I would like to have each web method report to whoever called it when an internal error occurs - for example when input validation has failed.
我正在开发一个Web服务(在asp.net中),我想将每个Web方法报告给发生内部错误时调用它的人 - 例如当输入验证失败时。
When I expose my web service with SOAP such errors can be reported by raising a SoapException. But what if I expose my web service with plain POST (aka Http-Post)? Other than returning a 500 Error HTTP status code, is there a standard for reporting errors or raising exceptions in this case?
当我使用SOAP公开我的Web服务时,可以通过引发SoapException来报告错误。但是,如果我使用普通POST(又名Http-Post)公开我的Web服务怎么办?除了返回500错误HTTP状态代码之外,在这种情况下是否有报告错误或引发异常的标准?
2 个解决方案
#1
Change the response object to contain a status filed, and error message (or only error message, and check at the receiver if empty), and set it properly instead of throwing an exception.
更改响应对象以包含状态字段和错误消息(或仅错误消息,并在接收器处检查是否为空),并正确设置它而不是抛出异常。
#2
I've always just returned the 500 status code, along with a textual description of the error. Just make sure it's documented so the client can handle it correctly.
我总是只返回500状态代码,以及错误的文字描述。只需确保它已记录,以便客户端可以正确处理它。
#1
Change the response object to contain a status filed, and error message (or only error message, and check at the receiver if empty), and set it properly instead of throwing an exception.
更改响应对象以包含状态字段和错误消息(或仅错误消息,并在接收器处检查是否为空),并正确设置它而不是抛出异常。
#2
I've always just returned the 500 status code, along with a textual description of the error. Just make sure it's documented so the client can handle it correctly.
我总是只返回500状态代码,以及错误的文字描述。只需确保它已记录,以便客户端可以正确处理它。