Merge pull request #1203 from cachethq/exceptions

Exception Improvements
This commit is contained in:
Graham Campbell
2015-12-07 21:59:10 +00:00
5 changed files with 62 additions and 5 deletions

View File

@@ -0,0 +1,24 @@
<?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;
use Exception;
/**
* This is the already subscribed exception class.
*
* @author Graham Campbell <graham@alt-three.com>
*/
class AlreadySubscribedException extends Exception implements ExceptionInterface
{
//
}

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

@@ -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

@@ -14,6 +14,7 @@ namespace CachetHQ\Cachet\Handlers\Commands\Subscriber;
use CachetHQ\Cachet\Commands\Subscriber\SubscribeSubscriberCommand;
use CachetHQ\Cachet\Commands\Subscriber\VerifySubscriberCommand;
use CachetHQ\Cachet\Events\Subscriber\SubscriberHasSubscribedEvent;
use CachetHQ\Cachet\Exceptions\AlreadySubscribedException;
use CachetHQ\Cachet\Models\Subscriber;
use Illuminate\Foundation\Bus\DispatchesJobs;
@@ -26,10 +27,16 @@ class SubscribeSubscriberCommandHandler
*
* @param \CachetHQ\Cachet\Commands\Subscriber\SubscribeSubscriberCommand $command
*
* @throws \CachetHQ\Cachet\Exceptions\AlreadySubscribedException
*
* @return \CachetHQ\Cachet\Models\Subscriber
*/
public function handle(SubscribeSubscriberCommand $command)
{
if (Subscriber::where('email', $command->email)->first()) {
throw new AlreadySubscribedException("Cannot subscribe {$command->email} because they're already subscribed.");
}
$subscriber = Subscriber::create(['email' => $command->email]);
if ($command->verified) {

View File

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