Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add details to error messages to make them more helpful #372

Merged
merged 1 commit into from
Oct 2, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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