From d10bcae197284161c7c8303df806d99e0bc006fd Mon Sep 17 00:00:00 2001 From: Pig Fang Date: Wed, 19 Aug 2020 18:34:17 +0800 Subject: [PATCH] provide better message when texture is missing --- app/Exceptions/Handler.php | 15 ++++++++++++++- .../ControllersTest/SkinlibControllerTest.php | 4 ++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/app/Exceptions/Handler.php b/app/Exceptions/Handler.php index 369d4b40..0e448c2c 100644 --- a/app/Exceptions/Handler.php +++ b/app/Exceptions/Handler.php @@ -2,6 +2,7 @@ namespace App\Exceptions; +use Illuminate\Database\Eloquent\ModelNotFoundException; use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler; use Illuminate\Support\Arr; use Illuminate\Support\Str; @@ -16,12 +17,24 @@ class Handler extends ExceptionHandler \Illuminate\Auth\AuthenticationException::class, \Illuminate\Auth\Access\AuthorizationException::class, \Symfony\Component\HttpKernel\Exception\HttpException::class, - \Illuminate\Database\Eloquent\ModelNotFoundException::class, \Illuminate\Validation\ValidationException::class, \Illuminate\Session\TokenMismatchException::class, + ModelNotFoundException::class, PrettyPageException::class, ]; + public function render($request, Throwable $exception) + { + if ($exception instanceof ModelNotFoundException) { + $model = $exception->getModel(); + if (Str::endsWith($model, 'Texture')) { + $exception = new ModelNotFoundException(trans('skinlib.non-existent')); + } + } + + return parent::render($request, $exception); + } + protected function convertExceptionToArray(Throwable $e) { return [ diff --git a/tests/HttpTest/ControllersTest/SkinlibControllerTest.php b/tests/HttpTest/ControllersTest/SkinlibControllerTest.php index 2ac9f74a..525f2dc8 100644 --- a/tests/HttpTest/ControllersTest/SkinlibControllerTest.php +++ b/tests/HttpTest/ControllersTest/SkinlibControllerTest.php @@ -210,6 +210,10 @@ class SkinlibControllerTest extends TestCase public function testInfo() { + $this->get(route('texture.info', ['texture' => 0])) + ->assertNotFound() + ->assertSee(trans('skinlib.non-existent')); + $texture = factory(Texture::class)->create(); $this->get(route('texture.info', ['texture' => $texture])) ->assertJson($texture->toArray());