first version
This commit is contained in:
11
kirby/src/Exception/BadMethodCallException.php
Executable file
11
kirby/src/Exception/BadMethodCallException.php
Executable file
@@ -0,0 +1,11 @@
|
||||
<?php
|
||||
|
||||
namespace Kirby\Exception;
|
||||
|
||||
class BadMethodCallException extends Exception
|
||||
{
|
||||
protected static $defaultKey = 'invalidMethod';
|
||||
protected static $defaultFallback = 'The method "{ method }" does not exist';
|
||||
protected static $defaultHttpCode = 400;
|
||||
protected static $defaultData = ['method' => null];
|
||||
}
|
10
kirby/src/Exception/DuplicateException.php
Executable file
10
kirby/src/Exception/DuplicateException.php
Executable file
@@ -0,0 +1,10 @@
|
||||
<?php
|
||||
|
||||
namespace Kirby\Exception;
|
||||
|
||||
class DuplicateException extends Exception
|
||||
{
|
||||
protected static $defaultKey = 'duplicate';
|
||||
protected static $defaultFallback = 'The entry exists';
|
||||
protected static $defaultHttpCode = 400;
|
||||
}
|
122
kirby/src/Exception/Exception.php
Executable file
122
kirby/src/Exception/Exception.php
Executable file
@@ -0,0 +1,122 @@
|
||||
<?php
|
||||
|
||||
namespace Kirby\Exception;
|
||||
|
||||
use Kirby\Cms\App;
|
||||
use Kirby\Toolkit\I18n;
|
||||
use Kirby\Toolkit\Str;
|
||||
|
||||
class Exception extends \Exception
|
||||
{
|
||||
protected $data;
|
||||
protected $httpCode;
|
||||
protected $details;
|
||||
protected $isTranslated = true;
|
||||
|
||||
protected static $defaultKey = 'general';
|
||||
protected static $defaultFallback = 'An error occurred';
|
||||
protected static $defaultData = [];
|
||||
protected static $defaultHttpCode = 500;
|
||||
protected static $defaultDetails = [];
|
||||
|
||||
private static $prefix = 'error';
|
||||
|
||||
public function __construct($args = [])
|
||||
{
|
||||
// Set data and httpCode from provided arguments or defaults
|
||||
$this->data = $args['data'] ?? static::$defaultData;
|
||||
$this->httpCode = $args['httpCode'] ?? static::$defaultHttpCode;
|
||||
$this->details = $args['details'] ?? static::$defaultDetails;
|
||||
|
||||
if (is_string($args) === true) {
|
||||
$this->isTranslated = false;
|
||||
parent::__construct($args);
|
||||
} else {
|
||||
// Define whether message can/should be translated
|
||||
$translate = ($args['translate'] ?? true) === true && class_exists('Kirby\Cms\App') === true;
|
||||
|
||||
// Define the Exception key
|
||||
$key = self::$prefix . '.' . ($args['key'] ?? static::$defaultKey);
|
||||
|
||||
// Fallback waterfall for message string
|
||||
$message = null;
|
||||
|
||||
if ($translate) {
|
||||
// 1. Translation for provided key in current language
|
||||
// 2. Translation for provided key in default language
|
||||
if (isset($args['key']) === true) {
|
||||
$message = I18n::translate(self::$prefix . '.' . $args['key']);
|
||||
$this->isTranslated = true;
|
||||
}
|
||||
}
|
||||
|
||||
// 3. Provided fallback message
|
||||
if ($message === null) {
|
||||
$message = $args['fallback'] ?? null;
|
||||
$this->isTranslated = false;
|
||||
}
|
||||
|
||||
if ($translate) {
|
||||
// 4. Translation for default key in current language
|
||||
// 5. Translation for default key in default language
|
||||
if ($message === null) {
|
||||
$message = I18n::translate(self::$prefix . '.' . static::$defaultKey);
|
||||
$this->isTranslated = true;
|
||||
}
|
||||
}
|
||||
|
||||
// 6. Default fallback message
|
||||
if ($message === null) {
|
||||
$message = static::$defaultFallback;
|
||||
$this->isTranslated = false;
|
||||
}
|
||||
|
||||
// Format message with passed data
|
||||
$message = Str::template($message, $this->data, '-', '{', '}');
|
||||
|
||||
// Handover to Exception parent class constructor
|
||||
parent::__construct($message, null, $args['previous'] ?? null);
|
||||
|
||||
// Set the Exception code to the key
|
||||
$this->code = $key;
|
||||
}
|
||||
}
|
||||
|
||||
final public function getData(): array
|
||||
{
|
||||
return $this->data;
|
||||
}
|
||||
|
||||
final public function getDetails(): array
|
||||
{
|
||||
return $this->details;
|
||||
}
|
||||
|
||||
final public function getKey(): string
|
||||
{
|
||||
return $this->getCode();
|
||||
}
|
||||
|
||||
final public function getHttpCode(): int
|
||||
{
|
||||
return $this->httpCode;
|
||||
}
|
||||
|
||||
final public function isTranslated(): bool
|
||||
{
|
||||
return $this->isTranslated;
|
||||
}
|
||||
|
||||
public function toArray(): array
|
||||
{
|
||||
return [
|
||||
'exception' => static::class,
|
||||
'message' => $this->getMessage(),
|
||||
'key' => $this->getKey(),
|
||||
'file' => ltrim($this->getFile(), $_SERVER['DOCUMENT_ROOT'] ?? null),
|
||||
'line' => $this->getLine(),
|
||||
'details' => $this->getDetails(),
|
||||
'code' => $this->getHttpCode()
|
||||
];
|
||||
}
|
||||
}
|
11
kirby/src/Exception/InvalidArgumentException.php
Executable file
11
kirby/src/Exception/InvalidArgumentException.php
Executable file
@@ -0,0 +1,11 @@
|
||||
<?php
|
||||
|
||||
namespace Kirby\Exception;
|
||||
|
||||
class InvalidArgumentException extends Exception
|
||||
{
|
||||
protected static $defaultKey = 'invalidArgument';
|
||||
protected static $defaultFallback = 'Invalid argument "{ argument }" in method "{ method }"';
|
||||
protected static $defaultHttpCode = 400;
|
||||
protected static $defaultData = ['argument' => null, 'method' => null];
|
||||
}
|
10
kirby/src/Exception/LogicException.php
Executable file
10
kirby/src/Exception/LogicException.php
Executable file
@@ -0,0 +1,10 @@
|
||||
<?php
|
||||
|
||||
namespace Kirby\Exception;
|
||||
|
||||
class LogicException extends Exception
|
||||
{
|
||||
protected static $defaultKey = 'logic';
|
||||
protected static $defaultFallback = 'This task cannot be finished';
|
||||
protected static $defaultHttpCode = 400;
|
||||
}
|
10
kirby/src/Exception/NotFoundException.php
Executable file
10
kirby/src/Exception/NotFoundException.php
Executable file
@@ -0,0 +1,10 @@
|
||||
<?php
|
||||
|
||||
namespace Kirby\Exception;
|
||||
|
||||
class NotFoundException extends Exception
|
||||
{
|
||||
protected static $defaultKey = 'notFound';
|
||||
protected static $defaultFallback = 'Not found';
|
||||
protected static $defaultHttpCode = 404;
|
||||
}
|
10
kirby/src/Exception/PermissionException.php
Executable file
10
kirby/src/Exception/PermissionException.php
Executable file
@@ -0,0 +1,10 @@
|
||||
<?php
|
||||
|
||||
namespace Kirby\Exception;
|
||||
|
||||
class PermissionException extends Exception
|
||||
{
|
||||
protected static $defaultKey = 'permission';
|
||||
protected static $defaultFallback = 'You are not allowed to do this';
|
||||
protected static $defaultHttpCode = 403;
|
||||
}
|
Reference in New Issue
Block a user