Skip to content

Commit

Permalink
Convert exceptions (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
barryvdh authored and freekmurze committed Oct 4, 2016
1 parent 08ea9db commit 7729c5d
Show file tree
Hide file tree
Showing 10 changed files with 78 additions and 13 deletions.
7 changes: 6 additions & 1 deletion src/Controller.php
Expand Up @@ -8,6 +8,7 @@
use Illuminate\Routing\Controller as IlluminateController;
use Spatie\SlashCommand\Exceptions\InvalidHandler;
use Spatie\SlashCommand\Exceptions\InvalidRequest;
use Spatie\SlashCommand\Exceptions\SlashException;
use Spatie\SlashCommand\Exceptions\RequestCouldNotBeHandled;

class Controller extends IlluminateController
Expand All @@ -31,7 +32,11 @@ public function getResponse(): IlluminateResponse

$handler = $this->determineHandler();

$response = $handler->handle($this->request);
try {
$response = $handler->handle($this->request);
} catch (SlashException $e) {
$response = $e->getResponse($this->request);
}

return $response->getIlluminateResponse();
}
Expand Down
3 changes: 1 addition & 2 deletions src/Exceptions/FieldCannotBeAdded.php
Expand Up @@ -2,10 +2,9 @@

namespace Spatie\SlashCommand\Exceptions;

use Exception;
use Spatie\SlashCommand\AttachmentField;

class FieldCannotBeAdded extends Exception
class FieldCannotBeAdded extends SlashException
{
public static function invalidType()
{
Expand Down
3 changes: 1 addition & 2 deletions src/Exceptions/InvalidHandler.php
Expand Up @@ -2,10 +2,9 @@

namespace Spatie\SlashCommand\Exceptions;

use Exception;
use Spatie\SlashCommand\Handlers\BaseHandler;

class InvalidHandler extends Exception
class InvalidHandler extends SlashException
{
public static function handlerDoesNotExist($handler)
{
Expand Down
30 changes: 30 additions & 0 deletions src/Exceptions/InvalidInput.php
@@ -0,0 +1,30 @@
<?php

namespace Spatie\SlashCommand\Exceptions;

use Exception;
use Spatie\SlashCommand\Attachment;
use Spatie\SlashCommand\Request;
use Spatie\SlashCommand\Response;
use Spatie\SlashCommand\Handlers\SignatureHandler;

class InvalidInput extends SlashException
{
protected $handler;

public function __construct($message, SignatureHandler $handler, Exception $previous = null)
{
parent::__construct($message, 0, $previous);

$this->handler = $handler;
}

public function getResponse(Request $request): Response
{
return parent::getResponse($request)
->withAttachment(
Attachment::create()
->setText($this->handler->getHelpDescription())
);
}
}
4 changes: 1 addition & 3 deletions src/Exceptions/InvalidRequest.php
Expand Up @@ -2,9 +2,7 @@

namespace Spatie\SlashCommand\Exceptions;

use Exception;

class InvalidRequest extends Exception
class InvalidRequest extends SlashException
{
public static function tokenNotFound()
{
Expand Down
4 changes: 1 addition & 3 deletions src/Exceptions/InvalidSignature.php
Expand Up @@ -2,9 +2,7 @@

namespace Spatie\SlashCommand\Exceptions;

use Exception;

class InvalidSignature extends Exception
class InvalidSignature extends SlashException
{
public static function signatureMustContainASpace($signature)
{
Expand Down
3 changes: 1 addition & 2 deletions src/Exceptions/RequestCouldNotBeHandled.php
Expand Up @@ -2,10 +2,9 @@

namespace Spatie\SlashCommand\Exceptions;

use Exception;
use Spatie\SlashCommand\Request;

class RequestCouldNotBeHandled extends Exception
class RequestCouldNotBeHandled extends SlashException
{
public static function noHandlerFound(Request $request)
{
Expand Down
21 changes: 21 additions & 0 deletions src/Exceptions/SlashException.php
@@ -0,0 +1,21 @@
<?php

namespace Spatie\SlashCommand\Exceptions;

use Exception;
use Spatie\SlashCommand\Attachment;
use Spatie\SlashCommand\Request;
use Spatie\SlashCommand\Response;

class SlashException extends Exception
{
public function getResponse(Request $request): Response
{
return Response::create($request)
->withAttachment(Attachment::create()
->setText($this->getMessage())
->setFallback($this->getMessage())
->setColor('danger')
);
}
}
6 changes: 6 additions & 0 deletions src/Handlers/BaseHandler.php
Expand Up @@ -4,6 +4,7 @@

use Illuminate\Contracts\Bus\Dispatcher;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Spatie\SlashCommand\Exceptions\SlashException;
use Spatie\SlashCommand\HandlesSlashCommand;
use Spatie\SlashCommand\Jobs\SlashCommandResponseJob;
use Spatie\SlashCommand\Request;
Expand Down Expand Up @@ -33,6 +34,11 @@ protected function dispatch(SlashCommandResponseJob $job)
return app(Dispatcher::class)->dispatch($job);
}

protected function abort($response)
{
throw new SlashException($response);
}

public function getRequest(): Request
{
return $this->getRequest();
Expand Down
10 changes: 10 additions & 0 deletions src/Handlers/SignatureHandler.php
Expand Up @@ -5,6 +5,7 @@
use Illuminate\Console\Parser;
use Illuminate\Support\Str;
use Spatie\SlashCommand\Exceptions\InvalidHandler;
use Spatie\SlashCommand\Exceptions\InvalidInput;
use Spatie\SlashCommand\Request;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Exception\RuntimeException;
Expand Down Expand Up @@ -164,4 +165,13 @@ protected function bindSignature()

return true;
}

protected function validate()
{
try {
$this->input->validate();
} catch (RuntimeException $e) {
throw new InvalidInput($e->getMessage(), $this, $e);
}
}
}

0 comments on commit 7729c5d

Please sign in to comment.