4
4
5
5
namespace Art4 \Wegliphant ;
6
6
7
- use Exception ;
7
+ use Art4 \ Wegliphant \ Exception \ UnexpectedResponseException ;
8
8
use JsonException ;
9
9
use Psr \Http \Client \ClientInterface ;
10
10
use Psr \Http \Message \RequestFactoryInterface ;
@@ -27,7 +27,7 @@ public static function create(
27
27
* @link https://www.weg.li/api
28
28
*
29
29
* @throws \Psr\Http\Client\ClientExceptionInterface If an error happens while processing the request.
30
- * @throws \Exception If an error happens while processing the response.
30
+ * @throws UnexpectedResponseException If an error happens while processing the response.
31
31
*
32
32
* @return mixed[]
33
33
*/
@@ -46,7 +46,7 @@ public function listDistricts(): array
46
46
* @link https://www.weg.li/api
47
47
*
48
48
* @throws \Psr\Http\Client\ClientExceptionInterface If an error happens while processing the request.
49
- * @throws \Exception If an error happens while processing the response.
49
+ * @throws UnexpectedResponseException If an error happens while processing the response.
50
50
*
51
51
* @return mixed[]
52
52
*/
@@ -65,7 +65,7 @@ public function getDistrictByZip(string $zip): array
65
65
* @link https://www.weg.li/api
66
66
*
67
67
* @throws \Psr\Http\Client\ClientExceptionInterface If an error happens while processing the request.
68
- * @throws \Exception If an error happens while processing the response.
68
+ * @throws UnexpectedResponseException If an error happens while processing the response.
69
69
*
70
70
* @return mixed[]
71
71
*/
@@ -97,23 +97,23 @@ private function sendJsonRequest(
97
97
}
98
98
99
99
/**
100
- * @throws \Exception If the response has the wrong status code or content type header.
100
+ * @throws UnexpectedResponseException If the response has the wrong status code or content type header.
101
101
*/
102
102
private function ensureJsonResponse (
103
103
ResponseInterface $ response ,
104
104
int $ expectedStatusCode ,
105
105
): void {
106
106
if ($ response ->getStatusCode () !== $ expectedStatusCode ) {
107
- throw new Exception ('Server replied with status code ' . $ response ->getStatusCode ());
107
+ throw UnexpectedResponseException:: create ('Server replied with status code ' . $ response ->getStatusCode (), $ response );
108
108
}
109
109
110
110
if (! str_starts_with ($ response ->getHeaderLine ('content-type ' ), 'application/json ' )) {
111
- throw new Exception ('Server replied not with JSON content. ' );
111
+ throw UnexpectedResponseException:: create ('Server replied not with JSON content. ' , $ response );
112
112
}
113
113
}
114
114
115
115
/**
116
- * @throws \Exception If an error happens while processing the response.
116
+ * @throws UnexpectedResponseException If an error happens while parsing the JSON response.
117
117
*
118
118
* @return mixed[]
119
119
*/
@@ -124,11 +124,11 @@ private function parseJsonResponseToArray(ResponseInterface $response): array
124
124
try {
125
125
$ data = json_decode ($ responseBody , true , 512 , JSON_THROW_ON_ERROR );
126
126
} catch (JsonException $ th ) {
127
- throw new Exception ('Response body contains no valid JSON: ' . $ responseBody , 0 , $ th );
127
+ throw UnexpectedResponseException:: create ('Response body contains no valid JSON: ' . $ responseBody , $ response , $ th );
128
128
}
129
129
130
130
if (! is_array ($ data )) {
131
- throw new Exception ('Response JSON does not contain an array: ' . $ responseBody );
131
+ throw UnexpectedResponseException:: create ('Response JSON does not contain an array: ' . $ responseBody, $ response );
132
132
}
133
133
134
134
return $ data ;
0 commit comments