-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Added support for scope parameter
- Loading branch information
1 parent
fd403fc
commit 7cb7bfc
Showing
5 changed files
with
64 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
@@ -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 = []) | ||
|
@@ -58,6 +62,8 @@ private static function _setDefaultOptions(array &$options) | |
'sentry_options' => [], | ||
'display_errors' => false, | ||
'report_level' => E_ALL, | ||
'scope_user' => [], | ||
'scope_tags' => [], | ||
], $options); | ||
} | ||
|
||
|
@@ -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))); | ||
|