rst630
rst630

Reputation: 89

Laravel not calling report method in custom exception

Trying to use custom exception:

namespace App\Exceptions\Cloud;

use Exception;

class CantConfirmOrder extends Exception
{
    public function report()
    {
        info('test exception');
    }
}

But when I throwing it in tinker - nothing writes to log:

>>> throw new CantConfirmOrder('test');
[!] Aliasing 'CantConfirmOrder' to 'App\Exceptions\Cloud\CantConfirmOrder' for this Tinker session.
App\Exceptions\Cloud\CantConfirmOrder with message 'test'

Handler.php:

public function report(Throwable $exception)
{
    parent::report($exception);
}

Does I need to call report() manually with try catch? I thinked it will be called automatically when I throwing.

Upvotes: 0

Views: 230

Answers (1)

Peter
Peter

Reputation: 48958

In the context of an HTTP-request, the routing will pick this up and call the report method. So if you throw the error in a Controller method or another route action it should be called. You can try it in a test like this :

Route::get('x', fn() => throw new CantConfirmOrder('test));  $this->get('x');

The Route class/method that invokes the method is

https://laravel.com/api/9.x/Illuminate/Routing/Pipeline.html#method_handleException

Upvotes: 2

Related Questions