mirror of
https://github.com/shlinkio/shlink.git
synced 2024-11-23 01:16:23 -06:00
Handled connection exceptions in Health action
This commit is contained in:
parent
d58e24bce5
commit
144a5415da
@ -8,6 +8,7 @@ use Psr\Http\Message\ResponseInterface;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use Shlinkio\Shlink\Core\Options\AppOptions;
|
||||
use Throwable;
|
||||
use Zend\Diactoros\Response\JsonResponse;
|
||||
|
||||
class HealthAction extends AbstractRestAction
|
||||
@ -38,9 +39,13 @@ class HealthAction extends AbstractRestAction
|
||||
*/
|
||||
public function handle(ServerRequestInterface $request): ResponseInterface
|
||||
{
|
||||
$connected = $this->conn->ping();
|
||||
$statusCode = $connected ? self::STATUS_OK : self::STATUS_SERVICE_UNAVAILABLE;
|
||||
try {
|
||||
$connected = $this->conn->ping();
|
||||
} catch (Throwable $e) {
|
||||
$connected = false;
|
||||
}
|
||||
|
||||
$statusCode = $connected ? self::STATUS_OK : self::STATUS_SERVICE_UNAVAILABLE;
|
||||
return new JsonResponse([
|
||||
'status' => $connected ? self::PASS_STATUS : self::FAIL_STATUS,
|
||||
'version' => $this->options->getVersion(),
|
||||
|
@ -4,6 +4,7 @@ declare(strict_types=1);
|
||||
namespace ShlinkioTest\Shlink\Rest\Action;
|
||||
|
||||
use Doctrine\DBAL\Connection;
|
||||
use Exception;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Prophecy\Prophecy\ObjectProphecy;
|
||||
use Shlinkio\Shlink\Core\Options\AppOptions;
|
||||
@ -67,4 +68,26 @@ class HealthActionTest extends TestCase
|
||||
$this->assertEquals('application/health+json', $resp->getHeaderLine('Content-type'));
|
||||
$ping->shouldHaveBeenCalledOnce();
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function failResponseIsReturnedWhenConnectionThrowsException()
|
||||
{
|
||||
$ping = $this->conn->ping()->willThrow(Exception::class);
|
||||
|
||||
/** @var JsonResponse $resp */
|
||||
$resp = $this->action->handle(new ServerRequest());
|
||||
$payload = $resp->getPayload();
|
||||
|
||||
$this->assertEquals(503, $resp->getStatusCode());
|
||||
$this->assertEquals('fail', $payload['status']);
|
||||
$this->assertEquals('1.2.3', $payload['version']);
|
||||
$this->assertEquals([
|
||||
'about' => 'https://shlink.io',
|
||||
'project' => 'https://github.com/shlinkio/shlink',
|
||||
], $payload['links']);
|
||||
$this->assertEquals('application/health+json', $resp->getHeaderLine('Content-type'));
|
||||
$ping->shouldHaveBeenCalledOnce();
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user