Errors & Debugging

Liaison does not have good built-in support for error handling, but does have a built-in \Lia::dump() method for debugging purposes.

Docs

  • Debug Liaison
  • Error Page
  • Show/Hide Development Errors
  • Logging Errors
  • Convert Errors to Exceptions
  • Exceptions
  • User errors
  • Error Message Management

Debug Liaison

In development, if you encounter an error, it can be helpful to dump the state of Liaison or other objects. $lia->dump() provides much more concise and thoughtful output than var_dump($lia), and it manages circular references.

vendor/bin/lia dump will dump the state of Liaison. (same as $lia->dump() below).

Call $lia->dump() to dump detailed information about the liaison instance, with a list of addons, packages, and their properties.

Call $lia->dump($var) for a simplified dump of an array or object.

Note: $lia->dump($var) does not provide enough information and will likely be improved in the future.

Error Page

When your site crashes, you'll want single .html file to print in order to show a generic error page. You'll make a public route to display generic errors, then use the built-in bin/lia cli tool to generate that .html file with styles and scripts printed inline. This way, the page looks like your full site and requires minimal setup.

To make one, create a public route for /error/ (such as file public/error.php), with content such as this:

<?php  
// optionally, set the HTML page theme for your error page. I just use my site's theme.  
// \Lia\Addon\Http:from($lia)->setTheme('vendor:namespace.view_name');  
?>  
<h1>Error</h1>  
<p>An unkown error has occured. Please visit the <a href="/">Home Page</a> or contact <a href="mailto:help@example.org">help@example.org</a>.</p>  

THEN:

  1. Configure Liaison's bin script (run vendor/bin/lia for instructions)
  2. Run vendor/bin/lia error-page, which will generate a .html file as configured
  3. echo file_get_contents(__DIR__.'/generic-error-page.html'); wherever you have an unrecoverable and otherwise unmanaged error, such as a catch{} block for your site's full delivery code.
    • NOTE: You might use $_SERVER['DOCUMENT_ROOT'] instead of __DIR__

Show/Hide Development Errors

In production, you should disable the display of errors on your site, but displaying them is very useful in development.

Show Errors:

<?php  
ini_set('display_errors', '1');  
ini_set('display_startup_errors', '1');  
error_reporting(E_ALL);  

Hide Errors:

<?php  
ini_set('display_errors', '0');  
ini_set('display_startup_errors', '0');  

Convert Errors to Exceptions

<?php  
set_error_handler(  
    function($errno, $errstr, $errfile, $errline) {  
        $e = new \ErrorException($errstr, $errno, 0, $errfile, $errline);  
        throw $e;  
    }  
);  

Logging Errors

There is a good chance your server has built-in error-logging with log-rotation, and you just need to navigate to the logs on your server to view them. In you terminal, use tail -n 50 log_file.txt to view the last 50 lines of the error log.

To log errors, use error_log().

For 3rd-party logging, see Awesome PHP by ziadoz. (Recommendations by ziadoz are not endorsed by me, and you use at your own risk)

Exceptions

This solution is a last-resort for unhandled exceptions.

Modify your deliver.php, wrapping the delivery code in a try/catch block, like so:

deliver(); } catch (\Exception $e){ // Log the error // assuming you've generated your error page echo file_get_contents(__DIR__.'/error-page.html'); exit; } ``` ## User errors If a user requests a page they don't have permission to view, or submits a bad request (*wrong form fields*), then you'll just need to print a message for the user, and set the appropriate header, and stop execution of the route. Typically, I do all this within the route itself (*i.e. in public/admin/whatever.php*). Example: ```php