Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
],
"require": {
"php": "^8.2",
"bear/resource": "^1.16",
"bear/resource": "^1.33",
"psr/log": "^1.1 || ^2.0 || ^3.0",
"ray/aop": "^2.12.3",
"ray/di": "^2.13",
Expand Down
6 changes: 6 additions & 0 deletions src/Provide/Error/ThrowableHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace BEAR\Sunday\Provide\Error;

use BEAR\Resource\Exception\BadRequestException;
use BEAR\Resource\Exception\JsonSchemaRequestException;
use BEAR\Sunday\Extension\Error\ErrorInterface;
use BEAR\Sunday\Extension\Error\ThrowableHandlerInterface;
use BEAR\Sunday\Extension\Router\RouterMatch as Request;
Expand All @@ -29,6 +31,10 @@ public function handle(Throwable $e, Request $request): ThrowableHandlerInterfac
$e = new ErrorException($e->getMessage(), $e->getCode(), E_ERROR, $e->getFile(), $e->getLine(), $e);
}

if ($e instanceof JsonSchemaRequestException) {
$e = new BadRequestException($e->getMessage(), $e->getCode(), $e);
}

/** @var Exception $e */
$this->error->handle($e, $request);

Expand Down
20 changes: 19 additions & 1 deletion tests/Provide/Error/ThrowableHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace BEAR\Sunday\Provide\Error;

use BEAR\Resource\Exception\JsonSchemaRequestException;
use BEAR\Resource\Exception\JsonSchemaResponseException;
use BEAR\Resource\Exception\ResourceNotFoundException;
use BEAR\Sunday\Extension\Router\RouterMatch;
use BEAR\Sunday\Provide\Transfer\ConditionalResponse;
Expand Down Expand Up @@ -36,12 +38,28 @@ public function testException(): void
$this->assertSame('{"message":"Not Found"}', FakeHttpResponder::$body);
}

public function testJsonSchemaRequestException(): void
{
$e = new JsonSchemaRequestException('title is required');
$this->throableHandler->handle($e, new RouterMatch())->transfer();
$this->assertSame(400, FakeHttpResponder::$code);
$this->assertSame([['Content-Type: application/vnd.error+json', false]], FakeHttpResponder::$headers);
$this->assertSame('{"message":"Bad Request"}', FakeHttpResponder::$body);
}

public function testJsonSchemaResponseException(): void
{
$e = new JsonSchemaResponseException('response does not match schema');
$this->throableHandler->handle($e, new RouterMatch())->transfer();
$this->assertSame(500, FakeHttpResponder::$code);
}

public function testError(): void
{
$e = null;
try {
echo HELLO; // @phpstan-ignore-line
} catch (Throwable $e) { // @phpstan-ignore-line create $e
} catch (Throwable $e) {
}

$this->throableHandler->handle($e, new RouterMatch())->transfer(); // @phpstan-ignore-line
Expand Down
Loading