解决使用 Laravel Octane 时,尽管 report() 返回 true,自定义异常仍被 Datadog 报告为错误的问题 | GGAMI.NETApp
\
Exceptions
;
use
Exception
;
use
Illuminate
\
Http
\
JsonResponse
;
use
Illuminate
\
Http
\
Request
;
class
CustomErrorException
extends
Exception
{
public
function
render
(
Request
$request
)
:
JsonResponse
{
return
response
(
)
->
json
(
[
'success'
=>
false
,
'message'
=>
$this
->
message
,
]
,
200
)
;
}
public
function
report
(
)
:
bool
{
return
true
;
}
}
我创建了一个 CustomErrorException,并将 report() 方法设置为返回 true,如上面的代码所示。在 Laravel 中,这样可以防止异常被报告。因此,使用 PHP-FPM 和 Datadog 时,它不会被记录为错误。
但是,使用 Octane + Datadog 时,即使在这种情况下,该异常仍会被报告为错误。
要解决此问题,可以将以下内容添加到 $dontReport 中的 Handler.php 数组:
这样,即使使用 Octane + Datadog,该异常也不会再被报告为错误。
简而言之,在 php-fpm + datadog 中,调用 report() 并使其返回 true 后,exception error 不会被统计;但在 octane + datadog 中,即使 report() 返回 true,仍会出现被统计为 error 的问题。
要解决这个问题,只需将 Exception class 添加到 Handler.php 的 $dontReport 中即可。