This commit is contained in:
Yuri Kuznetsov
2022-12-20 14:33:06 +02:00
parent 6cfe9b6b43
commit 185a24f007
12 changed files with 62 additions and 25 deletions
@@ -32,6 +32,9 @@ namespace Espo\Core\Exceptions;
use Throwable;
use Exception;
/**
* A bad request exception. Main purpose is for the 400 Bad Request HTTP error.
*/
class BadRequest extends Exception implements HasBody
{
/** @var int */
@@ -43,10 +46,12 @@ class BadRequest extends Exception implements HasBody
parent::__construct($message, $code, $previous);
}
/**
* Create with a body (supposed to be sent to the frontend).
*/
public static function createWithBody(string $reason, string $body): self
{
$exception = new static($reason);
$exception->body = $body;
return $exception;
@@ -32,6 +32,9 @@ namespace Espo\Core\Exceptions;
use Exception;
use Throwable;
/**
* A conflict exception. Main purpose is for the 409 Conflict HTTP error.
*/
class Conflict extends Exception implements HasBody
{
/** @var int */
@@ -43,6 +46,9 @@ class Conflict extends Exception implements HasBody
parent::__construct($message, $code, $previous);
}
/**
* Create with a body (supposed to be sent to the frontend).
*/
public static function createWithBody(string $reason, string $body): self
{
$exception = new static($reason);
@@ -31,6 +31,9 @@ namespace Espo\Core\Exceptions;
use Throwable;
/**
* An exception for 500 Internal Server Error.
*/
class Error extends InternalServerError implements HasBody
{
private ?string $body = null;
@@ -40,6 +43,9 @@ class Error extends InternalServerError implements HasBody
parent::__construct($message, $code, $previous);
}
/**
* Create with a body (supposed to be sent to the frontend).
*/
public static function createWithBody(string $message, string $body): self
{
$exception = new static($message);
@@ -31,15 +31,15 @@ namespace Espo\Core\Exceptions\Error;
use Espo\Core\Utils\Json;
/**
* A wrapper for error message data for the frontend. Supposed to be passed encoded to `createWithBody`
* methods of exceptions.
*/
class Body
{
private ?string $messageTranslationLabel = null;
private ?string $messageTranslationScope = null;
/**
* @var ?array<string,string>
*/
/** @var ?array<string, string> */
private ?array $messageTranslationData = null;
public static function create(): self
@@ -50,7 +50,7 @@ class Body
/**
* A translatable message to display in frontend. Labels should be in the `messages` category.
*
* @param ?array<string,string> $data
* @param ?array<string, string> $data
*/
public function withMessageTranslation(string $label, ?string $scope = null, ?array $data = null): self
{
@@ -32,13 +32,13 @@ namespace Espo\Core\Exceptions;
use Throwable;
use Exception;
/**
* A forbidden exception. Main purpose is for the 403 Forbidden HTTP error.
*/
class Forbidden extends Exception implements HasBody
{
/**
* @var int
*/
/** @var int */
protected $code = 403;
private ?string $body = null;
final public function __construct(string $message = '', int $code = 0, Throwable $previous = null)
@@ -46,6 +46,9 @@ class Forbidden extends Exception implements HasBody
parent::__construct($message, $code, $previous);
}
/**
* Create with a body (supposed to be sent to the frontend).
*/
public static function createWithBody(string $message, string $body): self
{
$exception = new static($message);
@@ -29,7 +29,13 @@
namespace Espo\Core\Exceptions;
/**
* Has a body supposed to be sent to the frontend.
*/
interface HasBody
{
/**
* Get a body (supposed to be sent to the frontend).
*/
public function getBody(): ?string;
}
@@ -29,7 +29,13 @@
namespace Espo\Core\Exceptions;
/**
* Has a specific log level (for the logger).
*/
interface HasLogLevel
{
/**
* Get a log level.
*/
public function getLogLevel(): string;
}
@@ -29,7 +29,13 @@
namespace Espo\Core\Exceptions;
/**
* Has a log message (for the logger).
*/
interface HasLogMessage
{
/**
* Get a log message.
*/
public function getLogMessage(): string;
}
@@ -31,8 +31,6 @@ namespace Espo\Core\Exceptions;
class InternalServerError extends \Exception
{
/**
* @var int
*/
/** @var int */
protected $code = 500;
}
@@ -29,10 +29,13 @@
namespace Espo\Core\Exceptions;
class NotFound extends \Exception
use Exception;
/**
* A not-found exception. Main purpose is for the 404 Not Found HTTP error.
*/
class NotFound extends Exception
{
/**
* @var int
*/
/** @var int */
protected $code = 404;
}
@@ -31,8 +31,6 @@ namespace Espo\Core\Exceptions;
class ServiceUnavailable extends \Exception
{
/**
* @var int
*/
/** @var int */
protected $code = 503;
}
@@ -29,10 +29,10 @@
namespace Espo\Core\Exceptions;
class Unauthorized extends \Exception
use Exception;
class Unauthorized extends Exception
{
/**
* @var int
*/
/** @var int */
protected $code = 401;
}