Skip to content

Commit

Permalink
Fix #26: V2 validation message
Browse files Browse the repository at this point in the history
  • Loading branch information
fean committed Feb 5, 2021
1 parent 717d944 commit 3831f2a
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 7 deletions.
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.2.1
2.0.1
17 changes: 13 additions & 4 deletions lib/Exception/ServerValidationException.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Trunkrs\SDK\Exception;

use Throwable;
use Trunkrs\SDK\Settings;

class ServerValidationException extends \Exception {
private $_message;
Expand All @@ -13,9 +13,18 @@ public function __construct(array $response)
? json_decode($response["body"])
: null;

$this->_message = $json
? $json->message
: "No validation message specified.";
switch (Settings::$apiVersion) {
case 1:
$this->_message = $json
? $json->message
: "No validation message specified.";
break;
case 2:
$this->_message = $json
? $json->reason
: "No validation message specified.";
break;
}

parent::__construct(
sprintf("Your payload did not match the expectation of the Trunkrs API\n\nValidation: %s", $this->_message)
Expand Down
2 changes: 1 addition & 1 deletion lib/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class Settings {
/**
* @var string The current version of the SDK.
*/
public static $sdkVersion = '2.0.0';
public static $sdkVersion = '2.0.1';

/**
* Sets the client credentials that will be used in subsequent requests.
Expand Down
17 changes: 16 additions & 1 deletion tests/SDK/RequestHandler/GeneralTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ public function testAppliesSDKUserAgent() {
RequestHandler::get("shipments");
}

public function testApiValidationMessageInException() {
public function testApiValidationMessageV1InException() {
Settings::setApiVersion(1);
$message = uniqid();
$this->mockResponse(422, [
'message' => $message,
Expand All @@ -74,4 +75,18 @@ public function testApiValidationMessageInException() {
$this->assertEquals($message, $exception->getValidationMessage());
}
}

public function testApiValidationMessageV2InException() {
$message = uniqid();
Settings::setApiVersion(2);
$this->mockResponse(422, [
'reason' => $message,
]);

try {
RequestHandler::post("shipments", []);
} catch (ServerValidationException $exception) {
$this->assertEquals($message, $exception->getValidationMessage());
}
}
}

0 comments on commit 3831f2a

Please sign in to comment.