Skip to content

Commit

Permalink
Payload data callback
Browse files Browse the repository at this point in the history
  • Loading branch information
baibaratsky committed Feb 5, 2016
1 parent a153c7f commit a22f78f
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 3 deletions.
33 changes: 32 additions & 1 deletion ErrorHandlerTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,18 @@ trait ErrorHandlerTrait
{
public $rollbarComponentName = 'rollbar';

/**
* @var callable Callback returning a payload data associative array or null
* Example:
* function (ErrorHandler $errorHandler) {
* return [
* 'foo' => 'bar',
* 'xyz' => getSomeData(),
* ];
* }
*/
public $payloadDataCallback;

public function handleException($exception)
{
$ignoreException = false;
Expand All @@ -28,7 +40,7 @@ public function handleException($exception)
}

if (!$ignoreException) {
Rollbar::report_exception($exception);
Rollbar::report_exception($exception, null, $this->getPayloadData());
}

parent::handleException($exception);
Expand All @@ -47,4 +59,23 @@ public function handleFatalError()

parent::handleFatalError();
}

private function getPayloadData()
{
if (!isset($this->payloadDataCallback)) {
return null;
}

if (!is_callable($this->payloadDataCallback)) {
throw new \Exception('Incorrect callback provided');
}

$payloadData = call_user_func($this->payloadDataCallback, $this);

if (!is_array($payloadData) && !is_null($payloadData)) {
throw new \Exception('Callback returns an incorrect result');
}

return $payloadData;
}
}
12 changes: 10 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ The preferred way to install this extension is through [composer](http://getcomp

To install, either run
```
$ php composer.phar require baibaratsky/yii2-rollbar:1.2.*
$ php composer.phar require baibaratsky/yii2-rollbar:1.3.*
```
or add
```
"baibaratsky/yii2-rollbar": "1.2.*"
"baibaratsky/yii2-rollbar": "1.3.*"
```
to the `require` section of your `composer.json` file.

Expand Down Expand Up @@ -48,6 +48,14 @@ Usage
'components' => [
'errorHandler' => [
'class' => 'baibaratsky\yii\rollbar\web\ErrorHandler',

// You can include additional data in a payload:
// 'payloadDataCallback' => function (\baibaratsky\yii\rollbar\web\ErrorHandler $errorHandler) {
// return [
// 'exceptionCode' => $errorHandler->exception->getCode(),
// 'rawRequestBody' => Yii::$app->request->getRawBody(),
// ];
// },
],
],
```
Expand Down

0 comments on commit a22f78f

Please sign in to comment.