Skip to content

Commit

Permalink
Explicitly handle QueryException.
Browse files Browse the repository at this point in the history
Signed-off-by: Mior Muhammad Zaki <crynobone@gmail.com>
  • Loading branch information
crynobone committed Aug 19, 2020
1 parent 09c3deb commit 5781437
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 13 deletions.
37 changes: 25 additions & 12 deletions src/Http/ExceptionHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,27 @@ public function handle($exception): Reply
return $this->handleJsonRpcException($exception);
}

return $this->handleGenericException($exception);
return $this->handleException($exception);
}

/**
* Handle generic Exception.
*
* @param \Throwable|\Error $exception
*/
public function handleException($exception): Reply
{
\report($exception);

return new Reply(\json_encode([
'jsonrpc' => Server::VERSION,
'id' => null,
'error' => [
'code' => -32603,
'message' => \get_class($exception).' - '.$exception->getMessage(),
'exception' => \get_class($exception),
],
]));
}

/**
Expand Down Expand Up @@ -94,19 +114,12 @@ protected function handleValidationException(ValidationException $exception): Re
* Handle generic Exception.
*
* @param \Throwable|\Error $exception
*
* @deprecated v2.5.0 and will be removed in v3.0.0
* @see self::handleException()
*/
protected function handleGenericException($exception): Reply
{
\report($exception);

return new Reply(\json_encode([
'jsonrpc' => Server::VERSION,
'id' => null,
'error' => [
'code' => -32603,
'message' => \get_class($exception).' - '.$exception->getMessage(),
'exception' => \get_class($exception),
],
]));
return $this->handleException($exception);
}
}
6 changes: 5 additions & 1 deletion src/Http/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Datto\JsonRpc\Evaluator;
use ErrorException;
use Illuminate\Contracts\Container\Container;
use Illuminate\Database\QueryException;
use Illuminate\Pipeline\Pipeline;
use Minions\Configuration;
use Minions\Exceptions\ProjectNotFound;
Expand Down Expand Up @@ -94,6 +95,7 @@ public function handle(ServerRequestInterface $request)
{
$project = $request->getHeader('X-Request-ID')[0] ?? null;
$app = $this->container;
$exceptionHandler = new ExceptionHandler();

try {
$config = $this->projectConfiguration($project);
Expand All @@ -106,8 +108,10 @@ public function handle(ServerRequestInterface $request)
])->then(static function (Message $message) use ($app) {
return $app->make('minions.controller')->handle($message);
});
} catch (QueryException $exception) {
return $exceptionHandler->handleException($exception);
} catch (ErrorException | Throwable $exception) {
return (new ExceptionHandler())->handle($exception);
return $exceptionHandler->handle($exception);
}
}

Expand Down

0 comments on commit 5781437

Please sign in to comment.