Last active
May 16, 2017 22:28
-
-
Save paulofreitas/6ba4c12f4098931ec0ca8f8ae934e569 to your computer and use it in GitHub Desktop.
A custom package/module exception handler that decorates the Laravel's app exception handler. And also a nice introduction to the Decorator and Chain of Responsibility design patterns.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Package\Exceptions; | |
use Exception; | |
use Illuminate\Contracts\Debug\ExceptionHandler as HandlerContract; | |
class Handler implements HandlerContract | |
{ | |
/** | |
* The exception handler being decorated. | |
* | |
* @var \Illuminate\Contracts\Debug\ExceptionHandler | |
*/ | |
protected $exceptionHandler; | |
/** | |
* Create a new exception handler instance. | |
* | |
* @param \Illuminate\Contracts\Debug\ExceptionHandler $exceptionHandler | |
* @return void | |
*/ | |
public function __construct(HandlerContract $exceptionHandler) | |
{ | |
$this->exceptionHandler = $exceptionHandler; | |
} | |
/** | |
* Report or log an exception. | |
* | |
* @param \Exception $exception | |
* @return void | |
* | |
* @throws \Exception | |
*/ | |
public function report(Exception $exception) | |
{ | |
// Handle whatever you want... | |
// Fallback to app handler | |
$this->exceptionHandler->report($exception); | |
} | |
/** | |
* Render an exception into a response. | |
* | |
* @param \Illuminate\Http\Request $request | |
* @param \Exception $exception | |
* @return \Symfony\Component\HttpFoundation\Response | |
*/ | |
public function render($request, Exception $exception) | |
{ | |
// Handle whatever you want... | |
// Fallback to app handler | |
return $this->exceptionHandler->render($request, $exception); | |
} | |
/** | |
* Render an exception to the console. | |
* | |
* @param \Symfony\Component\Console\Output\OutputInterface $output | |
* @param \Exception $exception | |
* @return void | |
*/ | |
public function renderForConsole($output, Exception $exception) | |
{ | |
// Handle whatever you want... | |
// Fallback to app handler | |
$this->exceptionHandler->renderForConsole($output, $exception); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Package; | |
use Illuminate\Contracts\Debug\ExceptionHandler as HandlerContract; | |
use Illuminate\Support\ServiceProvider as AbstractServiceProvider; | |
use Package\Exceptions\Handler; | |
class ServiceProvider extends AbstractServiceProvider | |
{ | |
/** | |
* Bootstrap the package services. | |
* | |
* @return void | |
*/ | |
public function boot() | |
{ | |
// | |
} | |
/** | |
* Register the package services. | |
* | |
* @return void | |
*/ | |
public function register() | |
{ | |
$this->app->extend(HandlerContract::class, function ($appHandler) { | |
return new Handler($appHandler); | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment