Skip to content

Commit

Permalink
Merge pull request #111 from sturents/master
Browse files Browse the repository at this point in the history
Handling bad requests with messages for invalid fields
  • Loading branch information
calcinai committed Nov 21, 2015
2 parents ef881d2 + 4a210d2 commit 6e83e38
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/XeroPHP/Remote/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ public function parse() {
//This catches actual app errors
if(isset($this->root_error)) {
$message = sprintf('%s (%s)', $this->root_error['message'], implode(', ', $this->element_errors));
$message .= $this->parseBadRequest();
throw new BadRequestException($message, $this->root_error['code']);
} else {
throw new BadRequestException();
Expand Down Expand Up @@ -111,6 +112,22 @@ public function parse() {
}
}

/**
* @return string
*/
private function parseBadRequest(){
if (isset($this->elements)){
$field_errors = [];
foreach ($this->elements as $n => $element){
if (isset($element['ValidationErrors'])){
$errors[] = $element['ValidationErrors'][0]['Message'];
}
}
return "\nValidation errors:\n".implode("\n", $errors);
}
return '';
}

public function getResponseBody(){
return $this->response_body;
}
Expand Down

0 comments on commit 6e83e38

Please sign in to comment.