error handling body

This commit is contained in:
Yuri Kuznetsov
2020-09-04 12:07:53 +03:00
parent bc01c9f7c6
commit c8ea3f869e
5 changed files with 92 additions and 20 deletions
+17 -5
View File
@@ -68,7 +68,7 @@ class ErrorOutput
public function process(
Response $response,
Throwable $exception,
bool $toPrint = false,
bool $toPrintBody = false,
?array $route = null,
?array $routeParams = null
) {
@@ -135,7 +135,7 @@ class ErrorOutput
$GLOBALS['log']->log($logLevel, $logMessage);
$toPrintXStatusReason = true;
$toPrintBodyXStatusReason = true;
if (
$exception &&
@@ -143,7 +143,7 @@ class ErrorOutput
get_class($exception), $this->ignorePrintXStatusReasonExceptionClassNameList
)
) {
$toPrintXStatusReason = false;
$toPrintBodyXStatusReason = false;
}
if (!in_array($statusCode, $this->allowedStatusCodeList)) {
@@ -152,12 +152,24 @@ class ErrorOutput
$response->setStatus($statusCode);
if ($toPrintXStatusReason) {
if ($toPrintBodyXStatusReason) {
$response->setHeader('X-Status-Reason', $this->stripInvalidCharactersFromHeaderValue($message));
}
if ($toPrint) {
$exceptionBody = null;
if (method_exists($exception, 'getBody')) {
$exceptionBody = $exception->getBody();
}
if ($exceptionBody) {
$response->writeBody($exceptionBody);
$toPrintBody = false;
}
if ($toPrintBody) {
$statusText = $this->getCodeDescription($statusCode);
$statusText = isset($statusText) ?
$statusCode . ' '. $statusText :
'HTTP ' . $statusCode;
@@ -0,0 +1,49 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM - Open Source CRM application.
* Copyright (C) 2014-2020 Yuri Kuznetsov, Taras Machyshyn, Oleksiy Avramenko
* Website: https://www.espocrm.com
*
* EspoCRM is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* EspoCRM is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with EspoCRM. If not, see http://www.gnu.org/licenses/.
*
* The interactive user interfaces in modified source and object code versions
* of this program must display Appropriate Legal Notices, as required under
* Section 5 of the GNU General Public License version 3.
*
* In accordance with Section 7(b) of the GNU General Public License version 3,
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
************************************************************************/
namespace Espo\Core\Exceptions;
trait BodyTrait
{
protected $body = null;
public static function createWithBody(string $reason, string $body) : self
{
$exception = new self($reason);
$exception->body = $body;
return $exception;
}
public function getBody() : ?string
{
return $this->body;
}
}
@@ -29,7 +29,11 @@
namespace Espo\Core\Exceptions;
class Conflict extends \Exception
use Exception;
class Conflict extends Exception
{
use BodyTrait;
protected $code = 409;
}
+1 -1
View File
@@ -31,5 +31,5 @@ namespace Espo\Core\Exceptions;
class Error extends InternalServerError
{
use BodyTrait;
}
+20 -13
View File
@@ -932,20 +932,27 @@ class Record implements Crud,
protected function processDuplicateCheck(Entity $entity, $data)
{
if (empty($data->_skipDuplicateCheck) && empty($data->skipDuplicateCheck) && empty($data->forceDuplicate)) {
$duplicateList = $this->findDuplicates($entity, $data);
if (!empty($duplicateList)) {
$data = [];
foreach ($duplicateList as $e) {
$data[$e->id] = $e->getValueMap();
}
$reason = [
'reason' => 'Duplicate',
'data' => $data
];
throw new ConflictSilent(json_encode($reason));
}
if (
!empty($data->_skipDuplicateCheck) ||
!empty($data->skipDuplicateCheck) ||
!empty($data->forceDuplicate)
) {
return;
}
$duplicateList = $this->findDuplicates($entity, $data);
if (empty($duplicateList)) {
return;
}
$list = [];
foreach ($duplicateList as $e) {
$list[] = $e->getValueMap();
}
throw ConflictSilent::createWithBody('duplicate', json_encode($list));
}
public function populateDefaults(Entity $entity, $data)