title: Accessing actual server response from Angular $resource
body: <p>I have an API built in Laravel which returns JSON in a format such as this:</p>
  
  <pre><code>{
    "data":{
      "errors":{
        "username":"The username has already been taken.",
        "email":"The email has already been taken."
      }
    },
    "success":true,
    "status":400
  }
  </code></pre>
  
  <p>In this case, I'm trying to create a user with a username and email address which already exists. This is the Angular $resource code I'm using inside my controller for that:</p>
  
  <pre><code>var user = new User({
      username: $scope.user.username,
      email: $scope.user.email,
      password: $scope.user.password
  });
  
  var response = user.$save(function(data) {
      console.log(data);
  }, function(data) {
      if (data.status === 400) {
          angular.forEach(data.data.data.errors, function(error) {
              console.log(error);
          });
      }
  });
  </code></pre>
  
  <p>So what this is doing is sending a POST request to <code>/users</code> on the API and if it comes back with a non-200 status code, it's checking if it's a 400 code which means a validation error and <code>console.log</code>'ing those error messages. Sure enough, I get the error messages output to the console.</p>
  
  <p>What I'm wondering is, if there's a better way to access the error messages than <code>data.data.data.errors</code>. Seen as the API wraps the response data in a <code>data</code> field, and Angular returns the <code>$resource</code> of the request rather than the actual server response, it leads to a rather unsightly amount of properties being used to get the error messages.</p>
  
  <p>Is there a better way to do this?</p>