filp/whoops is an exceptional PHP library designed to provide a much more user-friendly and informative error and exception handling experience during development. Instead of the default plain PHP error messages or a blank 'white screen of death', Whoops intercepts errors and unhandled exceptions, presenting a beautiful, interactive, and highly detailed error page directly in the browser.
The primary goal of Whoops is to significantly improve the developer's debugging workflow by presenting all relevant information about an error in an organized and digestible format. This includes:
1. Pretty Error Pages: It generates an elegant HTML page displaying the error message, the type of error/exception, the file and line number where it occurred.
2. Interactive Stack Trace: A full, clickable stack trace is provided, allowing developers to navigate through the call stack and inspect the code at each level.
3. Code Context: For each step in the stack trace, Whoops displays a snippet of the surrounding code, making it easy to pinpoint the exact line causing the issue without leaving the browser.
4. Request Data Inspection: It presents various contextual data related to the request, such as GET, POST, FILES, SESSION, COOKIE, and SERVER variables, which are often crucial for understanding the state of the application when the error occurred.
5. Environment Variables: Displays environment variables, aiding in debugging configuration-related issues.
6. Customizable Handlers: Beyond the default 'PrettyPageHandler', Whoops allows developers to define custom handlers for different output formats (e.g., JSON for API debugging, plain text for command-line scripts, XML) or for integrating with logging services. This flexibility makes it suitable for various application types.
7. Easy Integration: It's designed to be easily integrated into any PHP project, whether it's a standalone script, a micro-framework, or a full-fledged MVC framework.
In essence, filp/whoops transforms raw, often cryptic PHP errors into a powerful debugging tool that saves developers time and effort by providing immediate, comprehensive insight into what went wrong and where. It's a must-have dependency for any serious PHP development environment.
Example Code
<?php
require __DIR__ . '/vendor/autoload.php';
// 1. Create a Whoops\Run instance
$whoops = new \Whoops\Run;
// 2. Add a PrettyPageHandler to the Whoops instance.
// This handler renders the beautiful HTML error page.
$whoops->pushHandler(new \Whoops\Handler\PrettyPageHandler);
// If you want to customize the PrettyPageHandler further,
// for example, to set an application name:
// $handler = new \Whoops\Handler\PrettyPageHandler;
// $handler->setPageTitle("My Awesome App Error");
// $whoops->pushHandler($handler);
// For API applications, you might want a JSON handler:
// $whoops->pushHandler(new \Whoops\Handler\JsonResponseHandler);
// 3. Register Whoops as the default error and exception handler.
$whoops->register();
// --- Example Code Triggering Errors ---
echo "Starting application...\n";
// This will cause a division by zero error (E_WARNING)
// Which Whoops will convert to an ErrorException and handle.
$result = 10 / 0; // This line will trigger the error
echo "This line will not be reached.\n";
// Alternatively, you can throw an explicit exception:
// throw new \Exception("Something went terribly wrong!");
// If no error or exception occurs, Whoops does nothing.
echo "Application finished successfully.\n";
?>








filp/whoops