yiisoft/friendly-exception
Defines FriendlyExceptionInterface for exceptions that provide a human-friendly name and suggested solution. Lets error handlers detect these exceptions and display clearer, actionable information on error pages. Includes guidance for writing short, markdown-based solutions.
Installation
composer require yiisoft/friendly-exception:^1.2
Ensure your project meets the PHP 7.4+ requirement (now enforced in 1.2.0).
Basic Usage
For Laravel, register the exception handler in app/Exceptions/Handler.php:
use Yiisoft\FriendlyException\ExceptionHandler;
public function register()
{
$this->renderable(function (Throwable $e) {
return (new ExceptionHandler())->handle($e);
});
}
First Use Case Trigger an exception in a route:
Route::get('/test-error', function () {
throw new \RuntimeException('Test error for debugging');
});
Visit /test-error to see a formatted error page with:
Environment-Specific Rendering
Use ExceptionHandler with environment checks (PHP 7.4+ features supported):
$handler = new ExceptionHandler();
if (app()->environment('local')) {
$handler->setDebugMode(true);
}
Custom Error Pages
Extend Renderer to modify output (PHP 7.4+ typed properties compatible):
use Yiisoft\FriendlyException\Renderer\Renderer;
class CustomRenderer extends Renderer
{
protected function renderException(Throwable $exception): string
{
return "<div class='custom-error'>" . parent::renderException($exception) . "</div>";
}
}
Integration with Laravel’s Exception Handler
Override render() in Handler.php (PHP 7.4+ arrow functions supported):
public function render($request, Throwable $exception)
{
return (new ExceptionHandler())->handle($exception);
}
Logging Exceptions
Combine with Laravel’s logging (PHP 7.4+ Log channel improvements):
$handler = new ExceptionHandler();
$handler->setLogger(\Log::channel('single'));
StackTraceRenderer for syntax-highlighted code snippets (PHP 7.4+ token parsing).APP_ENV to control verbosity:
$handler = new ExceptionHandler(['debug' => app()->isLocal()]);
class DebugExceptionMiddleware
{
public function __construct(private ExceptionHandler $handler) {}
public function handle($request, Closure $next)
{
try {
return $next($request);
} catch (Throwable $e) {
return $this->handler->handle($e);
}
}
}
PHP Version Compatibility
yiisoft/friendly-exception:^1.1 in composer.json.composer validate --strict
Production Safety
debug: false in non-local environments:
$handler = new ExceptionHandler(['debug' => false]);
APP_DEBUG env var (PHP 7.4+ env() helper):
$handler = new ExceptionHandler(['debug' => (bool) env('APP_DEBUG')]);
Stack Trace Truncation
$handler = new ExceptionHandler(['maxStackTraceLines' => 20]);
php artisan view:clear
dd() on the renderer to debug output (PHP 7.4+ return type hints):
$renderer = new Renderer();
dd($renderer->renderException($exception));
errors views in resources/views to embed friendly-exception output.Add Context Data Attach custom data to exceptions (PHP 7.4+ union types):
$exception = new \RuntimeException('Failed', 0, null);
$exception->setContext(['user_id' => auth()->id()]);
Render it in a custom renderer:
$context = $exception->getContext();
echo "<pre>User ID: {$context['user_id']}</pre>";
Plugin System
Extend Renderer to add tabs or sections (PHP 7.4+ named arguments):
class PluginRenderer extends Renderer
{
protected function renderTabs(Throwable $exception): string
{
return '<div class="plugin-tab">Custom Data</div>';
}
}
API Error Responses Return JSON for APIs (PHP 7.4+ JSON_THROW_ON_ERROR):
$handler = new ExceptionHandler(['format' => 'json']);
return response()->json($handler->handle($exception));
How can I help you explore Laravel packages today?