Skip to content

Commit

Permalink
fix extending Laravel Exception Handler
Browse files Browse the repository at this point in the history
  • Loading branch information
Christian Bohollo Sáez committed Jun 25, 2022
1 parent d98b284 commit 9582de7
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 7 deletions.
30 changes: 26 additions & 4 deletions src/app/Exceptions/CustomHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,21 @@

namespace Omatech\Mage\App\Exceptions;

use Illuminate\Contracts\Container\Container;
use Throwable;
use Illuminate\Foundation\Exceptions\Handler;
use Illuminate\Contracts\Debug\ExceptionHandler as ExceptionHandlerContract;
use Illuminate\Support\Facades\Route;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Illuminate\Database\Eloquent\ModelNotFoundException;

class CustomHandler extends Handler
class CustomHandler implements ExceptionHandlerContract
{
public function __construct(Container $container, ExceptionHandlerContract $appExceptionHandler)
{
$this->container = $container;
$this->appExceptionHandler = $appExceptionHandler;
}

/**
* Render an exception into an HTTP response.
*
Expand Down Expand Up @@ -44,12 +51,12 @@ public function render($request, Throwable $exception)
if ($request->route()->getName() == 'mage.auth.login') {
return redirect()->route('mage.auth.login')->with('status', 'unauthorized');
}

return $this->exception(401, $request, $exception);
}
}

return parent::render($request, $exception);
return $this->appExceptionHandler->render($request, $e);
}

private function exception($code, $request, $exception)
Expand All @@ -63,4 +70,19 @@ private function exception($code, $request, $exception)

return Route::respondWithRoute("mage.error$code", $request);
}

public function report(Throwable $e)
{
$this->appExceptionHandler->report($e);
}

public function renderForConsole($output, Throwable $e)
{
$this->appExceptionHandler->renderForConsole($output, $e);
}

public function shouldReport(Throwable $e)
{
return $this->appExceptionHandler->shouldReport($e);
}
}
12 changes: 9 additions & 3 deletions src/app/Providers/ExceptionServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,21 @@

namespace Omatech\Mage\App\Providers;

use Illuminate\Support\Facades\Gate;
use Illuminate\Contracts\Debug\ExceptionHandler as ExceptionHandlerContract;
use Illuminate\Support\ServiceProvider;
use Omatech\Mage\App\Exceptions\CustomHandler;
use Illuminate\Contracts\Debug\ExceptionHandler;

class ExceptionServiceProvider extends ServiceProvider
{
public function register()
{
$this->app->bind(ExceptionHandler::class, CustomHandler::class);
$appExceptionHandler = $this->app->make(ExceptionHandlerContract::class);

$this->app->singleton(
ExceptionHandlerContract::class,
function ($app) use ($appExceptionHandler) {
return new CustomHandler($app, $appExceptionHandler);
}
);
}
}

0 comments on commit 9582de7

Please sign in to comment.