Added new exception interface and updated transformers

This commit is contained in:
Graham Campbell 2015-12-06 17:59:26 +00:00
parent b9ca5f7066
commit 6efe100064
3 changed files with 32 additions and 6 deletions

View File

@ -0,0 +1,22 @@
<?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;
/**
* This is the exception interface.
*
* @author Graham Campbell <graham@alt-three.com>
*/
interface ExceptionInterface
{
//
}

View File

@ -1,7 +1,7 @@
<?php
/*
* This file is part of Cachet.
* This file is part of StyleCI.
*
* (c) Alt Three Services Limited
*
@ -11,17 +11,19 @@
namespace CachetHQ\Cachet\Exceptions\Transformers;
use CachetHQ\Cachet\Exceptions\ExceptionInterface;
use Exception;
use GrahamCampbell\Exceptions\Transformers\TransformerInterface;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
/**
* This is the model not found transformer class.
* This is the exception transformer class.
*
* @author Graham Campbell <graham@alt-three.com>
*/
class ModelNotFoundTransformer implements TransformerInterface
class ExceptionTransformer implements TransformerInterface
{
/**
* Transform the provided exception.
@ -32,8 +34,10 @@ class ModelNotFoundTransformer implements TransformerInterface
*/
public function transform(Exception $exception)
{
if ($exception instanceof ModelNotFoundException) {
$exception = new NotFoundHttpException('Resource not found');
if ($exception instanceof ExceptionInterface) {
$exception = new BadRequestHttpException($exception->getMessage());
} elseif ($exception instanceof ModelNotFoundException) {
$exception = new NotFoundHttpException('Resource not found.');
}
return $exception;

View File

@ -27,7 +27,7 @@ return [
'transformers' => [
'GrahamCampbell\Exceptions\Transformers\CsrfTransformer',
'CachetHQ\Cachet\Exceptions\Transformers\ModelNotFoundTransformer',
'CachetHQ\Cachet\Exceptions\Transformers\ExceptionTransformer',
],
/*