Resolve issue where custom exceptions are reported as errors in Datadog despite report() returning true when using Laravel Octane

Updated on

<?php

namespace App\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;
    }
}

I created a CustomErrorException and set the report() method to return true, as shown in the code above. In Laravel, this prevents the exception from being reported. Therefore, when using PHP-FPM with Datadog, it does not get recorded as an error.

However, with Octane + Datadog, the exception is still reported as an error even in this case.

To resolve this issue, you can add the following to the $dontReport array in Handler.php:

    protected $dontReport = [
        CustomErrorException::class,
    ];

By doing this, the exception will no longer be reported as an error, even when using Octane + Datadog.

간단하게 설명해서, php-fpm + datadog 에서는 report() true 을 하면 exception error 가 집계되지 않는데, octane + datadog 에서는 report() true 이더라도 error 로 집계되는 문제가 있다.

이 문제를 해결하려면, Handler.php$dontReport 에 Exception class를 추가해주면 해결 된다.