From 2c9a6aa45632371c8fc23e1be7003eef7fec54fe Mon Sep 17 00:00:00 2001 From: Yuri Kuznetsov Date: Fri, 4 Sep 2020 12:50:18 +0300 Subject: [PATCH] fix exception --- application/Espo/Core/Api/ErrorOutput.php | 30 +++++++++++++++++------ 1 file changed, 23 insertions(+), 7 deletions(-) diff --git a/application/Espo/Core/Api/ErrorOutput.php b/application/Espo/Core/Api/ErrorOutput.php index fd123c0677..28c94ea9d2 100644 --- a/application/Espo/Core/Api/ErrorOutput.php +++ b/application/Espo/Core/Api/ErrorOutput.php @@ -32,6 +32,8 @@ namespace Espo\Core\Api; use Espo\Core\{ Api\Request, Api\Response, + Exceptions\Conflict, + Exceptions\Error, }; use Throwable; @@ -156,13 +158,8 @@ class ErrorOutput $response->setHeader('X-Status-Reason', $this->stripInvalidCharactersFromHeaderValue($message)); } - $exceptionBody = null; - if (method_exists($exception, 'getBody')) { - $exceptionBody = $exception->getBody(); - } - - if ($exceptionBody) { - $response->writeBody($exceptionBody); + if ($this->doesExceptionHaveBody($exception)) { + $response->writeBody($exception->getBody()); $toPrintBody = false; } @@ -182,6 +179,25 @@ class ErrorOutput } } + protected function doesExceptionHaveBody(Throwable $exception) : bool + { + if ( + ! $exception instanceof Error + && + ! $exception instanceof Conflict + ) { + return false; + } + + $exceptionBody = null; + + if (method_exists($exception, 'getBody')) { + $exceptionBody = $exception->getBody(); + } + + return $exceptionBody !== null; + } + protected function getCodeDescription(int $statusCode) : ?string { if (isset($this->errorDescriptions[$statusCode])) {