Skip to content

Commit 80f38df

Browse files
authored
Merge pull request #95 from danielh-official/pr/88-add-tests
NotionException: Use status from http response #88 additions
2 parents 0ab4d2f + 58b94f4 commit 80f38df

File tree

7 files changed

+21
-3
lines changed

7 files changed

+21
-3
lines changed

src/Exceptions/NotionException.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,17 @@ class NotionException extends LaravelNotionAPIException
1616
*/
1717
public static function instance(string $message, array $payload = []): NotionException
1818
{
19-
$e = new NotionException($message);
19+
$code = 0;
20+
21+
$responseDataExists = array_key_exists('responseData', $payload);
22+
23+
if ($responseDataExists) {
24+
$responseData = $payload['responseData'];
25+
26+
$code = array_key_exists('status', $responseData) ? $responseData['status'] : 0;
27+
}
28+
29+
$e = new NotionException($message, $code);
2030
$e->payload = $payload;
2131

2232
return $e;
@@ -46,7 +56,7 @@ public static function fromResponse(Response $response): NotionException
4656

4757
return new NotionException(
4858
$message,
49-
0,
59+
$response->status(),
5060
$response->toException()
5161
);
5262
}

tests/EndpointBlocksTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ public function it_throws_a_notion_exception_bad_request()
4545

4646
$this->expectException(NotionException::class);
4747
$this->expectExceptionMessage('Bad Request');
48+
$this->expectExceptionCode(400);
4849

4950
Notion::block('b55c9c91-384d-452b-81db-d1ef79372b76')->children();
5051
}
@@ -216,6 +217,7 @@ public function it_throws_a_notion_exception_not_found()
216217

217218
$this->expectException(NotionException::class);
218219
$this->expectExceptionMessage('Not found');
220+
$this->expectExceptionCode(404);
219221

220222
Notion::block('b55c9c91-384d-452b-81db-d1ef79372b11')->children();
221223
}

tests/EndpointDatabaseTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@
127127

128128
$this->expectException(NotionException::class);
129129
$this->expectExceptionMessage('Bad Request');
130-
130+
$this->expectExceptionCode(400);
131131
Notion::database('8284f3ff77e24d4a939d19459e4d6bdc')->query();
132132
});
133133

tests/EndpointPagesTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ public function it_throws_a_notion_exception_bad_request()
4646

4747
$this->expectException(NotionException::class);
4848
$this->expectExceptionMessage('Bad Request');
49+
$this->expectExceptionCode(400);
4950

5051
Notion::pages()->find('afd5f6fb-1cbd-41d1-a108-a22ae0d9bac8');
5152
}
@@ -97,6 +98,7 @@ public function it_throws_a_notion_exception_not_found()
9798

9899
$this->expectException(NotionException::class);
99100
$this->expectExceptionMessage('Not found');
101+
$this->expectExceptionCode(404);
100102

101103
Notion::pages()->find('b55c9c91-384d-452b-81db-d1ef79372b79');
102104
}

tests/EndpointSearchTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ public function it_throws_a_notion_exception_bad_request()
3232

3333
$this->expectException(NotionException::class);
3434
$this->expectExceptionMessage('Bad Request');
35+
$this->expectExceptionCode(400);
3536

3637
Notion::search()->query();
3738
}

tests/EndpointUsersTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ public function it_throws_a_notion_exception_bad_request()
3131

3232
$this->expectException(NotionException::class);
3333
$this->expectExceptionMessage('Bad Request');
34+
$this->expectExceptionCode(400);
3435

3536
Notion::users()->all();
3637
}
@@ -96,6 +97,7 @@ public function it_throws_a_notion_exception_not_found()
9697

9798
$this->expectException(NotionException::class);
9899
$this->expectExceptionMessage('Not found');
100+
$this->expectExceptionCode(404);
99101

100102
Notion::users()->find('d40e767c-d7af-4b18-a86d-55c61f1e39a1');
101103
}

tests/NotionExceptionTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ public function it_throws_a_notion_exception_with_detailed_message_from_response
2323

2424
$this->expectException(NotionException::class);
2525
$this->expectExceptionMessage('Bad Request: (validation_error) (path failed validation: path.id should be a valid uuid, instead was');
26+
$this->expectExceptionCode(400);
2627

2728
\Notion::block('d092140ce4e549bf9915fb8ad43d1699d')->children()->asCollection();
2829
}

0 commit comments

Comments
 (0)