diff --git a/ErrorHandlerTrait.php b/ErrorHandlerTrait.php index 549c6b1..2031b74 100644 --- a/ErrorHandlerTrait.php +++ b/ErrorHandlerTrait.php @@ -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; @@ -28,7 +40,7 @@ public function handleException($exception) } if (!$ignoreException) { - Rollbar::report_exception($exception); + Rollbar::report_exception($exception, null, $this->getPayloadData()); } parent::handleException($exception); @@ -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; + } } \ No newline at end of file diff --git a/README.md b/README.md index c01bd5d..986fc56 100644 --- a/README.md +++ b/README.md @@ -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. @@ -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(), + // ]; + // }, ], ], ```