Skip to content

Commit a3f3029

Browse files
committed
Merge pull request #8 from Textalk/dev-exception-message
Better exception messages.
2 parents 013bb5c + 1ad4d46 commit a3f3029

File tree

5 files changed

+58
-34
lines changed

5 files changed

+58
-34
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
/build/
22
/vendor/
3+
composer.phar

.travis.yml

+1-6
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,8 @@ php:
55
- 5.4
66
- 5.3
77

8-
before_script:
9-
- curl -s http://getcomposer.org/installer | php
10-
- php composer.phar install --dev --no-interaction
11-
128
script:
13-
- mkdir -p build/logs
14-
- ./vendor/bin/phpunit
9+
- make test
1510

1611
after_script:
1712
- php vendor/bin/coveralls -v

Makefile

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
install: composer.phar
2+
./composer.phar install
3+
4+
update:
5+
./composer.phar update
6+
7+
test: vendor/bin/phpunit build
8+
./vendor/bin/phpunit --strict
9+
10+
composer.phar:
11+
curl -s http://getcomposer.org/installer | php
12+
13+
vendor/bin/phpunit: install
14+
15+
build:
16+
mkdir build
17+
18+
clean:
19+
rm -f composer.phar
20+
rm -fr vendor
21+
rm -fr build

lib/Exception.php

+27-22
Original file line numberDiff line numberDiff line change
@@ -40,29 +40,11 @@ public function getRequestUri() {
4040
return $this->connection->getUri();
4141
}
4242

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

45-
public function __toString() {
46-
$method = $this->request->method;
47-
$message = $this->getMessage();
48-
$data = $this->request->errorData;
49-
50-
$phpversion = explode('.', phpversion());
51-
52-
$output = "$method: $message";
53-
54-
if (!empty($data)) {
55-
$json_encode_flags = 0;
56-
57-
if (($phpversion[0] === '5' && $phpversion[1] >= 4) || $phpversion[0] > 5) {
58-
$json_encode_flags = JSON_PRETTY_PRINT;
59-
}
60-
61-
$output .= "\n" . json_encode($data, $json_encode_flags);
62-
}
63-
64-
return $output;
65-
}
46+
/// Get the message part of the rpc error.
47+
public function getRpcMessage() { return $this->request->errorMessage; }
6648

6749
//
6850
// Protected
@@ -80,6 +62,29 @@ public function __construct(\Tivoka\Client\Connection\WebSocket $connection, Req
8062
$this->request = $request;
8163
$this->connection = $connection;
8264

83-
parent::__construct($request->errorMessage, $request->error);
65+
parent::__construct($this->formatMessage(), $request->error);
66+
}
67+
68+
protected function formatMessage() {
69+
$method = $this->request->method;
70+
$message = $this->request->errorMessage;
71+
$data = $this->request->errorData;
72+
73+
$phpversion = explode('.', phpversion());
74+
75+
$output = "$method: $message";
76+
77+
if (!empty($data)) {
78+
$json_encode_flags = 0;
79+
80+
if (($phpversion[0] === '5' && $phpversion[1] >= 4) || $phpversion[0] > 5) {
81+
$json_encode_flags = JSON_PRETTY_PRINT;
82+
}
83+
84+
$output .= "\nError data: " . json_encode($data, $json_encode_flags);
85+
}
86+
$output .= "\nOn request: " . $this->request->request;
87+
88+
return $output;
8489
}
8590
}

tests/ExceptionTest.php

+8-6
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,20 @@ public function testUnspecifiedExceptionCode() {
2222
$this->assertNotNull($e, 'A Textalk\WebshopClient\Exception should be thrown.');
2323
}
2424

25-
public function testToStringWithData() {
25+
public function testGetMessageWithData() {
2626
try {
2727
$connection = new Connection(array('webshop' => 22222));
28-
$new_order = new ApiInstance('Order', null, $connection);
29-
$new_order->set(array('language' => 'foo')); // Will not validate
28+
29+
// Make an invalid set call.
30+
$connection->Context->set(array('webshop' => 'Bad shop'), true);
3031
}
3132
catch (Textalk\WebshopClient\Exception $e) {
32-
$string = "$e";
33+
$string = $e->getMessage();
3334
$data = $e->getData();
3435

35-
$this->assertRegExp('/^Order.set: /', $string);
36-
$this->assertInternalType('array', $data);
36+
$this->assertRegExp('/^Context.set: Invalid params./', $string);
37+
$this->assertRegExp('/Error data:/', $string);
38+
$this->assertRegExp('/On request:/', $string);
3739
}
3840

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

0 commit comments

Comments
 (0)