diff --git a/app/Exceptions/Handler.php b/app/Exceptions/Handler.php index c594454e..c7c5d143 100644 --- a/app/Exceptions/Handler.php +++ b/app/Exceptions/Handler.php @@ -11,10 +11,7 @@ namespace CachetHQ\Cachet\Exceptions; -use Exception; use GrahamCampbell\Exceptions\ExceptionHandler; -use Illuminate\Database\Eloquent\ModelNotFoundException; -use Illuminate\Support\Facades\Response; use Symfony\Component\HttpKernel\Exception\HttpNotFoundException; class Handler extends ExceptionHandler @@ -27,21 +24,4 @@ class Handler extends ExceptionHandler protected $dontReport = [ HttpNotFoundException::class, ]; - - /** - * Render an exception into an HTTP response. - * - * @param \Illuminate\Http\Request $request - * @param \Exception $e - * - * @return \Illuminate\Http\Response - */ - public function render($request, Exception $e) - { - if ($e instanceof ModelNotFoundException) { - $e = new HttpNotFoundException('Resource not found'); - } - - return parent::render($request, $e); - } } diff --git a/app/Exceptions/Transformers/ModelNotFoundTransformer.php b/app/Exceptions/Transformers/ModelNotFoundTransformer.php new file mode 100644 index 00000000..9f5a6bca --- /dev/null +++ b/app/Exceptions/Transformers/ModelNotFoundTransformer.php @@ -0,0 +1,41 @@ + + */ +class ModelNotFoundTransformer implements TransformerInterface +{ + /** + * Transform the provided exception. + * + * @param \Exception $exception + * + * @return \Exception + */ + public function transform(Exception $exception) + { + if ($exception instanceof ModelNotFoundException) { + $exception = new HttpNotFoundException('Resource not found'); + } + + return $exception; + } +} diff --git a/config/exceptions.php b/config/exceptions.php index 124b3346..6185b6a2 100644 --- a/config/exceptions.php +++ b/config/exceptions.php @@ -11,6 +11,25 @@ return [ + /* + |-------------------------------------------------------------------------- + | Exception Transformers + |-------------------------------------------------------------------------- + | + | Here are each of the exception transformers setup for your application. + | + | This allows you to turn your exceptions into other exceptions such as + | http exceptions for perfect results when passed to the displayers. Note + | that this list is processed in order and subsequent transformers can + | still modify the results of previous ones if required. + | + */ + + 'transformers' => [ + 'GrahamCampbell\Exceptions\Transformers\CsrfTransformer', + 'CachetHQ\Cachet\Exceptions\Transformers\ModelNotFoundTransformer' + ], + /* |-------------------------------------------------------------------------- | Exception Displayers @@ -30,6 +49,7 @@ return [ 'GrahamCampbell\Exceptions\Displayers\DebugDisplayer', 'GrahamCampbell\Exceptions\Displayers\HtmlDisplayer', 'GrahamCampbell\Exceptions\Displayers\JsonDisplayer', + 'GrahamCampbell\Exceptions\Displayers\JsonApiDisplayer', ], /*