Skip to content

Commit

Permalink
Breaking changes: updated versions Rollbar 1.5, PHP 7.1, Yii 2.0.15 (#22
Browse files Browse the repository at this point in the history
)

* update rollbar to 1.5

* added `enabled` option

* min php version 7.1
min yii version 2.0.15

* refactor rollbar logging and remove error and fatal error handlers to log exception coming from Yii2 ErrorHandler.
correctly set error level if an error is fatal
  • Loading branch information
Danil Zakablukovskiy authored and baibaratsky committed Aug 10, 2018
1 parent 567c579 commit eb391e4
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 76 deletions.
85 changes: 41 additions & 44 deletions ErrorHandlerTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

namespace baibaratsky\yii\rollbar;

use Rollbar\Rollbar;
use Rollbar\Payload\Level;
use Rollbar\Utilities;
use Rollbar\Rollbar;
use Yii;
use yii\base\ErrorException;
use yii\helpers\ArrayHelper;

trait ErrorHandlerTrait
Expand All @@ -26,51 +26,11 @@ trait ErrorHandlerTrait

public function logException($exception)
{
$ignoreException = false;
foreach (Yii::$app->get($this->rollbarComponentName)->ignoreExceptions as $ignoreRecord) {
if ($exception instanceof $ignoreRecord[0]) {
$ignoreException = true;
foreach (array_slice($ignoreRecord, 1) as $property => $range) {
if (!in_array($exception->$property, $range)) {
$ignoreException = false;
break;
}
}
if ($ignoreException) {
break;
}
}
}

if (!$ignoreException) {
$extra = $this->getPayloadData($exception);

if ($extra === null) {
$extra = [Utilities::IS_UNCAUGHT_KEY => true];
} else {
$extra = array_merge($extra, [Utilities::IS_UNCAUGHT_KEY => true]);
}

Rollbar::log(Level::error(), $exception, $extra);
}
$this->logExceptionRollbar($exception);

parent::logException($exception);
}

public function handleError($code, $message, $file, $line)
{
Rollbar::errorHandler($code, $message, $file, $line);

parent::handleError($code, $message, $file, $line);
}

public function handleFatalError()
{
Rollbar::fatalHandler();

parent::handleFatalError();
}

private function getPayloadData($exception)
{
$payloadData = $this->payloadCallback();
Expand Down Expand Up @@ -109,4 +69,41 @@ private function payloadCallback()

return $payloadData;
}
}

protected function logExceptionRollbar($exception)
{
foreach (Yii::$app->get($this->rollbarComponentName)->ignoreExceptions as $ignoreRecord) {
if ($exception instanceof $ignoreRecord[0]) {
$ignoreException = true;
foreach (array_slice($ignoreRecord, 1) as $property => $range) {
if (!in_array($exception->$property, $range)) {
$ignoreException = false;
break;
}
}
if ($ignoreException) {
return;
}
}
}
// Check if an error coming from handleError() should be ignored.
if ($exception instanceof ErrorException && Rollbar::logger()->shouldIgnoreError($exception->getCode())) {
return;
}

$extra = $this->getPayloadData($exception);
if ($extra === null) {
$extra = [];
}
$level = $this->isFatal($exception) ? Level::CRITICAL : Level::ERROR;

Rollbar::log($level, $exception, $extra, true);
}

protected function isFatal($exception): bool
{
return $exception instanceof \Error
|| ($exception instanceof ErrorException
&& ErrorException::isFatalError(['type' => $exception->getSeverity()]));
}
}
49 changes: 22 additions & 27 deletions Rollbar.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@

namespace baibaratsky\yii\rollbar;

use \Rollbar\Rollbar as BaseRollbar;
use Rollbar\Rollbar as BaseRollbar;
use Yii;
use yii\base\BaseObject;

class Rollbar extends BaseObject
{
public $enabled = true;
public $accessToken;
public $baseApiUrl = 'https://api.rollbar.com/api/1/';
public $batchSize;
Expand All @@ -30,35 +31,29 @@ class Rollbar extends BaseObject
* Format: ['name of the exception class', 'exception_property' => ['range', 'of', 'values], ...]
*/
public $ignoreExceptions = [
['yii\web\HttpException', 'statusCode' => [404]],
['yii\web\HttpException', 'statusCode' => [404]],
];

public function init()
{
BaseRollbar::init(
[
'access_token' => $this->accessToken,
'base_api_url' => $this->baseApiUrl,
'batch_size' => $this->batchSize,
'batched' => $this->batched,
'branch' => $this->branch,
'code_version' => $this->codeVersion,
'environment' => $this->environment,
'host' => $this->host,
'included_errno' => $this->includedErrno,
'logger' => $this->logger,
'person_fn' => $this->personFn,
'root' => !empty($this->root) ? Yii::getAlias($this->root) : null,
'scrub_fields' => $this->scrubFields,
'timeout' => $this->timeout,
'proxy' => $this->proxy,
'enable_utf8_sanitization' => $this->enableUtf8Sanitization,
],
false,
false,
false
);

parent::init();
BaseRollbar::init([
'enabled' => $this->enabled,
'access_token' => $this->accessToken,
'base_api_url' => $this->baseApiUrl,
'batch_size' => $this->batchSize,
'batched' => $this->batched,
'branch' => $this->branch,
'code_version' => $this->codeVersion,
'environment' => $this->environment,
'host' => $this->host,
'included_errno' => $this->includedErrno,
'logger' => $this->logger,
'person_fn' => $this->personFn,
'root' => !empty($this->root) ? Yii::getAlias($this->root) : null,
'scrub_fields' => $this->scrubFields,
'timeout' => $this->timeout,
'proxy' => $this->proxy,
'enable_utf8_sanitization' => $this->enableUtf8Sanitization,
], false, false, false);
}
}
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@
}
],
"require": {
"rollbar/rollbar": "1.3.1",
"yiisoft/yii2": ">=2.0.13"
"php": ">=7.1",
"yiisoft/yii2": "^2.0.15",
"rollbar/rollbar": "^1.5"
},
"autoload": {
"psr-4": {
Expand Down
6 changes: 3 additions & 3 deletions log/Target.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

namespace baibaratsky\yii\rollbar\log;

use Rollbar\Rollbar;
use Rollbar\Payload\Level;
use Rollbar\Rollbar;
use yii\log\Logger;

class Target extends \yii\log\Target
Expand Down Expand Up @@ -31,10 +31,10 @@ public function export()
protected static function getLevelName($level)
{
if (in_array($level,
[Logger::LEVEL_PROFILE, Logger::LEVEL_PROFILE_BEGIN, Logger::LEVEL_PROFILE_END, Logger::LEVEL_TRACE])) {
[Logger::LEVEL_PROFILE, Logger::LEVEL_PROFILE_BEGIN, Logger::LEVEL_PROFILE_END, Logger::LEVEL_TRACE])) {
return 'debug';
}

return Logger::getLevelName($level);
}
}
}

0 comments on commit eb391e4

Please sign in to comment.