heimdal
heimdal copied to clipboard
Use heimdal ExceptionHandler just in API
Hello, I am creating the API of a system that already has an administrator module running today. Is it possible for Laravel to use the Heindal Handler only in the API module?
I've found a solution, but I do not know if it's for the best.
- Remove singleton from bootstrap/app.php;
- Change the app/Exceptions/Handler.php render method to:
public function render($request, Exception $exception)
{
if($request->is('api/*')) {
$handler = new \Optimus\Heimdal\ExceptionHandler($this->container);
return $handler->render($request, $exception);
}
return parent::render($request, $exception);
}
Thanks for the solution @VictorAssis ` $handler = new APIExceptionHandler($this->container);
if ($request->is('api/*')) {
return $handler->render($request, $exception);
}
$handler->report($exception);
return parent::render($request, $exception);`
If anyone needs to use reporter from the library for web
You can also detect request type (API/web) during binding procedure and provide appropriate ExceptionHandler class. In your bootstrap/app.php add something similar to:
$app->singleton(Illuminate\Contracts\Debug\ExceptionHandler::class, function($app) {
if(request()->is('api/*')) {
return new Optimus\Heimdal\ExceptionHandler($app);
}
return new App\Exceptions\Handler($app);
});