Response message: Composite message or optional fields
We are working with a set of web services and we're looking for the best option to return errors to the web service's consumer. This is the current response:
Response
- Some data about the server
- Some data about the user
- Some resulting data of executing the transaction
So, we need to return errors too. these are our options:
Composite message
we'll return two kinds of responses depending if the transaction was approved or had an error:
First:
- type identifier (this message is serialized. so I need to know which kind of message I'm dealing with, to deserialize the last part)
- Some data about the server
- Some data about the user
- Some resulting data of executing the transaction
Second:
- type identifier (this message is serialized. so I need to know which kind of message I'm dealing with, to deserialize the last part)
- Some data about the server
- Some data about the user
- The errors
Optional fields
the transaction data and error fields will be optional. if there's no errors I will know it was approved.
- Some data about the server
- Some data about the user
- Some resu开发者_运维百科lting data of executing the transaction
- The errors
Which option is more appropriate?
This is discutable and more of a personal opinion than a best practice.
My personal favor is to use the Optional fields, because the error code is possible outcome of an operation. I would expect the client to always first check the (optional) error properties of the returned result before parsing the results. This allows to also return non-fatal errors and partial results together. Exclusive makes it so ... exclusive. Optional is more flexible.
精彩评论