Maybe I'm doing it wrong, but it doesnt't work:
This is my custom Exception:
class RolesSearchNotFoundException extends Exception
{
/**
* Report the exception.
*
* @return bool|null
*/
public function report()
{
Log::warning('Hooray'); // doesnt work
}
/**
* Render the exception into an HTTP response.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function render($request)
{
}
}
This is Handler.php:
public function register()
{
$this->reportable(function (RolesSearchNotFoundException $e) {
dd($e); // this doesnt work
});
$this->renderable(function (\Exception $e) {
if ($e->getPrevious() instanceof \Illuminate\Session\TokenMismatchException) {
return redirect()->back()
->withInput(request()->except('_token'))
->withErrors(['errors' => __('app.errors.invalid_token')]);
};
});
}
And now I'm throwing my custom exception:
public function getRoleOptions(): array
{
$roles = $this->roleService->searchRoles();
$rolesOptions[''] = '';
$roles = []; // I'm making it empty for this test
if(count($roles) == 0)
throw new RolesSearchNotFoundException('Roles not found for form options');
return $roles;
}
Catching:
try {
return $this->getRoleOptions();
} catch (RolesSearchNotFoundException $exception) {
abort(500, 'Roles not found');
}
Aborting works - so it catches RolesSearchNotFoundException.
Reportable in register function doesnt receive RolesSearchNotFoundException in callback, but if I change callback argument to Throwable, dd() shows native PHP exception:
ErrorException {#1704 ▼
#message: "Undefined variable $options"
#code: 0
#file: "/app/Services/User/Retrievers/UserGetSelectOptionsWithKeyRetriever.php"
#line: 22
#severity: E_WARNING
trace: {▶}
}
Neither report() method in RolesSearchNotFoundException Logs my message.