ExceptionController: return JSON-Response on api-routes

This commit is contained in:
fipwmaqzufheoxq92ebc 2020-08-27 08:37:37 +02:00
parent 6bf19f493f
commit 1f91ebc8f0

View File

@ -4,16 +4,14 @@
namespace Grocy\Controllers; namespace Grocy\Controllers;
use Exception;
use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Message\ServerRequestInterface;
use Psr\Log\LoggerInterface; use Psr\Log\LoggerInterface;
use Slim\Exception\HttpException;
use Slim\Exception\HttpForbiddenException; use Slim\Exception\HttpForbiddenException;
use Slim\Exception\HttpInternalServerErrorException;
use Slim\Exception\HttpNotFoundException; use Slim\Exception\HttpNotFoundException;
use Slim\Exception\HttpSpecializedException;
use Throwable; use Throwable;
class ExceptionController extends BaseController class ExceptionController extends BaseApiController
{ {
/** /**
* @var \Slim\App * @var \Slim\App
@ -34,6 +32,26 @@ class ExceptionController extends BaseController
?LoggerInterface $logger = null) ?LoggerInterface $logger = null)
{ {
$response = $this->app->getResponseFactory()->createResponse(); $response = $this->app->getResponseFactory()->createResponse();
$isApiRoute = string_starts_with($request->getUri()->getPath(), '/api/');
if ($isApiRoute) {
$status = 500;
if ($exception instanceof HttpException) {
$status = $exception->getCode();
}
$data = [
'error_message' => $exception->getMessage(),
];
if ($displayErrorDetails) {
$data['error_details'] = [
'stack_trace' => $exception->getTrace(),
'previous' => $exception->getPrevious(),
'file' => $exception->getFile(),
'line' => $exception->getLine(),
];
}
return $this->ApiResponse($response->withStatus($status), $data);
}
if ($exception instanceof HttpNotFoundException) { if ($exception instanceof HttpNotFoundException) {
return $this->renderPage($response->withStatus(404), 'errors/404', [ return $this->renderPage($response->withStatus(404), 'errors/404', [
'exception' => $exception 'exception' => $exception