Reportable and renderable methods in exception handler with Laravel 11 service provider (modules).
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use Illuminate\Contracts\Debug\ExceptionHandler as AppExceptionHandler;
use App\Exception\Handler;
class ExceptionHandlerProvider extends ServiceProvider
{
/**
* Register any application services.
*/
public function register(): void
{
$this->app->singleton(AppExceptionHandler::class, Handler::class);
}
}
<?php
namespace App\Exception;
use Throwable;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use Illuminate\Http\Request;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
class Handler extends ExceptionHandler
{
public function render($request, Throwable $exception)
{
if ($exception instanceof \Exception) {
// Custom error handling logic for HTTP requests
}
return parent::render($request, $exception);
}
/**
* Register the exception handling callbacks for the application.
*/
public function register(): void
{
$this->reportable(function (Throwable $e) {
//
});
// Model not found exception
$this->renderable(function (NotFoundHttpException $e, Request $request) {
if ($request->is('web/api/*')) {
return response()->json([
'message' => 'Record not found.'
], 404);
}
});
}
}
<?php
return [
App\Providers\AppServiceProvider::class,
App\Providers\ExceptionHandlerProvider::class,
];