Skip to content

Commit 06ac4fd

Browse files
author
Apostolos Krionidis
committed
Removed 'throwOnJsonLastError' method
1 parent e012d3d commit 06ac4fd

File tree

1 file changed

+1
-64
lines changed

1 file changed

+1
-64
lines changed

src/Response/Response.php

Lines changed: 1 addition & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public function __construct(ResponseInterface $response)
5353
$response->getBody()->rewind();
5454
}
5555

56-
$body = $this->decodeValidJson($response->getBody()->getContents());
56+
$body = json_decode($response->getBody()->getContents(), false, 512, JSON_THROW_ON_ERROR);
5757

5858
if ($body) {
5959
$members = array_keys(get_object_vars($this));
@@ -66,69 +66,6 @@ public function __construct(ResponseInterface $response)
6666
}
6767
}
6868

69-
/**
70-
* @throws \JsonException
71-
*/
72-
protected function decodeValidJson(string $json): string
73-
{
74-
$body = json_decode($json, false, 512, JSON_THROW_ON_ERROR);
75-
76-
// Check and throw exception in case of unhandled decode error
77-
$this->throwOnJsonLastError($body);
78-
79-
return $body;
80-
}
81-
82-
/**
83-
* @throws \JsonException
84-
*/
85-
private function throwOnJsonLastError($decoded): void
86-
{
87-
$generalErrorMessage = 'Could not decode JSON!';
88-
89-
//Backwards compatability.
90-
if (!function_exists('json_last_error')) {
91-
if ($decoded === false || $decoded === null) {
92-
throw new \JsonException($generalErrorMessage);
93-
}
94-
} else {
95-
//Get the last JSON error.
96-
$jsonError = json_last_error();
97-
98-
//In some cases, this will happen.
99-
if (is_null($decoded) && $jsonError == JSON_ERROR_NONE) {
100-
throw new \JsonException($generalErrorMessage);
101-
}
102-
103-
//If an error exists.
104-
if ($jsonError != JSON_ERROR_NONE) {
105-
//Use a switch statement to figure out the exact error.
106-
switch ($jsonError) {
107-
case JSON_ERROR_DEPTH:
108-
$error = 'Maximum depth exceeded!';
109-
break;
110-
case JSON_ERROR_STATE_MISMATCH:
111-
$error = 'Underflow or the modes mismatch!';
112-
break;
113-
case JSON_ERROR_CTRL_CHAR:
114-
$error = 'Unexpected control character found';
115-
break;
116-
case JSON_ERROR_SYNTAX:
117-
$error = 'Malformed JSON';
118-
break;
119-
case JSON_ERROR_UTF8:
120-
$error = 'Malformed UTF-8 characters found!';
121-
break;
122-
default:
123-
$error = 'Unknown error!';
124-
break;
125-
}
126-
127-
throw new \JsonException(sprintf('%s %s', $generalErrorMessage, $error));
128-
}
129-
}
130-
}
131-
13269
/**
13370
* @return int
13471
*/

0 commit comments

Comments
 (0)