在 laravel 11 站点上,我想在事件处理程序中使用标签“InvalidDataEvent”将消息写入哨兵。
谷歌搜索我找到页面Laravel 和 Sentry:如何在 Sentry 中设置自定义标签?
并在事件处理程序中进行:
public function handle(InvalidDataEvent $event): void
{
$errorMessage = 'Invalid Data Event: ' . $event->message;
if (app()->bound('sentry')) {
app('sentry')->withScope(function (\Sentry\State\Scope $scope) use ($errorMessage): void {
$scope->setLevel(\Sentry\Severity::warning());
// ERROR POINTING AT ROW BELOW
$scope->setTag('InvalidDataEvent', app(\Illuminate\Http\Request::class)->headers->get('InvalidDataEvent'));
app('sentry')->captureException($errorMessage);
});
}
/* Sentry\State\Scope::setTag(): Argument #2 ($value) must be of type string, null given, called in /Project/app/Listeners/InvalidDataListener.php on line 26 */
if(App::isLocal()) {
throw new \ErrorException($errorMessage);
}
}
但是出现错误:
Sentry\State\Scope::setTag(): Argument #2 ($value) must be of type string, null given, called in /Project/app/Listeners/InvalidDataListener.php on line 26
a) 不清楚 $value var 是什么以及如何修复它? b) 如何将字符串 $errorMessage 转换为 Throwable 类:
app('哨兵')->captureException($errorMessage);
?
"laravel/framework": "^11.9",
"sentry/sentry-laravel": "^4.7",
提前致谢!
您的 setTag 不起作用可能是由于有时请求中 InvalidDataEvent 值不可用。因此,为了解决这个问题,您可以将默认值传递给您的 InvalidDataEvent
$customEvent = app(Request::class)->headers->get('YOUR_CUSTOM_EVENT', 'DTag');
$scope->setTag('YOUR_CUSTOM_EVENT', $customEvent);
如果无效数据事件丢失,那么它将使用 DTag
然后从错误消息生成可抛出的异常
$throwableException = new \Exception($errorMessage);
app('sentry')->captureException($throwableException);