heimdal icon indicating copy to clipboard operation
heimdal copied to clipboard

Use heimdal ExceptionHandler just in API

Open VictorAssis opened this issue 8 years ago • 3 comments

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?

VictorAssis avatar Sep 14 '17 12:09 VictorAssis

I've found a solution, but I do not know if it's for the best.

  1. Remove singleton from bootstrap/app.php;
  2. 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);
}

VictorAssis avatar Sep 15 '17 17:09 VictorAssis

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

keremcankaya0 avatar Jan 10 '18 02:01 keremcankaya0

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);
});

2jan avatar Mar 08 '18 11:03 2jan