Skip to content

Commit b397e2c

Browse files
committed
[Errors] Display api message on 404 errors
1 parent 8041138 commit b397e2c

File tree

4 files changed

+25
-4
lines changed

4 files changed

+25
-4
lines changed

src/Exceptions/ApiEntityNotFoundException.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@
66

77
class ApiEntityNotFoundException extends ApiException
88
{
9-
public function __construct($response, $httpCode = 0, Throwable $previous = null)
9+
public function __construct($response, $message = null, $httpCode = 0, Throwable $previous = null)
1010
{
1111
parent::__construct(
1212
$response,
13-
'Entity not found',
13+
$message ?? 'Entity not found',
1414
$httpCode,
1515
$previous
1616
);

src/Exceptions/ApiException.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class ApiException extends Exception
1616

1717
public function __construct($response, $message = "", $httpCode = 0, Throwable $previous = null)
1818
{
19-
parent::__construct("The request ended on a $httpCode code : $message", $httpCode, $previous);
19+
parent::__construct($message, $httpCode, $previous);
2020
$this->response = $response;
2121
}
2222

src/Exceptions/Handlers/NotFoundErrorHandler.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ public function handle(ApiException $exception, array $requestArguments)
1818
{
1919
throw new ApiEntityNotFoundException(
2020
$exception->getResponse(),
21+
$exception->getMessage(),
2122
$exception->getCode(),
2223
$exception->getPrevious()
2324
);

src/Transports/Transport.php

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,11 @@ public function request($endpoint, array $data = [], $method = 'get')
109109

110110
$exception = new ApiException(
111111
$response,
112-
$response['message'] ?? $rawResponse ?? 'Unknown error message',
112+
sprintf(
113+
'The request ended on a %s code : %s',
114+
$httpStatusCode,
115+
$this->arrayGet($response, $this->getErrorKey()) ?? $rawResponse ?? 'Unknown error message'
116+
),
113117
$httpStatusCode
114118
);
115119

@@ -120,6 +124,11 @@ public function request($endpoint, array $data = [], $method = 'get')
120124
throw $exception;
121125
}
122126

127+
public function getErrorKey(): string
128+
{
129+
return 'message';
130+
}
131+
123132
/**
124133
* {@inheritdoc}
125134
*/
@@ -235,4 +244,15 @@ public function getResponseHeaders(): array
235244
{
236245
return iterator_to_array($this->getClient()->getResponseHeaders());
237246
}
247+
248+
protected function arrayGet(array $array, string $key)
249+
{
250+
$exploded = explode('.', $key, 2);
251+
252+
if (!isset($exploded[1])) {
253+
return $array[$key];
254+
}
255+
256+
return $this->arrayGet($array[$exploded[0]], $exploded[1]);
257+
}
238258
}

0 commit comments

Comments
 (0)