Installation:
composer require errors/exceptions
set_error_handler().First Use Case:
E_ERROR, E_WARNING) now throw exceptions instead of triggering error callbacks.// This will throw an \ErrorException instead of calling error_handler()
trigger_error("Test error", E_USER_ERROR);
Where to Look First:
PHP_ERROR_EXCEPTIONS env var).Exception-Based Error Handling:
set_error_handler() with this package for consistent exception handling.try {
$result = riskyOperation();
} catch (\ErrorException $e) {
// Handle errors as exceptions (e.g., log, notify, or rethrow)
Log::error("Error occurred", ['exception' => $e]);
}
Integration with Laravel:
App\Exceptions\Handler to catch \ErrorException alongside other exceptions.
public function render($request, Throwable $exception)
{
if ($exception instanceof \ErrorException) {
return response()->json([
'error' => $exception->getMessage(),
'code' => $exception->getCode(),
], $exception->getCode());
}
// ... other exception handling
}
public function handle($request, Closure $next)
{
set_error_handler(function ($severity, $message, $file, $line) {
throw new \ErrorException($message, $severity, $file, $line);
});
return $next($request);
}
Deprecation Handling:
.env:
PHP_ERROR_EXCEPTION_DEPRECATIONS=1
render():
if ($exception instanceof \ErrorException && $exception->getSeverity() === E_DEPRECATED) {
// Log or notify about deprecations
}
Testing:
PHP_ERROR_EXCEPTIONS=0
$this->expectException(\ErrorException::class);
trigger_error("Test error", E_USER_ERROR);
Archived Package:
whoops for debugging.symfony/error-handler for production.Environment Variable Quirks:
PHP_ERROR_EXCEPTIONS=0 must be set to 0 (not false, "", or null). Other values are ignored.PHP_ERROR_EXCEPTION_DEPRECATIONS) only works for E_DEPRECATED and E_USER_DEPRECATED.BC Breaks:
DISABLE_PHP_ERROR_EXCEPTIONS will fail. Update to PHP_ERROR_EXCEPTIONS=0.Performance:
Error Context Loss:
$_SERVER, $_GET) is not preserved in \ErrorException. Use debug_backtrace() if needed:
$exception = new \ErrorException($message, $severity, $file, $line, $context);
Verify Handler Registration:
var_dump(set_error_handler('var_dump') !== false); // Should return true
whoops) is overriding it.Log Unhandled Exceptions:
App\Exceptions\Handler should log uncaught \ErrorException:
public function report(Throwable $exception)
{
if ($exception instanceof \ErrorException) {
Log::critical('Unhandled error', [
'message' => $exception->getMessage(),
'file' => $exception->getFile(),
'line' => $exception->getLine(),
]);
}
}
Custom Exception Classes:
\ErrorException for domain-specific errors:
class ValidationErrorException extends \ErrorException {}
set_error_handler(function ($severity, $message) {
if (strpos($message, 'validation') !== false) {
throw new ValidationErrorException($message, $severity);
}
throw new \ErrorException($message, $severity);
});
Custom Error Handler:
composer.json):
{
"provide": {
"errors/exceptions": "your-package/v1.0.0"
}
}
Error Severity Mapping:
$severityMap = [
E_ERROR => \RuntimeException::class,
E_WARNING => \WarningException::class,
];
set_error_handler(function ($severity, $message) use ($severityMap) {
$class = $severityMap[$severity] ?? \ErrorException::class;
throw new $class($message, $severity);
});
Integration with Monolog:
ErrorHandler to log exceptions:
$log = new Monolog\Logger('name');
$handler = new Monolog\Handler\StreamHandler('php://stderr');
$log->pushHandler($handler);
set_exception_handler(function ($e) use ($log) {
$log->error('Exception', ['exception' => $e]);
});
How can I help you explore Laravel packages today?