diff --git a/src/Exceptions/NotionException.php b/src/Exceptions/NotionException.php index f33689d..000c62b 100644 --- a/src/Exceptions/NotionException.php +++ b/src/Exceptions/NotionException.php @@ -3,6 +3,7 @@ namespace FiveamCode\LaravelNotionApi\Exceptions; use Illuminate\Http\Client\Response; +use Throwable; /** * Class NotionException @@ -33,8 +34,22 @@ public static function instance(string $message, array $payload = []): NotionExc */ public static function fromResponse(Response $response): NotionException { + $responseBody = json_decode($response->getBody()->getContents(), true); + + $errorCode = $errorMessage = ""; + if (array_key_exists("code", $responseBody)) { + $errorCode = "({$responseBody["code"]})"; + } + + if (array_key_exists("code", $responseBody)) { + $errorMessage = "({$responseBody["message"]})"; + } + + $message = "{$response->getReasonPhrase()}: {$errorCode} {$errorMessage}"; + return new NotionException( - $response->getReasonPhrase(), 0, + $message, + 0, $response->toException() ); } diff --git a/tests/NotionExceptionTest.php b/tests/NotionExceptionTest.php new file mode 100644 index 0000000..61717ee --- /dev/null +++ b/tests/NotionExceptionTest.php @@ -0,0 +1,34 @@ + Http::response( + json_decode(file_get_contents("tests/stubs/endpoints/blocks/response_children_invalid_uuid_400.json"), true), + 400, + ['Headers'] + ) + ]); + + $this->expectException(NotionException::class); + $this->expectExceptionMessage("Bad Request: (validation_error) (path failed validation: path.id should be a valid uuid, instead was"); + + \Notion::block("d092140ce4e549bf9915fb8ad43d1699d")->children()->asCollection(); + } +} \ No newline at end of file diff --git a/tests/stubs/endpoints/blocks/response_children_invalid_uuid_400.json b/tests/stubs/endpoints/blocks/response_children_invalid_uuid_400.json new file mode 100644 index 0000000..764f781 --- /dev/null +++ b/tests/stubs/endpoints/blocks/response_children_invalid_uuid_400.json @@ -0,0 +1,6 @@ +{ + "object": "error", + "status": 400, + "code": "validation_error", + "message": "path failed validation: path.id should be a valid uuid, instead was `\"d092140ce4e549bf9915fb8ad43d1699d\"`." +} \ No newline at end of file