-
Notifications
You must be signed in to change notification settings - Fork 181
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #668 from tighten/jbk/exception-handling
Improve error handling
- Loading branch information
Showing
9 changed files
with
327 additions
and
6 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
<?php | ||
|
||
namespace TightenCo\Jigsaw\Bootstrap; | ||
|
||
use ErrorException; | ||
use Exception; | ||
use Illuminate\Contracts\Debug\ExceptionHandler; | ||
use Symfony\Component\Console\Output\ConsoleOutput; | ||
use Symfony\Component\ErrorHandler\Error\FatalError; | ||
use Throwable; | ||
use TightenCo\Jigsaw\Container; | ||
use TightenCo\Jigsaw\Exceptions\DeprecationException; | ||
|
||
class HandleExceptions | ||
{ | ||
public static $reservedMemory; | ||
|
||
protected static ?Container $app; | ||
|
||
public static function forgetApp(): void | ||
{ | ||
static::$app = null; | ||
} | ||
|
||
public function bootstrap(Container $app): void | ||
{ | ||
self::$reservedMemory = str_repeat('x', 32768); | ||
|
||
static::$app = $app; | ||
|
||
error_reporting(-1); | ||
|
||
set_error_handler($this->forwardTo('handleError')); | ||
set_exception_handler($this->forwardTo('handleException')); | ||
register_shutdown_function($this->forwardTo('handleShutdown')); | ||
|
||
/* @internal The '__testing' binding is for Jigsaw development only and may be removed. */ | ||
if (! $app->has('__testing') || ! $app['__testing']) { | ||
ini_set('display_errors', 'Off'); | ||
} | ||
} | ||
|
||
/** | ||
* Report PHP deprecations, or convert PHP errors to ErrorException instances. | ||
* | ||
* @param int $level | ||
* @param string $message | ||
* @param string $file | ||
* @param int $line | ||
* @param array $context | ||
* | ||
* @throws ErrorException | ||
*/ | ||
private function handleError($level, $message, $file = '', $line = 0, $context = []): void | ||
{ | ||
if (in_array($level, [E_DEPRECATED, E_USER_DEPRECATED])) { | ||
$this->handleDeprecation(new DeprecationException($message, 0, $level, $file, $line)); | ||
|
||
return; | ||
} | ||
|
||
if (error_reporting() & $level) { | ||
throw new ErrorException($message, 0, $level, $file, $line); | ||
} | ||
} | ||
|
||
/** | ||
* Handle a deprecation. | ||
* | ||
* @throws \TightenCo\Jigsaw\Exceptions\DeprecationException | ||
*/ | ||
private function handleDeprecation(Throwable $e): void | ||
{ | ||
/* @internal The '__testing' binding is for Jigsaw development only and may be removed. */ | ||
if (static::$app->has('__testing') && static::$app['__testing']) { | ||
throw $e; | ||
} | ||
|
||
try { | ||
static::$app->make(ExceptionHandler::class)->report($e); | ||
} catch (Exception $e) { | ||
// | ||
} | ||
|
||
static::$app->make(ExceptionHandler::class)->renderForConsole(new ConsoleOutput, $e); | ||
} | ||
|
||
/** | ||
* Handle an uncaught exception from the application. | ||
* | ||
* Note: Most exceptions can be handled in a try / catch block higher | ||
* in the app, but fatal error exceptions must be handled | ||
* differently since they are not normal exceptions. | ||
*/ | ||
private function handleException(Throwable $e): void | ||
{ | ||
self::$reservedMemory = null; | ||
|
||
try { | ||
static::$app->make(ExceptionHandler::class)->report($e); | ||
} catch (Exception $e) { | ||
// | ||
} | ||
|
||
static::$app->make(ExceptionHandler::class)->renderForConsole(new ConsoleOutput, $e); | ||
} | ||
|
||
/** | ||
* Handle the PHP shutdown event. | ||
*/ | ||
private function handleShutdown(): void | ||
{ | ||
self::$reservedMemory = null; | ||
|
||
if ( | ||
! is_null($error = error_get_last()) | ||
&& in_array($error['type'], [E_COMPILE_ERROR, E_CORE_ERROR, E_ERROR, E_PARSE]) | ||
) { | ||
$this->handleException(new FatalError($error['message'], 0, $error, 0)); | ||
} | ||
} | ||
|
||
/** | ||
* Forward a method call to the given method on this class if an application instance exists. | ||
*/ | ||
private function forwardTo(string $method): callable | ||
{ | ||
return fn (...$arguments) => static::$app ? $this->{$method}(...$arguments) : false; | ||
} | ||
} |
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
|
||
namespace TightenCo\Jigsaw\Exceptions; | ||
|
||
use ErrorException; | ||
|
||
class DeprecationException extends ErrorException | ||
{ | ||
// | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
<?php | ||
|
||
namespace TightenCo\Jigsaw\Exceptions; | ||
|
||
use Closure; | ||
use Illuminate\Console\View\Components\BulletList; | ||
use Illuminate\Console\View\Components\Error; | ||
use Illuminate\Console\View\Components\Warn; | ||
use Illuminate\Contracts\Debug\ExceptionHandler; | ||
use Illuminate\Support\Traits\ReflectsClosures; | ||
use Illuminate\View\ViewException; | ||
use InvalidArgumentException; | ||
use NunoMaduro\Collision\Adapters\Laravel\Inspector; | ||
use NunoMaduro\Collision\Provider; | ||
use Symfony\Component\Console\Application as ConsoleApplication; | ||
use Symfony\Component\Console\Exception\CommandNotFoundException; | ||
use Symfony\Component\Console\Exception\ExceptionInterface as SymfonyConsoleExceptionInterface; | ||
use Throwable; | ||
|
||
class Handler implements ExceptionHandler | ||
{ | ||
use ReflectsClosures; | ||
|
||
/** @var array<string, Closure> */ | ||
private array $exceptionMap = []; | ||
|
||
public function report(Throwable $e): void | ||
{ | ||
// | ||
} | ||
|
||
public function shouldReport(Throwable $e): bool | ||
{ | ||
return true; | ||
} | ||
|
||
public function render($request, Throwable $e): void | ||
{ | ||
// | ||
} | ||
|
||
/** | ||
* @param \Symfony\Component\Console\Output\OutputInterface $output | ||
*/ | ||
public function renderForConsole($output, Throwable $e): void | ||
{ | ||
if ($e instanceof CommandNotFoundException) { | ||
$message = str($e->getMessage())->explode('.')->first(); | ||
|
||
if (! empty($alternatives = $e->getAlternatives())) { | ||
(new Error($output))->render("{$message}. Did you mean one of these?"); | ||
(new BulletList($output))->render($alternatives); | ||
} else { | ||
(new Error($output))->render($message); | ||
} | ||
|
||
return; | ||
} | ||
|
||
if ($e instanceof SymfonyConsoleExceptionInterface) { | ||
(new ConsoleApplication)->renderThrowable($e, $output); | ||
|
||
return; | ||
} | ||
|
||
if ($e instanceof DeprecationException) { | ||
// If the deprecation appears to have come from a compiled Blade view, wrap it in | ||
// a ViewException and map it manually so Ignition will add the uncompiled path | ||
if (preg_match('/cache\/\w+\.php$/', $e->getFile()) === 1) { | ||
$e = $this->mapException( | ||
new ViewException("{$e->getMessage()} (View: )", 0, 1, $e->getFile(), $e->getLine(), $e), | ||
); | ||
} | ||
|
||
(new Warn($output))->render("{$e->getMessage()} in {$e->getFile()} on line {$e->getLine()}"); | ||
|
||
return; | ||
} | ||
|
||
$e = $this->mapException($e); | ||
|
||
/** @var \NunoMaduro\Collision\Provider $provider */ | ||
$provider = app(Provider::class); | ||
|
||
$handler = $provider->register()->getHandler()->setOutput($output); | ||
$handler->setInspector(new Inspector($e)); | ||
|
||
$handler->handle(); | ||
} | ||
|
||
public function map(Closure|string $from, Closure|string|null $to = null): static | ||
{ | ||
if (is_string($to)) { | ||
$to = fn ($exception) => new $to('', 0, $exception); | ||
} | ||
|
||
if (is_callable($from) && is_null($to)) { | ||
$from = $this->firstClosureParameterType($to = $from); | ||
} | ||
|
||
if (! is_string($from) || ! $to instanceof Closure) { | ||
throw new InvalidArgumentException('Invalid exception mapping.'); | ||
} | ||
|
||
$this->exceptionMap[$from] = $to; | ||
|
||
return $this; | ||
} | ||
|
||
protected function mapException(Throwable $e): Throwable | ||
{ | ||
if (method_exists($e, 'getInnerException') && ($inner = $e->getInnerException()) instanceof Throwable) { | ||
return $inner; | ||
} | ||
|
||
foreach ($this->exceptionMap as $class => $mapper) { | ||
if ($e instanceof $class) { | ||
return $mapper($e); | ||
} | ||
} | ||
|
||
return $e; | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
namespace TightenCo\Jigsaw\Providers; | ||
|
||
use Illuminate\Contracts\Debug\ExceptionHandler; | ||
use Illuminate\View\ViewException; | ||
use Spatie\LaravelIgnition\Views\ViewExceptionMapper; | ||
use TightenCo\Jigsaw\Support\ServiceProvider; | ||
|
||
class ExceptionServiceProvider extends ServiceProvider | ||
{ | ||
public function register(): void | ||
{ | ||
$this->app->make(ExceptionHandler::class)->map( | ||
fn (ViewException $e) => $this->app->make(ViewExceptionMapper::class)->map($e), | ||
); | ||
} | ||
} |
Oops, something went wrong.