Create ODataError for non-success responses - #623
Conversation
|
|
||
| internal static void ValidateErrorCode(string expectedErrorCode, Microsoft.OData.ODataError error) | ||
| { | ||
| if (expectedErrorCode != error.ErrorCode) |
There was a problem hiding this comment.
folks, this line looks like creating additional expectation that error.ErrorCode must be the same as HTTP status code, while according to the OData spec, it can be " language-independent string" (see below for more details)
9.4 Error Response Body
The representation of an error response body is format-specific. It consists at least of the following information:
· code: required non-null, non-empty, language-independent string. Its value is a service-defined error code. This code serves as a sub-status for the HTTP error code specified in the response.
· message: required non-null, non-empty, language-dependent, human-readable string describing the error. The Content-Language header MUST contain the language code from [RFC5646] corresponding to the language in which the value for message is written.
· target: optional nullable, potentially empty string indicating the target of the error, for example, the name of the property in error.
· details: optional, potentially empty collection of structured instances with code, message, and target following the rules above.
· innererror: optional structured instance with service-defined content.
There was a problem hiding this comment.
it is essentially a breaking change! it generates 500 internal server error for this code (suddenly the code that was valid before becomes invalid/not supported)
return NotFound(
new OData.ODataError()
{
ErrorCode = "ErrorEntityNotFound",
Message = "The specified entity was not found."
});
and may prevent clients from upgrading to the latest version.
When we return error messages from the controller e.g
The Error Message is serialized as a string and the response is as follows:
{ "@odata.context":"https://ServiceUri/odata/$metadata#Edm.String", "value":"Error Message" }In this PR, we add Custom methods in the
ODataControllerthat allows us to Create anODataErrorfrom the Error Message hence the response will be Serialized as an Error to achieve the response below:{ "error": { "code": "400", "message": "Error Message" } }