Merge pull request #1203 from cachethq/exceptions
Exception Improvements
This commit is contained in:
24
app/Exceptions/AlreadySubscribedException.php
Normal file
24
app/Exceptions/AlreadySubscribedException.php
Normal 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
|
||||
{
|
||||
//
|
||||
}
|
||||
22
app/Exceptions/ExceptionInterface.php
Normal file
22
app/Exceptions/ExceptionInterface.php
Normal 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
|
||||
{
|
||||
//
|
||||
}
|
||||
@@ -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;
|
||||
@@ -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) {
|
||||
|
||||
@@ -27,7 +27,7 @@ return [
|
||||
|
||||
'transformers' => [
|
||||
'GrahamCampbell\Exceptions\Transformers\CsrfTransformer',
|
||||
'CachetHQ\Cachet\Exceptions\Transformers\ModelNotFoundTransformer',
|
||||
'CachetHQ\Cachet\Exceptions\Transformers\ExceptionTransformer',
|
||||
],
|
||||
|
||||
/*
|
||||
|
||||
Reference in New Issue
Block a user