mirror of
https://github.com/CachetHQ/Cachet.git
synced 2025-01-17 13:38:20 +01:00
Cleanup exception handling
You're going to love this @joecohens and @jbrooksuk :)
This commit is contained in:
parent
a72543daa2
commit
bf05197c4f
@ -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);
|
||||
}
|
||||
}
|
||||
|
41
app/Exceptions/Transformers/ModelNotFoundTransformer.php
Normal file
41
app/Exceptions/Transformers/ModelNotFoundTransformer.php
Normal file
@ -0,0 +1,41 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of Cachet.
|
||||
*
|
||||
* (c) Alt Three Services Limited
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace CachetHQ\Cachet\Exceptions\Transformers;
|
||||
|
||||
use Exception;
|
||||
use GrahamCampbell\Exceptions\Transformers\TransformerInterface;
|
||||
use Illuminate\Database\Eloquent\ModelNotFoundException;
|
||||
use Symfony\Component\HttpKernel\Exception\HttpNotFoundException;
|
||||
|
||||
/**
|
||||
* This is the model not found transformer class.
|
||||
*
|
||||
* @author Graham Campbell <graham@alt-three.com>
|
||||
*/
|
||||
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;
|
||||
}
|
||||
}
|
@ -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',
|
||||
],
|
||||
|
||||
/*
|
||||
|
Loading…
x
Reference in New Issue
Block a user