NanoMVC

Custom Error Handler

NanoMVC allows you to define your own custom error handler class to handle system-level errors such as exceptions and warnings. This gives you the flexibility to log, display, or process errors in a way that fits your application.

Learning by Example

  1. Create a new plugin file
    Add a new file to:
    /myapp/plugins/​my_errorhandler.php
  2. Extend the built-in error handler
    In your new file, define your class by extending NanoMVC_ErrorHandler:
    class My_ErrorHandler extends NanoMVC_ErrorHandler {
      // put your logic here
    }
  3. Update application config
    Open:
    /myapp/configs/config_​application.php
    and specify your custom handler:
    // PHP class that handles system errors
    $config['error_handler_class'] = 'My_ErrorHandler';

Result: Your custom error handler My_ErrorHandler will be automatically loaded and used on each request. All error and exception handling will pass through your overridden methods.

You can override only what you need — such as logging logic — and leave the rest to the base class. This allows you to extend behavior without rewriting it from scratch.

HTTP Error Codes and Views

NanoMVC automatically sends an appropriate HTTP status header when an error occurs. If no specific code is provided, the framework defaults to sending a 500 Internal Server Error header.

To trigger specific error views and headers (such as a 404 page), you can throw an exception with the desired code:

throw new Exception('Page not found', 404);

If the code is 404, the view file /sysfiles/views/notfound_view.php will be used instead of the default /sysfiles/views/error_view.php. You can customize both views by placing your own versions in /myapp/views/ or /myfiles/views/. Your modified version will be used automatically in your project.

Supported HTTP codes:

400 => Bad Request
401 => Unauthorized
403 => Forbidden
404 => Not Found
405 => Method Not Allowed
408 => Request Timeout
410 => Gone
429 => Too Many Requests
500 => Internal Server Error
501 => Not Implemented
502 => Bad Gateway
503 => Service Unavailable
504 => Gateway Timeout

← to Integrating a Template Engine to Customize Core →

← Back to Documentation