diff --git a/composer.json b/composer.json index 2d2aad21..fdf7f1e9 100644 --- a/composer.json +++ b/composer.json @@ -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", diff --git a/src/Provide/Error/ThrowableHandler.php b/src/Provide/Error/ThrowableHandler.php index 51dcaf05..e244bf21 100644 --- a/src/Provide/Error/ThrowableHandler.php +++ b/src/Provide/Error/ThrowableHandler.php @@ -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; @@ -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); diff --git a/tests/Provide/Error/ThrowableHandlerTest.php b/tests/Provide/Error/ThrowableHandlerTest.php index 505b6c4a..47c719f5 100644 --- a/tests/Provide/Error/ThrowableHandlerTest.php +++ b/tests/Provide/Error/ThrowableHandlerTest.php @@ -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; @@ -36,6 +38,22 @@ 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;