Commit 15503c3b authored by Matthias Adrowski's avatar Matthias Adrowski
Browse files

[TASK] Move processRequest try->catch up to dispatcher

parent 06494ace
......@@ -157,35 +157,6 @@ abstract class AbstractRestController extends ActionController {
}
}
/**
* Processes the request (additionally we catch any AJAX exceptions to provide better error messages)
*
* @param RequestInterface $request
* @param ResponseInterface $response
* @throws Exception
*/
public function processRequest(RequestInterface $request): \Psr\Http\Message\ResponseInterface {
try {
return parent::processRequest($request);
} catch (Exception $exception) {
$logger = GeneralUtility::makeInstance(LogManager::class)->getLogger(self::class);
$logger->critical(
'AbstractRestController::processRequest - Request failed with an exception',
[
'message' => $exception->getMessage(),
'code' => $exception->getCode(),
'line' => $exception->getLine(),
'trace' => $exception->getTraceAsString()
]
);
$this->response = new Response();
$this->response->setStatus($exception->getCode() < 1000 ? $exception->getCode() : 500);
$this->response->addAdditionalHeaderData($_SERVER['SERVER_PROTOCOL'] . ' 500 Internal Server Error');
$this->response->appendContent(json_encode(['message' => $exception->getMessage()]));
return $this->response;
}
}
/**
* Prepare the request parameter for the given property name. This is only called for properties of a type that
* inherits from AbstractEntity.
......
......@@ -33,7 +33,9 @@ use SGalinski\SgRest\Service\DataResolveService;
use SGalinski\SgRest\Utility\PathUtility;
use TYPO3\CMS\Core\Context\Context;
use TYPO3\CMS\Core\Context\TypoScriptAspect;
use TYPO3\CMS\Core\Http\Response;
use TYPO3\CMS\Core\Log\LogLevel;
use TYPO3\CMS\Core\Log\LogManager;
use TYPO3\CMS\Core\Utility\ExtensionManagementUtility;
use TYPO3\CMS\Core\Utility\GeneralUtility;
......@@ -198,6 +200,42 @@ class RestDispatcher extends AbstractRestMiddleware {
);
}
return $handler->handle($request);
try {
return $handler->handle($request);
} catch (\Exception $exception) {
return $this->getErrorResponse($exception);
}
}
/**
* Builds an Error Repsonse from catched exception
*
* @param \Exception $exception
* @return Response
*/
private function getErrorResponse(\Exception $exception): Response {
$logger = GeneralUtility::makeInstance(LogManager::class)->getLogger(self::class);
$logger->critical(
'AbstractAjaxController::processRequest - Request failed with an exception',
[
'message' => $exception->getMessage(),
'code' => $exception->getCode(),
'line' => $exception->getLine(),
'trace' => $exception->getTraceAsString()
]
);
if (version_compare(\TYPO3\CMS\Core\Utility\VersionNumberUtility::getCurrentTypo3Version(), '11.0.0', '<')) {
$response = new Response();
$response->setStatus(500);
$response->addAdditionalHeaderData($_SERVER['SERVER_PROTOCOL'] . ' 500 Internal Server Error');
$response->appendContent($exception->getMessage());
} else {
$message = $exception->getMessage();
$stream = fopen('php://temp', 'r+');
fwrite($stream, $message);
$response = new Response($stream, 500);
}
return $response;
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment