From f3f0538d1a52e14a16c52fc2c6246a1ca48a0496 Mon Sep 17 00:00:00 2001 From: James Fellows Date: Mon, 4 Apr 2016 22:52:13 +0100 Subject: [PATCH] Improve Exception message on 403 response Fixes #70. --- lib/SparkPost/APIResource.php | 3 +++ test/unit/APIResourceTest.php | 15 +++++++++++++++ 2 files changed, 18 insertions(+) diff --git a/lib/SparkPost/APIResource.php b/lib/SparkPost/APIResource.php index 20e7b19..3d0f8c1 100644 --- a/lib/SparkPost/APIResource.php +++ b/lib/SparkPost/APIResource.php @@ -193,6 +193,9 @@ private function callResource( $action, $resourcePath=null, $options=[] ) { if ($statusCode < 400) { return json_decode($response->getBody()->getContents(), true); } + elseif ($statusCode === 403) { + throw new APIResponseException('Request forbidden. Does this API Key have the correct SparkPost permissions?'); + } elseif ($statusCode === 404) { throw new APIResponseException('The specified resource does not exist', 404); } diff --git a/test/unit/APIResourceTest.php b/test/unit/APIResourceTest.php index 0baef41..1d29c55 100644 --- a/test/unit/APIResourceTest.php +++ b/test/unit/APIResourceTest.php @@ -111,6 +111,21 @@ public function testAdapter404Exception() { } } + public function testAdapter403Exception() { + try { + $responseMock = Mockery::mock(); + $this->sparkPostMock->httpAdapter->shouldReceive('send')-> + once()-> + andReturn($responseMock); + $responseMock->shouldReceive('getStatusCode')->andReturn(403); + + $this->resource->get('test'); + } + catch(\Exception $e) { + $this->assertRegExp('/Request forbidden/', $e->getMessage()); + } + } + public function testAdapter4XXException() { try { $testBody = ['errors'=>['my'=>'test']];