Skip to content

Commit

Permalink
Merge pull request #372 from recurly/aaron-suarez/add-details-tag
Browse files Browse the repository at this point in the history
Add details to error messages to make them more helpful
  • Loading branch information
bhelx committed Oct 2, 2018
2 parents 2755f26 + 08c59f8 commit 9198e34
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 3 deletions.
15 changes: 15 additions & 0 deletions Tests/Recurly/Client_Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,19 @@ public function testGatweayError() {
}
catch (Recurly_ConnectionError $e) {}
}

// Test that the <details> tag from a 400 response is appended to the message
public function testBadRequestError() {
$this->client->addResponse('POST', '/purchases', 'client/bad-request-400.xml');

try {
$purchase = new Recurly_Purchase();
$purchase->address = 'something unacceptable';

$collection = Recurly_Purchase::invoice($purchase, $this->client);
}
catch(Recurly_Error $e) {
$this->assertEquals($e->getMessage(), "The provided XML was invalid. Details: Unacceptable tags <address>");
}
}
}
9 changes: 9 additions & 0 deletions Tests/fixtures/client/bad-request-400.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
HTTP/1.1 400 Bad Request
Content-Type: application/xml; charset=utf-8

<?xml version="1.0" encoding="UTF-8"?>
<error>
<symbol>invalid_xml</symbol>
<description>The provided XML was invalid.</description>
<details>Unacceptable tags &lt;address&gt;</details>
</error>
7 changes: 5 additions & 2 deletions lib/recurly/errors.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,16 @@ class Recurly_FieldError
var $field;
var $symbol;
var $description;
var $details;

public function __toString() {
if (!empty($this->field) && ($this->__readableField() != 'base')) {
return $this->__readableField() . ' ' . $this->description;
$details = $this->details ? ' Details: ' . $this->details : '';
return $this->__readableField() . ' ' . $this->description . $details;
}
else {
return $this->description;
$details = $this->details ? ' Details: ' . $this->details : '';
return $this->description . $details;
}
}

Expand Down
5 changes: 4 additions & 1 deletion lib/recurly/response.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public function assertValidResponse()
case 0:
throw new Recurly_ConnectionError('An error occurred while connecting to Recurly.');
case 400:
$message = (is_null($error) ? 'Bad API Request' : $error->description);
$message = (is_null($error) ? 'Bad API Request' : (string) $error);
throw new Recurly_Error($message, 0, null, $recurlyCode);
case 401:
throw new Recurly_UnauthorizedError('Your API Key is not authorized to connect to Recurly.');
Expand Down Expand Up @@ -117,6 +117,9 @@ private static function parseErrorNode($node)
case 'description':
$error->description = $node->nodeValue;
break;
case 'details':
$error->details = $node->nodeValue;
break;
}
$node = $node->nextSibling;
}
Expand Down

0 comments on commit 9198e34

Please sign in to comment.