Skip to content

Commit

Permalink
Merge pull request #8 from Textalk/dev-exception-message
Browse files Browse the repository at this point in the history
Better exception messages.
  • Loading branch information
fiddur committed Oct 28, 2014
2 parents 013bb5c + 1ad4d46 commit a3f3029
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 34 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
/build/
/vendor/
composer.phar
7 changes: 1 addition & 6 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,8 @@ php:
- 5.4
- 5.3

before_script:
- curl -s http://getcomposer.org/installer | php
- php composer.phar install --dev --no-interaction

script:
- mkdir -p build/logs
- ./vendor/bin/phpunit
- make test

after_script:
- php vendor/bin/coveralls -v
21 changes: 21 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
install: composer.phar
./composer.phar install

update:
./composer.phar update

test: vendor/bin/phpunit build
./vendor/bin/phpunit --strict

composer.phar:
curl -s http://getcomposer.org/installer | php

vendor/bin/phpunit: install

build:
mkdir build

clean:
rm -f composer.phar
rm -fr vendor
rm -fr build
49 changes: 27 additions & 22 deletions lib/Exception.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,29 +40,11 @@ public function getRequestUri() {
return $this->connection->getUri();
}

/// Get the data part of the rpc error.
public function getData() { return $this->request->errorData; }

public function __toString() {
$method = $this->request->method;
$message = $this->getMessage();
$data = $this->request->errorData;

$phpversion = explode('.', phpversion());

$output = "$method: $message";

if (!empty($data)) {
$json_encode_flags = 0;

if (($phpversion[0] === '5' && $phpversion[1] >= 4) || $phpversion[0] > 5) {
$json_encode_flags = JSON_PRETTY_PRINT;
}

$output .= "\n" . json_encode($data, $json_encode_flags);
}

return $output;
}
/// Get the message part of the rpc error.
public function getRpcMessage() { return $this->request->errorMessage; }

//
// Protected
Expand All @@ -80,6 +62,29 @@ public function __construct(\Tivoka\Client\Connection\WebSocket $connection, Req
$this->request = $request;
$this->connection = $connection;

parent::__construct($request->errorMessage, $request->error);
parent::__construct($this->formatMessage(), $request->error);
}

protected function formatMessage() {
$method = $this->request->method;
$message = $this->request->errorMessage;
$data = $this->request->errorData;

$phpversion = explode('.', phpversion());

$output = "$method: $message";

if (!empty($data)) {
$json_encode_flags = 0;

if (($phpversion[0] === '5' && $phpversion[1] >= 4) || $phpversion[0] > 5) {
$json_encode_flags = JSON_PRETTY_PRINT;
}

$output .= "\nError data: " . json_encode($data, $json_encode_flags);
}
$output .= "\nOn request: " . $this->request->request;

return $output;
}
}
14 changes: 8 additions & 6 deletions tests/ExceptionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,20 @@ public function testUnspecifiedExceptionCode() {
$this->assertNotNull($e, 'A Textalk\WebshopClient\Exception should be thrown.');
}

public function testToStringWithData() {
public function testGetMessageWithData() {
try {
$connection = new Connection(array('webshop' => 22222));
$new_order = new ApiInstance('Order', null, $connection);
$new_order->set(array('language' => 'foo')); // Will not validate

// Make an invalid set call.
$connection->Context->set(array('webshop' => 'Bad shop'), true);
}
catch (Textalk\WebshopClient\Exception $e) {
$string = "$e";
$string = $e->getMessage();
$data = $e->getData();

$this->assertRegExp('/^Order.set: /', $string);
$this->assertInternalType('array', $data);
$this->assertRegExp('/^Context.set: Invalid params./', $string);
$this->assertRegExp('/Error data:/', $string);
$this->assertRegExp('/On request:/', $string);
}

$this->assertNotNull($e, 'A Textalk\WebshopClient\Exception should be thrown.');
Expand Down

0 comments on commit a3f3029

Please sign in to comment.