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.

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.

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>  
  1. Then visit localhost:3000/error/ to view your error page.
  2. Create a file, such as ROOT_DIR/error-page.html and copy + paste the html source code into that files.
  3. Add a <style> tag to the <head> of that html, then copy+paste the CSS from the linked stylesheet into that <style> tag.
  4. When an unhandled error is encountered, simply echo file_get_contents(ROOT_DIR.'/error-page.html);exit;

Note: bin/lia error-page was created to generate the error page, but this needs to be updated and may not currently work.

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