Skip to content
Snippets Groups Projects
Unverified Commit f14311e2 authored by Christoph Wurst's avatar Christoph Wurst
Browse files

Return full exception trace

parent c65803fa
No related branches found
No related tags found
No related merge requests found
......@@ -39,6 +39,7 @@ use OCP\AppFramework\Middleware;
use OCP\AppFramework\Utility\IControllerMethodReflector;
use OCP\IConfig;
use OCP\ILogger;
use function get_class;
class ErrorMiddleware extends Middleware {
......@@ -67,6 +68,7 @@ class ErrorMiddleware extends Middleware {
* @param Controller $controller
* @param string $methodName
* @param Exception $exception
*
* @return Response
* @throws Exception
*/
......@@ -86,17 +88,29 @@ class ErrorMiddleware extends Middleware {
} else {
$this->logger->logException($exception);
if ($this->config->getSystemValue('debug', false)) {
return new JSONErrorResponse([
'debug' => true,
'type' => get_class($exception),
'message' => $exception->getMessage(),
'code' => $exception->getCode(),
'trace' => $this->filterTrace($exception->getTrace()),
], Http::STATUS_INTERNAL_SERVER_ERROR);
} else {
return new JSONErrorResponse([], Http::STATUS_INTERNAL_SERVER_ERROR);
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(?Exception $exception): ?array {
if ($exception === null) {
return null;
}
return [
'type' => get_class($exception),
'message' => $exception->getMessage(),
'code' => $exception->getCode(),
'trace' => $this->filterTrace($exception->getTrace()),
'previous' => $this->serializeException($exception->getPrevious()),
];
}
private function filterTrace(array $original): array {
......
......@@ -25,6 +25,7 @@
namespace OCA\Mail\Tests\Unit\Http\Middleware;
use ChristophWurst\Nextcloud\Testing\TestCase;
use Exception;
use OCA\Mail\Exception\NotImplemented;
use OCA\Mail\Exception\ServiceException;
use OCA\Mail\Http\Middleware\ErrorMiddleware;
......@@ -108,4 +109,20 @@ class ErrorMiddlewareTest extends TestCase {
$this->assertEquals($expectedStatus, $response->getStatus());
}
public function testSerializesRecursively() {
$inner = new Exception();
$outer = new ServiceException("Test", 0, $inner);
$controller = $this->createMock(Controller::class);
$this->reflector->expects($this->once())
->method('hasAnnotation')
->willReturn(true);
$this->logger->expects($this->once())
->method('logException')
->with($outer);
$response = $this->middleware->afterException($controller, 'index', $outer);
$this->assertInstanceOf(JSONResponse::class, $response);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment