From 98e7395a21afe08873a65df069deed40bf6fe174 Mon Sep 17 00:00:00 2001 From: Rodrigo Pedra Brum Date: Fri, 15 Jan 2021 14:27:42 -0300 Subject: [PATCH 1/2] handle sole exceptions --- .../Foundation/Exceptions/Handler.php | 4 +++ .../FoundationExceptionsHandlerTest.php | 36 +++++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/src/Illuminate/Foundation/Exceptions/Handler.php b/src/Illuminate/Foundation/Exceptions/Handler.php index c73130cb137e..4c8f6d828190 100644 --- a/src/Illuminate/Foundation/Exceptions/Handler.php +++ b/src/Illuminate/Foundation/Exceptions/Handler.php @@ -373,6 +373,10 @@ protected function prepareException(Throwable $e) $e = new HttpException(419, $e->getMessage(), $e); } elseif ($e instanceof SuspiciousOperationException) { $e = new NotFoundHttpException('Bad hostname provided.', $e); + } elseif ($e instanceof RecordsNotFoundException) { + $e = new NotFoundHttpException('Not found.', $e); + } elseif ($e instanceof MultipleRecordsFoundException) { + $e = new HttpException(400, 'Bad request.', $e); } return $e; diff --git a/tests/Foundation/FoundationExceptionsHandlerTest.php b/tests/Foundation/FoundationExceptionsHandlerTest.php index 887de48f7ddd..534ddb3459fe 100644 --- a/tests/Foundation/FoundationExceptionsHandlerTest.php +++ b/tests/Foundation/FoundationExceptionsHandlerTest.php @@ -8,6 +8,8 @@ use Illuminate\Contracts\Routing\ResponseFactory as ResponseFactoryContract; use Illuminate\Contracts\Support\Responsable; use Illuminate\Contracts\View\Factory; +use Illuminate\Database\MultipleRecordsFoundException; +use Illuminate\Database\RecordsNotFoundException; use Illuminate\Foundation\Exceptions\Handler; use Illuminate\Http\RedirectResponse; use Illuminate\Http\Request; @@ -235,6 +237,40 @@ public function testSuspiciousOperationReturns404WithoutReporting() $this->handler->report(new SuspiciousOperationException('Invalid method override "__CONSTRUCT"')); } + + public function testRecordsNotFoundReturns404WithoutReporting() + { + $this->config->shouldReceive('get')->with('app.debug', null)->once()->andReturn(true); + $this->request->shouldReceive('expectsJson')->once()->andReturn(true); + + $response = $this->handler->render($this->request, new RecordsNotFoundException()); + + $this->assertEquals(404, $response->getStatusCode()); + $this->assertStringContainsString('"message": "Not found."', $response->getContent()); + + $logger = m::mock(LoggerInterface::class); + $this->container->instance(LoggerInterface::class, $logger); + $logger->shouldNotReceive('error'); + + $this->handler->report(new RecordsNotFoundException()); + } + + public function testMultipleRecordsFoundReturns400WithoutReporting() + { + $this->config->shouldReceive('get')->with('app.debug', null)->once()->andReturn(true); + $this->request->shouldReceive('expectsJson')->once()->andReturn(true); + + $response = $this->handler->render($this->request, new MultipleRecordsFoundException()); + + $this->assertEquals(400, $response->getStatusCode()); + $this->assertStringContainsString('"message": "Bad request."', $response->getContent()); + + $logger = m::mock(LoggerInterface::class); + $this->container->instance(LoggerInterface::class, $logger); + $logger->shouldNotReceive('error'); + + $this->handler->report(new MultipleRecordsFoundException()); + } } class CustomException extends Exception From 97b9a0451708ba243abd086bb2c626124efb051d Mon Sep 17 00:00:00 2001 From: Rodrigo Pedra Brum Date: Fri, 15 Jan 2021 15:07:34 -0300 Subject: [PATCH 2/2] do not handle MultipleRecordsFoundException --- .../Foundation/Exceptions/Handler.php | 2 -- .../FoundationExceptionsHandlerTest.php | 18 ------------------ 2 files changed, 20 deletions(-) diff --git a/src/Illuminate/Foundation/Exceptions/Handler.php b/src/Illuminate/Foundation/Exceptions/Handler.php index 4c8f6d828190..33fbccc5e7e1 100644 --- a/src/Illuminate/Foundation/Exceptions/Handler.php +++ b/src/Illuminate/Foundation/Exceptions/Handler.php @@ -375,8 +375,6 @@ protected function prepareException(Throwable $e) $e = new NotFoundHttpException('Bad hostname provided.', $e); } elseif ($e instanceof RecordsNotFoundException) { $e = new NotFoundHttpException('Not found.', $e); - } elseif ($e instanceof MultipleRecordsFoundException) { - $e = new HttpException(400, 'Bad request.', $e); } return $e; diff --git a/tests/Foundation/FoundationExceptionsHandlerTest.php b/tests/Foundation/FoundationExceptionsHandlerTest.php index 534ddb3459fe..f7175dc58db5 100644 --- a/tests/Foundation/FoundationExceptionsHandlerTest.php +++ b/tests/Foundation/FoundationExceptionsHandlerTest.php @@ -8,7 +8,6 @@ use Illuminate\Contracts\Routing\ResponseFactory as ResponseFactoryContract; use Illuminate\Contracts\Support\Responsable; use Illuminate\Contracts\View\Factory; -use Illuminate\Database\MultipleRecordsFoundException; use Illuminate\Database\RecordsNotFoundException; use Illuminate\Foundation\Exceptions\Handler; use Illuminate\Http\RedirectResponse; @@ -254,23 +253,6 @@ public function testRecordsNotFoundReturns404WithoutReporting() $this->handler->report(new RecordsNotFoundException()); } - - public function testMultipleRecordsFoundReturns400WithoutReporting() - { - $this->config->shouldReceive('get')->with('app.debug', null)->once()->andReturn(true); - $this->request->shouldReceive('expectsJson')->once()->andReturn(true); - - $response = $this->handler->render($this->request, new MultipleRecordsFoundException()); - - $this->assertEquals(400, $response->getStatusCode()); - $this->assertStringContainsString('"message": "Bad request."', $response->getContent()); - - $logger = m::mock(LoggerInterface::class); - $this->container->instance(LoggerInterface::class, $logger); - $logger->shouldNotReceive('error'); - - $this->handler->report(new MultipleRecordsFoundException()); - } } class CustomException extends Exception