Trait luya\traits\ErrorHandlerTrait
Implemented by | luya\console\ErrorHandler, luya\web\ErrorHandler |
---|---|
Available since version | 1.0.0 |
Source Code | https://github.com/luyadev/luya/blob/master/core/traits/ErrorHandlerTrait.php |
ErrorHandler trait to extend the renderException method with an api call if enabled.
Public Properties
Property | Type | Description | Defined By |
---|---|---|---|
$api | string | The url of the error api without trailing slash. | luya\traits\ErrorHandlerTrait |
$lastTransferCall | \Curl\Curl|null | The curl object from the last error api call. | luya\traits\ErrorHandlerTrait |
$sensitiveKeys | array | luya\traits\ErrorHandlerTrait | |
$transferException | boolean | Enable the transfer of exceptions to the defined $api server. |
luya\traits\ErrorHandlerTrait |
$whitelist | array | An array of exceptions which are whitelisted and therefore NOT TRANSFERED. | luya\traits\ErrorHandlerTrait |
Public Methods
Method | Description | Defined By |
---|---|---|
getExceptionArray() | Get an readable array to transfer from an exception | luya\traits\ErrorHandlerTrait |
isExceptionWhitelisted() | Returns whether a given exception is whitelisted or not. | luya\traits\ErrorHandlerTrait |
renderException() | Renders an exception using ansi format for console output. | luya\traits\ErrorHandlerTrait |
transferMessage() | Send a custom message to the api server event its not related to an exception. | luya\traits\ErrorHandlerTrait |
Property Details
The url of the error api without trailing slash. Make sure you have installed the error api module on the requested api url (https://luya.io/guide/module/luyadev---luya-module-errorapi).
An example when using the erroapi module, the url could look like this https://luya.io/errorapi
.
The curl object from the last error api call.
'password',
'pwd',
'pass',
'passwort',
'pw',
'token',
'hash',
'authorization',
]
Enable the transfer of exceptions to the defined $api
server.
An array of exceptions which are whitelisted and therefore NOT TRANSFERED. Whitelisted exception are basically expected application logic which does not need to report informations to the developer, as the application works as expect.
'yii\base\InvalidRouteException',
'yii\web\NotFoundHttpException',
'yii\web\ForbiddenHttpException',
'yii\web\MethodNotAllowedHttpException',
'yii\web\UnauthorizedHttpException',
'yii\web\BadRequestHttpException',
]
Method Details
Get an readable array to transfer from an exception
public array getExceptionArray ( $exception ) | ||
$exception | mixed |
Exception object |
return | array |
An array with transformed exception data |
---|
public function getExceptionArray($exception)
{
$_message = 'Uknonwn exception object, not instance of \Exception.';
$_file = 'unknown';
$_line = 0;
$_trace = [];
$_previousException = [];
$_exceptionClassName = 'Unknown';
if (is_object($exception)) {
$_exceptionClassName = get_class($exception);
$prev = $exception->getPrevious();
if (!empty($prev)) {
$_previousException = [
'message' => $prev->getMessage(),
'file' => $prev->getFile(),
'line' => $prev->getLine(),
'trace' => $this->buildTrace($prev),
];
}
$_trace = $this->buildTrace($exception);
$_message = $exception->getMessage();
$_file = $exception->getFile();
$_line = $exception->getLine();
} elseif (is_string($exception)) {
$_message = 'exception string: ' . $exception;
} elseif (is_array($exception)) {
$_message = isset($exception['message']) ? $exception['message'] : 'exception array dump: ' . print_r($exception, true);
$_file = isset($exception['file']) ? $exception['file'] : __FILE__;
$_line = isset($exception['line']) ? $exception['line'] : __LINE__;
}
$exceptionName = 'Exception';
if ($exception instanceof Exception) {
$exceptionName = $exception->getName();
} elseif ($exception instanceof ErrorException) {
$exceptionName = $exception->getName();
}
return [
'message' => $_message,
'file' => $_file,
'line' => $_line,
'requestUri' => isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : null,
'serverName' => isset($_SERVER['SERVER_NAME']) ? $_SERVER['SERVER_NAME'] : null,
'date' => date('d.m.Y H:i'),
'trace' => $_trace,
'previousException' => $_previousException,
'ip' => isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : null,
'get' => isset($_GET) ? ArrayHelper::coverSensitiveValues($_GET, $this->sensitiveKeys) : [],
'post' => isset($_POST) ? ArrayHelper::coverSensitiveValues($_POST, $this->sensitiveKeys) : [],
'bodyParams' => Yii::$app instanceof Application ? ArrayHelper::coverSensitiveValues(Yii::$app->request->bodyParams) : [],
'session' => isset($_SESSION) ? ArrayHelper::coverSensitiveValues($_SESSION, $this->sensitiveKeys) : [],
'server' => isset($_SERVER) ? ArrayHelper::coverSensitiveValues($_SERVER, $this->sensitiveKeys) : [],
// since 1.0.20
'yii_debug' => YII_DEBUG,
'yii_env' => YII_ENV,
'status_code' => $exception instanceof HttpException ? $exception->statusCode : 500,
'exception_name' => $exceptionName,
'exception_class_name' => $_exceptionClassName,
'php_version' => phpversion(),
'luya_version' => Boot::VERSION,
'yii_version' => Yii::getVersion(),
'app_version' => Yii::$app->version,
];
}
Returns whether a given exception is whitelisted or not.
If an exception is whitelisted or in the liste of whitelisted exception the exception content won't be transmitted to the error api.
public boolean isExceptionWhitelisted ( $exception ) | ||
$exception | mixed | |
return | boolean |
Returns true if whitelisted, or false if not and can therefore be transmitted. |
---|
public function isExceptionWhitelisted($exception)
{
if (!is_object($exception)) {
return false;
}
if (ObjectHelper::isInstanceOf($exception, WhitelistedException::class, false)) {
return true;
}
return ObjectHelper::isInstanceOf($exception, $this->whitelist, false);
}
Renders an exception using ansi format for console output.
public void renderException ( $exception ) | ||
$exception | Throwable |
The exception to be rendered. |
public function renderException($exception)
{
if ($this->transferException && !$this->isExceptionWhitelisted($exception)) {
$this->apiServerSendData($this->getExceptionArray($exception));
}
return parent::renderException($exception);
}
Send a custom message to the api server event its not related to an exception.
Sometimes you just want to pass informations from your application, this method allows you to transfer a message to the error api server.
Example of sending a message
Yii::$app->errorHandler->transferMessage('Something went wrong here!', __FILE__, __LINE__);
public boolean|null transferMessage ( $message, $file = __FILE__, $line = __LINE__ ) | ||
$message | string |
The message you want to send to the error api server. |
$file | string |
The you are currently send the message (use FILE) |
$line | string |
The line you want to submit (use LINE) |
public function transferMessage($message, $file = __FILE__, $line = __LINE__)
{
return $this->apiServerSendData($this->getExceptionArray([
'message' => $message,
'file' => $file,
'line' => $line,
]));
}