From 43668cf0c67b4f8da4ab1c243a05038c6e0ffa0d Mon Sep 17 00:00:00 2001 From: Andrzej Dybowski Date: Mon, 30 Jul 2018 14:01:47 +0200 Subject: [PATCH 1/3] add JsonParseException --- src/Httpful/Exception/JsonParseException.php | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 src/Httpful/Exception/JsonParseException.php diff --git a/src/Httpful/Exception/JsonParseException.php b/src/Httpful/Exception/JsonParseException.php new file mode 100644 index 0000000..6656056 --- /dev/null +++ b/src/Httpful/Exception/JsonParseException.php @@ -0,0 +1,7 @@ + Date: Mon, 30 Jul 2018 14:02:39 +0200 Subject: [PATCH 2/3] replace Exception to JsonParseException --- src/Httpful/Handlers/JsonHandler.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/Httpful/Handlers/JsonHandler.php b/src/Httpful/Handlers/JsonHandler.php index ef3bee8..6166283 100644 --- a/src/Httpful/Handlers/JsonHandler.php +++ b/src/Httpful/Handlers/JsonHandler.php @@ -6,6 +6,8 @@ namespace Httpful\Handlers; +use Httpful\Exception\JsonParseException; + class JsonHandler extends MimeHandlerAdapter { private $decode_as_array = false; @@ -27,7 +29,7 @@ public function parse($body) return null; $parsed = json_decode($body, $this->decode_as_array); if (is_null($parsed) && 'null' !== strtolower($body)) - throw new \Exception("Unable to parse response as JSON"); + throw new JsonParseException('Unable to parse response as JSON: ' . json_last_error_msg()); return $parsed; } From 9ae25f34de50df444f404743cabb548aaf656a50 Mon Sep 17 00:00:00 2001 From: Andrzej Dybowski Date: Mon, 30 Jul 2018 14:03:30 +0200 Subject: [PATCH 3/3] change unit test (testParseJSON) --- tests/Httpful/HttpfulTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/Httpful/HttpfulTest.php b/tests/Httpful/HttpfulTest.php index ad74d0d..91efd88 100644 --- a/tests/Httpful/HttpfulTest.php +++ b/tests/Httpful/HttpfulTest.php @@ -570,8 +570,8 @@ public function testParseJSON() try { $result = $handler->parse('invalid{json'); - } catch(\Exception $e) { - $this->assertEquals('Unable to parse response as JSON', $e->getMessage()); + } catch (\Httpful\Exception\JsonParseException $e) { + $this->assertEquals('Unable to parse response as JSON: ' . json_last_error_msg(), $e->getMessage()); return; } $this->fail('Expected an exception to be thrown due to invalid json');