Skip to content

Commit

Permalink
feat: Added support for scope parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
gaowei-space committed Apr 12, 2023
1 parent fd403fc commit 7cb7bfc
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 8 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@ $options = [
'sample_rate' => 1,
'http_timeout' => 0.5,
],
'scope_user' => [
'id' => 10,
'username' => '丹',
],
'scope_tags' => [
'game_role' => '菜鸟',
],
];
ErrorHandler::init($options);
```
Expand Down
7 changes: 7 additions & 0 deletions README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@ $options = [
'sample_rate' => 1,
'http_timeout' => 0.5,
],
'scope_user' => [
'id' => 10,
'username' => 'dan',
],
'scope_tags' => [
'game_role' => 'rookie',
],
];
ErrorHandler::init($options);
```
Expand Down
4 changes: 3 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
{
"name": "gaowei-space\/error-handler",
"name": "gaowei-space/error-handler",
"description": "ErrorHandler is used to catch all php runtime errors and supports reporting to monolog or sentry.",
"license": "MIT",
"homepage": "https://github.com/gaowei-space/error-handler",
"authors": [
{
"name": "gaowei",
"email": "[email protected]"
}
],
"version": "1.2.0",
"require": {
"php": "^7.2|^8.0",
"sentry/sdk": "^3.2",
Expand Down
14 changes: 14 additions & 0 deletions examples/ErrorHandlerExample.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,13 @@ public function testInitForSentry($code)
'sample_rate' => 1, // report rate, float range 0-1
'http_timeout' => 0.5,
],
'scope_user' => [
'id' => 100,
'username' => 'dan',
],
'scope_tags' => [
'game_role' => 'rookie'
],
];
ErrorHandler::init($options);

Expand All @@ -70,6 +77,13 @@ public function testCaptureMessageForSentry($message, $level)
'sample_rate' => 1, // report rate, float range 0-1
'http_timeout' => 0.5,
],
'scope_user' => [
'id' => 100,
'username' => 'dan',
],
'scope_tags' => [
'game_role' => 'rookie'
],
];
ErrorHandler::create($options)->captureMessage($message, $level);
}
Expand Down
40 changes: 33 additions & 7 deletions src/ErrorHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
*
* @author gaowei <[email protected]>
*
* @version 1.0.2
* @version 1.2.0
*
* @copyright gaowei
* @created_at 2022-05-26
* @updated_at 2022-08-01
* @updated_at 2023-04-12
*/

namespace GaoweiSpace\ErrorHandler;
Expand All @@ -19,18 +19,22 @@
class ErrorHandler
{
public $logger;
public $handler = 'logger'; // logger | sentry
public $handler = 'logger'; // logger | sentry
public $display_errors = false;
public $sentry_options = [];
public $report_level = E_ALL;
public $report_level = E_ALL;
public $scope_user = [];
public $scope_tags = [];

public function __construct(array $options = [])
{
$this->display_errors = $options['display_errors'];
$this->handler = $options['handler'];
$this->logger = $options['logger'];
$this->handler = $options['handler'];
$this->logger = $options['logger'];
$this->sentry_options = $options['sentry_options'];
$this->report_level = $options['report_level'];
$this->report_level = $options['report_level'];
$this->scope_user = $options['scope_user'];
$this->scope_tags = $options['scope_tags'];
}

public static function init(array $options = [])
Expand Down Expand Up @@ -58,6 +62,8 @@ private static function _setDefaultOptions(array &$options)
'sentry_options' => [],
'display_errors' => false,
'report_level' => E_ALL,
'scope_user' => [],
'scope_tags' => [],
], $options);
}

Expand Down Expand Up @@ -146,9 +152,29 @@ private function _sentryCaptureException(\Throwable $exception)
}

\Sentry\init($this->sentry_options);

$this->_setScope();

\Sentry\captureException($exception);
}

private function _setScope()
{
$scope_user = $this->scope_user;
$scope_tags = $this->scope_tags;
if (!$scope_user && !$scope_tags) {
return false;
}

\Sentry\configureScope(function (\Sentry\State\Scope $scope) use ($scope_user, $scope_tags): void {
$scope->setUser($scope_user);

foreach ($scope_tags as $tag_name => $tag_value) {
$scope->setTag($tag_name, $tag_value);
}
});
}

private function _sentryCaptureMessage($message, $level)
{
$this->_sentryCaptureException(new \ErrorException($message, 0, $this->_getErrorType($level)));
Expand Down

0 comments on commit 7cb7bfc

Please sign in to comment.