mirror of
https://github.com/shlinkio/shlink.git
synced 2024-12-22 15:13:59 -06:00
Removed content-based-error-handler in preparation for the problem details module
This commit is contained in:
parent
6ddb60d047
commit
a0510d6a69
@ -15,7 +15,6 @@
|
||||
"php": "^7.2",
|
||||
"ext-json": "*",
|
||||
"ext-pdo": "*",
|
||||
"acelaya/ze-content-based-error-handler": "^3.0",
|
||||
"akrabat/ip-address-middleware": "^1.0",
|
||||
"cakephp/chronos": "^1.2",
|
||||
"cocur/slugify": "^3.0",
|
||||
|
@ -10,9 +10,20 @@ use Zend\Stratigility\Middleware\ErrorHandler;
|
||||
return [
|
||||
|
||||
'middleware_pipeline' => [
|
||||
'pre-routing' => [
|
||||
'error-handler' => [
|
||||
'middleware' => [
|
||||
ErrorHandler::class,
|
||||
],
|
||||
'priority' => 15,
|
||||
],
|
||||
// 'error-handler-rest' => [
|
||||
// 'path' => '/rest',
|
||||
// 'middleware' => [],
|
||||
// 'priority' => 14,
|
||||
// ],
|
||||
|
||||
'pre-routing' => [
|
||||
'middleware' => [
|
||||
Expressive\Helper\ContentLengthMiddleware::class,
|
||||
Common\Middleware\CloseDbConnectionMiddleware::class,
|
||||
],
|
||||
|
@ -4,7 +4,6 @@ declare(strict_types=1);
|
||||
|
||||
namespace Shlinkio\Shlink;
|
||||
|
||||
use Acelaya\ExpressiveErrorHandler;
|
||||
use Zend\ConfigAggregator;
|
||||
use Zend\Expressive;
|
||||
|
||||
@ -16,7 +15,6 @@ return (new ConfigAggregator\ConfigAggregator([
|
||||
Expressive\Router\FastRouteRouter\ConfigProvider::class,
|
||||
Expressive\Plates\ConfigProvider::class,
|
||||
Expressive\Swoole\ConfigProvider::class,
|
||||
ExpressiveErrorHandler\ConfigProvider::class,
|
||||
Common\ConfigProvider::class,
|
||||
IpGeolocation\ConfigProvider::class,
|
||||
Core\ConfigProvider::class,
|
||||
|
@ -1,21 +0,0 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use Shlinkio\Shlink\Rest\ErrorHandler\JsonErrorResponseGenerator;
|
||||
|
||||
return [
|
||||
|
||||
'error_handler' => [
|
||||
'plugins' => [
|
||||
'invokables' => [
|
||||
'application/json' => JsonErrorResponseGenerator::class,
|
||||
],
|
||||
'aliases' => [
|
||||
'application/x-json' => 'application/json',
|
||||
'text/json' => 'application/json',
|
||||
],
|
||||
],
|
||||
],
|
||||
|
||||
];
|
@ -15,8 +15,8 @@ use Zend\Diactoros\Response\JsonResponse;
|
||||
class HealthAction extends AbstractRestAction
|
||||
{
|
||||
private const HEALTH_CONTENT_TYPE = 'application/health+json';
|
||||
private const PASS_STATUS = 'pass';
|
||||
private const FAIL_STATUS = 'fail';
|
||||
private const STATUS_PASS = 'pass';
|
||||
private const STATUS_FAIL = 'fail';
|
||||
|
||||
protected const ROUTE_PATH = '/health';
|
||||
protected const ROUTE_ALLOWED_METHODS = [self::METHOD_GET];
|
||||
@ -48,7 +48,7 @@ class HealthAction extends AbstractRestAction
|
||||
|
||||
$statusCode = $connected ? self::STATUS_OK : self::STATUS_SERVICE_UNAVAILABLE;
|
||||
return new JsonResponse([
|
||||
'status' => $connected ? self::PASS_STATUS : self::FAIL_STATUS,
|
||||
'status' => $connected ? self::STATUS_PASS : self::STATUS_FAIL,
|
||||
'version' => $this->options->getVersion(),
|
||||
'links' => [
|
||||
'about' => 'https://shlink.io',
|
||||
|
@ -1,44 +0,0 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Shlinkio\Shlink\Rest\ErrorHandler;
|
||||
|
||||
use Acelaya\ExpressiveErrorHandler\ErrorHandler\ErrorResponseGeneratorInterface;
|
||||
use Fig\Http\Message\StatusCodeInterface;
|
||||
use Psr\Http\Message\ResponseInterface as Response;
|
||||
use Psr\Http\Message\ServerRequestInterface as Request;
|
||||
use Throwable;
|
||||
use Zend\Diactoros\Response\JsonResponse;
|
||||
|
||||
use function str_replace;
|
||||
use function strtoupper;
|
||||
|
||||
class JsonErrorResponseGenerator implements ErrorResponseGeneratorInterface, StatusCodeInterface
|
||||
{
|
||||
/**
|
||||
* Final handler for an application.
|
||||
*
|
||||
* @param \Throwable|\Exception $e
|
||||
* @param Request $request
|
||||
* @param Response $response
|
||||
* @return Response
|
||||
* @throws \InvalidArgumentException
|
||||
*/
|
||||
public function __invoke(?Throwable $e, Request $request, Response $response)
|
||||
{
|
||||
$status = $response->getStatusCode();
|
||||
$responsePhrase = $status < 400 ? 'Internal Server Error' : $response->getReasonPhrase();
|
||||
$status = $status < 400 ? self::STATUS_INTERNAL_SERVER_ERROR : $status;
|
||||
|
||||
return new JsonResponse([
|
||||
'error' => $this->responsePhraseToCode($responsePhrase),
|
||||
'message' => $responsePhrase,
|
||||
], $status);
|
||||
}
|
||||
|
||||
private function responsePhraseToCode(string $responsePhrase): string
|
||||
{
|
||||
return strtoupper(str_replace(' ', '_', $responsePhrase));
|
||||
}
|
||||
}
|
@ -18,11 +18,10 @@ class ConfigProviderTest extends TestCase
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function properConfigIsReturned()
|
||||
public function properConfigIsReturned(): void
|
||||
{
|
||||
$config = $this->configProvider->__invoke();
|
||||
|
||||
$this->assertArrayHasKey('error_handler', $config);
|
||||
$this->assertArrayHasKey('routes', $config);
|
||||
$this->assertArrayHasKey('dependencies', $config);
|
||||
}
|
||||
|
@ -1,62 +0,0 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace ShlinkioTest\Shlink\Rest\ErrorHandler;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Shlinkio\Shlink\Rest\ErrorHandler\JsonErrorResponseGenerator;
|
||||
use Zend\Diactoros\Response;
|
||||
use Zend\Diactoros\ServerRequest;
|
||||
|
||||
use function array_map;
|
||||
use function range;
|
||||
|
||||
class JsonErrorResponseGeneratorTest extends TestCase
|
||||
{
|
||||
/** @var JsonErrorResponseGenerator */
|
||||
private $errorHandler;
|
||||
|
||||
public function setUp(): void
|
||||
{
|
||||
$this->errorHandler = new JsonErrorResponseGenerator();
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function noErrorStatusReturnsInternalServerError(): void
|
||||
{
|
||||
/** @var Response\JsonResponse $response */
|
||||
$response = $this->errorHandler->__invoke(null, new ServerRequest(), new Response());
|
||||
$payload = $response->getPayload();
|
||||
|
||||
$this->assertInstanceOf(Response\JsonResponse::class, $response);
|
||||
$this->assertEquals(500, $response->getStatusCode());
|
||||
$this->assertEquals('Internal Server Error', $payload['message']);
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
* @dataProvider provideStatus
|
||||
*/
|
||||
public function errorStatusReturnsThatStatus(int $status, string $message): void
|
||||
{
|
||||
/** @var Response\JsonResponse $response */
|
||||
$response = $this->errorHandler->__invoke(
|
||||
null,
|
||||
new ServerRequest(),
|
||||
(new Response())->withStatus($status, $message)
|
||||
);
|
||||
$payload = $response->getPayload();
|
||||
|
||||
$this->assertInstanceOf(Response\JsonResponse::class, $response);
|
||||
$this->assertEquals($status, $response->getStatusCode());
|
||||
$this->assertEquals($message, $payload['message']);
|
||||
}
|
||||
|
||||
public function provideStatus(): iterable
|
||||
{
|
||||
return array_map(function (int $status) {
|
||||
return [$status, 'Some message'];
|
||||
}, range(400, 500, 20));
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user