Let ExceptionMiddleware properly return JSON on API related exceptions

Signed-off-by: Julius Härtl <jus@bitgrid.net>
This commit is contained in:
Julius Härtl
2019-03-05 19:01:55 +01:00
parent 5c7ee5c920
commit 0132dae215
3 changed files with 54 additions and 21 deletions

View File

@@ -23,8 +23,8 @@
namespace OCA\Deck\Middleware;
use OCA\Deck\Controller\PageController;
use OCA\Deck\StatusException;
use OCA\Deck\BadRequestException;
use OCP\AppFramework\Db\DoesNotExistException;
use OCP\AppFramework\Middleware;
use OCP\AppFramework\Http\JSONResponse;
@@ -33,7 +33,7 @@ use OCP\Util;
use OCP\IConfig;
class SharingMiddleware extends Middleware {
class ExceptionMiddleware extends Middleware {
/** @var ILogger */
private $logger;
@@ -71,6 +71,20 @@ class SharingMiddleware extends Middleware {
], $exception->getStatus());
}
if (strpos(get_class($controller), 'OCA\\Deck\\Controller\\') === 0) {
$response = [
'status' => 500,
'message' => $exception->getMessage()
];
if ($this->config->getSystemValue('loglevel', Util::WARN) === Util::DEBUG) {
$this->logger->logException($exception);
}
if ($this->config->getSystemValue('debug', true) === true) {
$response['exception'] = (array) $exception;
}
return new JSONResponse($response, 500);
}
// uncatched DoesNotExistExceptions will be thrown when the main entity is not found
// we return a 403 so we don't leak information over existing entries
// TODO: At some point those should properly be catched in the service classes
@@ -84,4 +98,4 @@ class SharingMiddleware extends Middleware {
throw $exception;
}
}
}