Search code examples
c#odata

Can OData Error objects contain arbitrary properties


I'm working with a new system and building up the error contracts that I'm going to be returning. I'm trying to follow the OData v4 Error Response structure.

Since this is a C# service, it seems like there is a very nice mapping between Exception objects and OData Error Response. However, we're trying to determine if it's okay for an Error Response object to contain additional arbitrary properties. Inner errors are explicitly stated as being allowed to have additional properties so the following is valid:

{
  "error": {
    "code": "BadArgument",
    "message": "Previous passwords may not be reused",
    "target": "password",
    "innererror": {
      "code": "PasswordDoesNotMeetPolicy",
      "minLength": "6",
      "maxLength": "64",
      "minDistinctCharacterTypes": "2",
    }
  }
}

The Data property on an exception maps very well to this, and we can just convert each value on the inner exception into a property on the error. So if a client adds arbitrary properties to the outer exception, is it okay to expose them as properties at the root. For example:

{
  "error": {
    "code": "BadArgument",
    "message": "PasswordDoesNotMeetPolicy",
    "target": "password",
    "minLength": "6",
    "maxLength": "64",
    "minDistinctCharacterTypes": "2",
  }
}

Or is something like this generally considered 'bad form' for OData?


Solution

  • If you add custom properties directly on the error object, you will have a non-standard error response. Code generation tools and 3rd-party clients will not recognize the custom properties.

    The innererror property is an extension point. The spec says: "The contents of this object are service-defined. Usually this object contains information that will help debug the service." Stick with innererror.