Skip to content

Commit

Permalink
parse error message
Browse files Browse the repository at this point in the history
  • Loading branch information
gam6itko committed Jun 16, 2020
1 parent 960e2ff commit c0c73c0
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 21 deletions.
6 changes: 1 addition & 5 deletions src/Transport/SparkPostApiTransport.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,11 +143,7 @@ private function handleError(ResponseInterface $response): void

$data = json_decode($response->getContent(false), true);
$this->getLogger()->error('SparkPostApiTransport error response', $data);
$error = $data['errors'][0] ?? null;
if (empty($error)) {
return;
}

throw new HttpTransportException("[{$error['code']}] {$error['message']}. {$error['description']}.", $response);
throw new HttpTransportException(json_encode($data['errors']), $response, $response->getStatusCode());
}
}
56 changes: 40 additions & 16 deletions tests/Transport/SparkPostApiTransportTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -257,25 +257,14 @@ public function testToString()
self::assertSame('sparkpost+api://', (string) $transport);
}

public function testNotSuccess(): void
/**
* @dataProvider dataNotSuccess
*/
public function testNotSuccess(array $error, string $message): void
{
self::expectException(HttpTransportException::class);
self::expectExceptionMessage('[1902] Message generation rejected. Blocked Sending Domain <domain.com>.');
self::expectExceptionMessage($message);

$error = [
'errors' => [
[
'message' => 'Message generation rejected',
'description' => 'Blocked Sending Domain <domain.com>',
'code' => '1902',
],
],
'results' => [
'total_rejected_recipients' => 0,
'total_accepted_recipients' => 1,
'id' => '012345678901234567',
],
];
$response = $this->createMock(ResponseInterface::class);
$response
->expects(self::atLeastOnce())
Expand All @@ -300,4 +289,39 @@ public function testNotSuccess(): void
$transport = new SparkPostApiTransport('api-key', $client, null, $logger);
$transport->send($this->createDefaultMessage(), $this->createDefaultEnvelope());
}

public function dataNotSuccess()
{
yield [
[
'errors' => [
[
'message' => 'Message generation rejected',
'description' => 'Blocked Sending Domain <domain.com>',
'code' => '1902',
],
],
'results' => [
'total_rejected_recipients' => 0,
'total_accepted_recipients' => 1,
'id' => '012345678901234567',
],
],
'[{"message":"Message generation rejected","description":"Blocked Sending Domain <domain.com>","code":"1902"}]',
];

yield [
[
'errors' => [
[
'message' => 'error 0',
],
[
'message' => 'error 1',
],
],
],
'[{"message":"error 0"},{"message":"error 1"}]',
];
}
}

0 comments on commit c0c73c0

Please sign in to comment.