Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion src/Exceptions/NotionException.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace FiveamCode\LaravelNotionApi\Exceptions;

use Illuminate\Http\Client\Response;
use Throwable;

/**
* Class NotionException
Expand Down Expand Up @@ -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()
);
}
Expand Down
34 changes: 34 additions & 0 deletions tests/NotionExceptionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace FiveamCode\LaravelNotionApi\Tests;

use FiveamCode\LaravelNotionApi\Exceptions\NotionException;
use Notion;
use Illuminate\Support\Facades\Http;

/**
* Class HandlingExceptionTest
* @package FiveamCode\LaravelNotionApi\Tests
*/
class NotionExceptionTest extends NotionApiTest
{

/** @test */
public function it_throws_a_notion_exception_with_detailed_message_from_response()
{

Http::fake([
'https://api.notion.com/v1/blocks/d092140ce4e549bf9915fb8ad43d1699d/children*'
=> 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();
}
}
Original file line number Diff line number Diff line change
@@ -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\"`."
}