title: REST API error codes return structure
body: <p>I am writing a REST API and I have stumbled upon a problem.  What is the best way to return the validation errors. </p>
  
  <p>Until now I have been returning the error messages dumped into a general error code (let's say bad request for example)</p>
  
  <pre><code>{
      "status": 400,
      "error": {
          "code": 1, // General bad request code
          "message": [
                  "The Key \"a\" is missing",
                  "The Key \"b\" is missing",
                  "The Key \"c\" is missing",
                  "Incorrect Format for field \"y\""
           ]
      }
  
  )
  </code></pre>
  
  <p>I have researched a little more about how should a good API response should look like and I thought of the following options:</p>
  
  <ol>
  <li><p>Stop at the first encountered error and return a response with the specific error code</p>
  
  <pre><code>{
     "status": 400, //Same as the HTTP header returned
     "error" {
          "code": 1, // Specific field validation error code
          "message": "Field \"x\" is missing from the array structure",
          "developer_message": "The request structure must contain the following fields {a,b,c{x,y,z}}",
          "more_info" =&gt; "www.api.com/help/errors/1"
      }
  )
  </code></pre></li>
  <li><p>Parse all the request data and return multiple field validation errors.</p>
  
  <pre><code>{
    "status": 400,
    "error": {
      "code": 1 //General bad Request code
      "message": "Bad Request",
      "developer_message": "Field validation errors."
      "more_info": "www.api.com/help/errors/1",
      "error_details": {
              0: {
                      "code": 2 // Specific field validation error code
                      "message": "Field \"x\" is missing from the array structure",
                      "developer_message": "The request structure must contain the following fields {a,b,c{x,y,z}}",
                      "more_info": "www.api.com/help/errors/2"
                  },
  
              1: {
                      "code": 3 // Specific field validation error code
                      "message": "Incorrect Format for field \"y\"",
                      "developer_message": "The field \"y\" must be in the form of \"Y-m-d\"",
                      "more_info": "www.api.com/help/errors/3"
                 }
                     }
        }
    }
  </code></pre></li>
  </ol>
  
  <p>In my opinion option 2 would be the right way (it gives more useful information to the developers/end users and the server load might be lower(less requests/no need to revalidate valid data/no need to compute the signature and authenticate the user)), but I am wandering what are the best practices, and if there is another way to treat this kind of problems.</p>
  
  <p>Also i think option 1 is still valid if i get a single fatal error in the flow of the script.(not validation errors) </p>
  
  <p>Please note that the code is just a simple array just so it is easier to follow. The response format will be JSON or XML.</p>