The Symfony Debug Component basically does two things:
- It lets you quickly register a common exception/error-handler in your PHP project
- It prvides ready-to-use formatters for exceptions and errors to print them to HTML or the command line interface
The compontent is totally independant from the rest of the Symfony framework and it can also be easily customized by extending the built-in exception handlers. An advantage over other exception handlers like whoops is in my oppinion, that the built-in HTML handler does not use any JavaScript, so the exception will be correctly displayed anywhere - on a regular web page, in ajax requests and event in all kinds of browser development tools.
Quick start
To use the Debug component, you need to register it's handlers first. Technically, it contains three independent parts: an error handler, an exception handler and a the DebugClassLoader which helps debug autoloaders. You can either register the all at once or each one separately.
// Register all handlers at once \Symfony\Component\Debug\Debug::enable(); // OR register each separately \Symfony\Component\Debug\ExceptionHandler::register(); \Symfony\Component\Debug\ErrorHandler::register();
That's it! Now any error or exception will produce nice and informative output.
Dumping exceptions to variables
By default, the Debug Component will directly send a response to the browser after it has finished formatting the exception. This means, nothing of your application will be visible - just the error screen. Alternatively you can dump the exception to a variable and print it whereever you like. This is a great help, if our application already has some sort of error visualizer. It is also usefull for logging, sending error reports by mail, etc.
use Symfony\Component\Debug\ExceptionHandler; use Symfony\Component\Debug\Exception\FlattenException; public function printException(\Throwable $exception){ $handler = new ExceptionHandler(); $flattened_exception = FlattenException::create($exception); $output = "<style>" . $handler->getStylesheet($flattened_exception) . "</style>" . $handler->getContent($flattened_exception) ; }
If you just want the stack trace, whithout the header, simply add #sf-resetcontent h1 {display: none;} to the CSS.
Getting rid of CSS conflicts
The built-in styles may, of course, conflict with your style sheets. Here is a CSS fix for a few typical problems
#sf-resetcontent {width: auto !important;} #sf-resetcontent h2.block_exception.clear_fix {margin: 0}
Also keep in mind, that the regular error page includes the following top-level styles and, thus, is likely to suddenly change the appearance of some elements if the error is shown within your HTML page (e.g. as a result of an ajax-request)
html{color:#000;background:#FFF;} body,div,dl,dt,dd,ul,ol,li,h1,h2,h3,h4,h5,h6,pre,code,form,fieldset,legend,input,textarea,p,blockquote,th,td{margin:0;padding:0;}table{border-collapse:collapse;border-spacing:0;} fieldset,img{border:0;} address,caption,cite,code,dfn,em,strong,th,var{font-style:normal;font-weight:normal;} li{list-style:none;} caption,th{text-align:left;} h1,h2,h3,h4,h5,h6{font-size:100%;font-weight:normal;} q:before,q:after{content:'';} abbr,acronym{border:0;font-variant:normal;} sup{vertical-align:text-top;} sub{vertical-align:text-bottom;} input,textarea,select{font-family:inherit;font-size:inherit;font-weight:inherit;} input,textarea,select{*font-size:100%;}legend{color:#000;} html { background: #eee; padding: 10px } img { border: 0; } #sf-resetcontent { width:970px; margin:0 auto; }
Unfortunately, these styles are added in a protected method, so they cannot be removed by extending the handler. So just use the printException() function above if you want to include the stack trace somewere and do not let the component render an entire page.