Skip to content

Commit 1124aaa

Browse files
committed
MA-1084 #time 1h Implemented better exception handling and added unit
tests for said exception handling
1 parent d3eeaeb commit 1124aaa

File tree

5 files changed

+60
-28
lines changed

5 files changed

+60
-28
lines changed

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,11 +87,12 @@ try {
8787
## Development
8888

8989
### Setup
90-
9190
Run `composer install` inside the directory to install dependecies and development tools.
9291

9392
### Testing
94-
Once all the dependencies are installed, you can execute the unit tests using `vendor/bin/phpunit --bootstrap test/unit/bootstrap.php ./test/unit`
93+
Once all the dependencies are installed, you can execute the unit tests using `vendor/bin/phpunit --bootstrap test/unit/bootstrap.php ./test/unit`.
94+
95+
If you're interested in code coverage, you can add the `--coverage` flag for phpunit like so: ```phpunit --coverage-html test/output/report --bootstrap test/unit/bootstrap.php ./test/unit```
9596

9697
### Contributing
9798
Guidelines for adding issues

lib/SparkPost/Transmission.php

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22
namespace SparkPost;
33
use Guzzle\Http\Client;
4-
use Guzzle\Http\Exception\RequestException;
4+
use Guzzle\Http\Exception\ClientErrorResponseException;
55

66
/**
77
* @desc SDK interface for managing transmissions
@@ -98,18 +98,6 @@ private static function getBaseUrl($config) {
9898
return $config['protocol'] . '://' . $config['host'] . ($config['port'] ? ':' . $config['port'] : '') . $baseUrl;
9999
}
100100

101-
/**
102-
* Helper function for extracting the response status code out of request thrown exceptions
103-
* @param string $exceptionMessage
104-
* @return number
105-
*/
106-
private static function getStatusCode ($exceptionMessage) {
107-
$messageParts = explode("\n", $exceptionMessage);
108-
$codeLine = explode('] ', $messageParts[1]);
109-
$code = trim($codeLine[1]);
110-
return (int)$code;
111-
}
112-
113101
/**
114102
* @desc Method for issuing POST request to the Transmissions API
115103
*
@@ -150,11 +138,19 @@ public static function send($transmissionConfig) {
150138
try {
151139
$response = $request->post(self::getBaseUrl($hostConfig), array('authorization' => $hostConfig['key']), json_encode($model), array("verify"=>$hostConfig['strictSSL']))->send();
152140
return $response->json();
153-
} catch (RequestException $exception) {
141+
}
142+
/*
143+
* Handles 4XX responses
144+
*/
145+
catch (ClientErrorResponseException $exception) {
154146
$response = $exception->getResponse();
155147
$responseArray = $response->json();
156-
throw new \Exception(json_encode($responseArray['errors']));
157-
} catch (\Exception $exception) {
148+
throw new \Exception(json_encode($responseArray['errors']));
149+
}
150+
/*
151+
* Handles 5XX Errors, Configuration Errors, and a catch all for other errors
152+
*/
153+
catch (\Exception $exception) {
158154
throw new \Exception('Unable to contact Transmissions API: '. $exception->getMessage());
159155
}
160156
}
@@ -184,15 +180,23 @@ private static function fetch ($transmissionID = null) {
184180
try {
185181
$response = $request->get($url, array('authorization' => $hostConfig['key']), array("verify"=>$hostConfig['strictSSL']))->send();
186182
return $response->json();
187-
} catch (\Exception $exception) {
188-
$statusCode = self::getStatusCode($exception->getMessage());
183+
}
184+
/*
185+
* Handles 4XX responses
186+
*/
187+
catch (ClientErrorResponseException $exception) {
188+
$response = $exception->getResponse();
189+
$statusCode = $response->getStatusCode();
189190
if($statusCode === 404) {
190191
throw new \Exception("The specified Transmission ID does not exist", 404);
191-
}else if ($statusCode !== null){
192-
throw new \Exception("Received bad response from Transmission API: ". $statusCode);
193-
} else {
194-
throw new \Exception('Unable to contact Transmissions API: '. $exception->getMessage());
195192
}
193+
throw new \Exception("Received bad response from Transmission API: ". $statusCode);
194+
}
195+
/*
196+
* Handles 5XX Errors, Configuration Errors, and a catch all for other errors
197+
*/
198+
catch (\Exception $exception) {
199+
throw new \Exception('Unable to contact Transmissions API: '. $exception->getMessage());
196200
}
197201
}
198202

test/unit/SendGridCompatibiility/EmailTest.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,11 +96,13 @@ public function testSetHtml() {
9696
$this->assertEquals($value, $this->email->model['html']);
9797
}
9898

99+
/**
100+
* @desc test that adding a category throws an exception since we don't support tags at transmission level yet
101+
* @expectedException Exception
102+
* @expectedExceptionMessage Adding categories is not yet supported
103+
*/
99104
public function testAddCategory() {
100-
$value = 'Category A';
101105
$this->email->addCategory($value);
102-
103-
$this->assertEquals(array($value), $this->email->model['tags']);
104106
}
105107

106108
/**

test/unit/TransmissionTest.php

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,18 @@ public function testFindWithOtherBadResponse() {
8989
Transmission::find('someId');
9090
}
9191

92+
/**
93+
* @desc tests bad response
94+
* @expectedException Exception
95+
* @expectedExceptionMessageRegExp /Unable to contact Transmissions API:.* /
96+
*/
97+
public function testFindForCatchAllException() {
98+
$mock = new MockPlugin();
99+
$mock->addResponse(new Response(500));
100+
$this->client->addSubscriber($mock);
101+
Transmission::find('someId');
102+
}
103+
92104
/**
93105
* @desc tests happy path
94106
*/
@@ -107,13 +119,26 @@ public function testSuccessfulSend() {
107119
* @expectedException Exception
108120
* @expectedExceptionMessage ["This is a fake error"]
109121
*/
110-
public function testSendForRequestException() {
122+
public function testSendFor400Exception() {
111123
$body = array('errors'=>array('This is a fake error'));
112124
$mock = new MockPlugin();
113125
$mock->addResponse(new Response(400, array(), json_encode($body)));
114126
$this->client->addSubscriber($mock);
115127
Transmission::send(array('text'=>'awesome email'));
116128
}
117129

130+
131+
/**
132+
* @desc tests bad response
133+
* @expectedException Exception
134+
* @expectedExceptionMessageRegExp /Unable to contact Transmissions API:.* /
135+
*/
136+
public function testSendForCatchAllException() {
137+
$mock = new MockPlugin();
138+
$mock->addResponse(new Response(500));
139+
$this->client->addSubscriber($mock);
140+
Transmission::send(array('text'=>'awesome email'));
141+
}
142+
118143
}
119144
?>

0 commit comments

Comments
 (0)