Skip to content
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
43 changes: 25 additions & 18 deletions lib/PaymentRails/Exception/Standard.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,33 @@
use PaymentRails\Exception;

/**
* Raised when a standard error request is received
*
* @package PaymentRails
* @subpackage Exception
*/
* Raised when a standard error request is received
*
* @package PaymentRails
* @subpackage Exception
*/
class Standard extends Exception
{
protected $errorBody;
protected $errorBody;

public function __construct($errorBody)
{
$message = "";
foreach ($errorBody as $e) {
if (isset($e['field'])) {
$message = $message . $e['code'] . ": " . $e['message'] . " (field: '" . $e['field'] . "') \n";
} else {
$message = $message . $e['code'] . ": " . $e['message'] . "\n";
}
}
$this->message = $message;
}
/**
* @var $errorBody string|array
*/
public function __construct($errorBody)
{
$message = '';
if (is_array($errorBody)) {
foreach($errorBody as $e) {
$message .= "{$e['code']}: {$e['message']}";
if (!empty($e['field'])) {
$message .= " (field: {$e['field']})";
}
$message .= "\n";
}
} elseif (is_string($errorBody)) {
$message = $errorBody;
}
$this->message = $message;
}
}
class_alias('PaymentRails\Exception\Standard', 'PaymentRails_Exception_Standard');
34 changes: 34 additions & 0 deletions tests/Exception/StandardTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

use PHPUnit\Framework\TestCase;
use PaymentRails\Exception\Standard;

class StandardTest extends TestCase
{
public function testExceptionWithString()
{
$this->expectException(Standard::class);
$this->expectExceptionMessage('unknown');
throw new Standard('unknown');
}

public function testExceptionWithArray()
{
$errorBody = array(
array(
'code' => 404,
'message' => 'not found',
'field' => 'text'
),
array(
'code' => 202,
'message' => 'success'
)
);
$this->expectException(Standard::class);
$this->expectExceptionMessage(
"404: not found (field: text)\n202: success\n"
);
throw new Standard($errorBody);
}
}