Refactored exceptions to properly use package exceptions

This commit is contained in:
Alejandro Celaya
2017-12-30 21:35:26 +01:00
parent 745ff51150
commit ede4525332
18 changed files with 61 additions and 30 deletions

View File

@@ -3,9 +3,7 @@ declare(strict_types=1);
namespace Shlinkio\Shlink\Rest\Exception;
use Shlinkio\Shlink\Common\Exception\ExceptionInterface;
class AuthenticationException extends \RuntimeException implements ExceptionInterface
class AuthenticationException extends RuntimeException
{
public static function fromCredentials($username, $password)
{

View File

@@ -0,0 +1,8 @@
<?php
declare(strict_types=1);
namespace Shlinkio\Shlink\Rest\Exception;
interface ExceptionInterface extends \Throwable
{
}

View File

@@ -0,0 +1,8 @@
<?php
declare(strict_types=1);
namespace Shlinkio\Shlink\Rest\Exception;
class RuntimeException extends \RuntimeException implements ExceptionInterface
{
}

View File

@@ -8,7 +8,7 @@ use Interop\Http\ServerMiddleware\DelegateInterface;
use Interop\Http\ServerMiddleware\MiddlewareInterface;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Shlinkio\Shlink\Common\Exception\RuntimeException;
use Shlinkio\Shlink\Rest\Exception\RuntimeException;
class BodyParserMiddleware implements MiddlewareInterface, RequestMethodInterface
{
@@ -27,7 +27,7 @@ class BodyParserMiddleware implements MiddlewareInterface, RequestMethodInterfac
$currentParams = $request->getParsedBody();
// In requests that do not allow body or if the body has already been parsed, continue to next middleware
if (! empty($currentParams) || in_array($method, [
if (! empty($currentParams) || \in_array($method, [
self::METHOD_GET,
self::METHOD_HEAD,
self::METHOD_OPTIONS,
@@ -37,7 +37,7 @@ class BodyParserMiddleware implements MiddlewareInterface, RequestMethodInterfac
// If the accepted content is JSON, try to parse the body from JSON
$contentType = $this->getRequestContentType($request);
if (in_array($contentType, ['application/json', 'text/json', 'application/x-json'], true)) {
if (\in_array($contentType, ['application/json', 'text/json', 'application/x-json'], true)) {
return $delegate->process($this->parseFromJson($request));
}
@@ -48,27 +48,28 @@ class BodyParserMiddleware implements MiddlewareInterface, RequestMethodInterfac
* @param Request $request
* @return string
*/
protected function getRequestContentType(Request $request)
private function getRequestContentType(Request $request): string
{
$contentType = $request->getHeaderLine('Content-type');
$contentTypes = explode(';', $contentType);
return trim(array_shift($contentTypes));
$contentTypes = \explode(';', $contentType);
return \trim(\array_shift($contentTypes));
}
/**
* @param Request $request
* @return Request
* @throws RuntimeException
*/
protected function parseFromJson(Request $request)
private function parseFromJson(Request $request): Request
{
$rawBody = (string) $request->getBody();
if (empty($rawBody)) {
return $request;
}
$parsedJson = json_decode($rawBody, true);
if (json_last_error() !== JSON_ERROR_NONE) {
throw new RuntimeException(sprintf('Error when parsing JSON request body: %s', json_last_error_msg()));
$parsedJson = \json_decode($rawBody, true);
if (\json_last_error() !== JSON_ERROR_NONE) {
throw new RuntimeException(\sprintf('Error when parsing JSON request body: %s', \json_last_error_msg()));
}
return $request->withParsedBody($parsedJson);
@@ -78,7 +79,7 @@ class BodyParserMiddleware implements MiddlewareInterface, RequestMethodInterfac
* @param Request $request
* @return Request
*/
protected function parseFromUrlEncoded(Request $request)
private function parseFromUrlEncoded(Request $request): Request
{
$rawBody = (string) $request->getBody();
if (empty($rawBody)) {

View File

@@ -20,7 +20,7 @@ class RestUtils
const NOT_FOUND_ERROR = 'NOT_FOUND';
const UNKNOWN_ERROR = 'UNKNOWN_ERROR';
public static function getRestErrorCodeFromException(Common\ExceptionInterface $e)
public static function getRestErrorCodeFromException(\Throwable $e)
{
switch (true) {
case $e instanceof Core\InvalidShortCodeException: