diff options
author | Christoph Wurst <christoph@winzerhof-wurst.at> | 2020-01-31 18:02:11 +0300 |
---|---|---|
committer | Christoph Wurst <christoph@winzerhof-wurst.at> | 2020-02-03 17:33:24 +0300 |
commit | b0f915d79cf8338f02a90533c0f1abd5b2b0f16d (patch) | |
tree | 28ba83c51166b54545dd2ac3dd7bb5391677ae3e /lib/Http/Middleware | |
parent | ef1585e19280a393bac48f26e3cc2f4c55825f00 (diff) |
Use JSend-inspired JSON response structures
We don't want SOAP messages, but having a bit of standardized structure
in JSON responses makes it easier to interpret them in code. This is
just a start for errors. We will have to also migrate the existing
(success) respones at some point.
Signed-off-by: Christoph Wurst <christoph@winzerhof-wurst.at>
Diffstat (limited to 'lib/Http/Middleware')
-rw-r--r-- | lib/Http/Middleware/ErrorMiddleware.php | 60 |
1 files changed, 21 insertions, 39 deletions
diff --git a/lib/Http/Middleware/ErrorMiddleware.php b/lib/Http/Middleware/ErrorMiddleware.php index 957506d07..07fd4130e 100644 --- a/lib/Http/Middleware/ErrorMiddleware.php +++ b/lib/Http/Middleware/ErrorMiddleware.php @@ -29,18 +29,15 @@ namespace OCA\Mail\Http\Middleware; use Exception; use OCA\Mail\Exception\ClientException; use OCA\Mail\Exception\NotImplemented; -use OCA\Mail\Http\JSONErrorResponse; +use OCA\Mail\Http\JsonResponse; use OCP\AppFramework\Controller; use OCP\AppFramework\Db\DoesNotExistException; use OCP\AppFramework\Http; -use OCP\AppFramework\Http\JSONResponse; use OCP\AppFramework\Http\Response; use OCP\AppFramework\Middleware; use OCP\AppFramework\Utility\IControllerMethodReflector; use OCP\IConfig; use OCP\ILogger; -use Throwable; -use function get_class; class ErrorMiddleware extends Middleware { @@ -79,46 +76,31 @@ class ErrorMiddleware extends Middleware { } if ($exception instanceof ClientException) { - return new JSONErrorResponse([ + return JsonResponse::fail([ 'message' => $exception->getMessage() - ], Http::STATUS_BAD_REQUEST); - } else if ($exception instanceof DoesNotExistException) { - return new JSONResponse([], Http::STATUS_NOT_FOUND); - } else if ($exception instanceof NotImplemented) { - return new JSONResponse([], Http::STATUS_NOT_IMPLEMENTED); - } else { - $this->logger->logException($exception); - if ($this->config->getSystemValue('debug', false)) { - return new JSONErrorResponse(array_merge( - [ - 'debug' => true, - ], - $this->serializeException($exception) - ), Http::STATUS_INTERNAL_SERVER_ERROR); - } - - return new JSONErrorResponse([], Http::STATUS_INTERNAL_SERVER_ERROR); + ]); } - } - private function serializeException(?Throwable $throwable): ?array { - if ($throwable === null) { - return null; + if ($exception instanceof DoesNotExistException) { + return JSONResponse::fail([], Http::STATUS_NOT_FOUND); + } + + if ($exception instanceof NotImplemented) { + return JSONResponse::fail([], Http::STATUS_NOT_IMPLEMENTED); + } + + $this->logger->logException($exception); + if ($this->config->getSystemValue('debug', false)) { + return JsonResponse::errorFromThrowable( + $exception, + Http::STATUS_INTERNAL_SERVER_ERROR, + [ + 'debug' => true, + ] + ); } - return [ - 'type' => get_class($throwable), - 'message' => $throwable->getMessage(), - 'code' => $throwable->getCode(), - 'trace' => $this->filterTrace($throwable->getTrace()), - 'previous' => $this->serializeException($throwable->getPrevious()), - ]; - } - private function filterTrace(array $original): array { - return array_map(function (array $row) { - return array_intersect_key($row, - array_flip(['file', 'line', 'function', 'class'])); - }, $original); + return JsonResponse::error("Server error"); } } |