Best way to do custom error handling in F3

1,703 views
Skip to first unread message

Andrew Quan

unread,
Feb 7, 2013, 4:04:45 AM2/7/13
to f3-fra...@googlegroups.com
How do you handle custom error handling in F3?

It's not much but I have the following so far:


$f3->set('ONERROR',

function($f3) {

switch ($f3->get('ERROR.code')) {
case 404: echo 'Custom 404!';
break;
default: 
break;
}
}
);

jhice

unread,
Feb 7, 2013, 7:16:43 AM2/7/13
to f3-fra...@googlegroups.com
Hi Andrew,

You could use :
echo Template::instance()->render('404.htm');
instead of your echo 'Custom 404!';

Or just echo what you want in a custom template for all the errors (in your site layout).

What do you want to do exactly ?

There is an example in the archive tree @github, you can look in the file cms.php (in f3-3.0.1.cms.demo.zip => click on "view raw" to download it)

Andrew Quan

unread,
Feb 8, 2013, 3:01:41 AM2/8/13
to f3-fra...@googlegroups.com

Thanks jhice,


I'm not looking for anything specific, just interested in how everyone handles them differently (if they do)...

jhice

unread,
Feb 8, 2013, 4:06:37 AM2/8/13
to f3-fra...@googlegroups.com
Ok.

Personnaly I'm not at the point to handle errors, but what I'd probably do in production mode is to log the error (F3 log or Web server log) and show to the user some "readable" messages or generic messages like : 404 => "Page not found". Other errors => "An error occured. We've been informed and will look at it."

Jakofff Parker

unread,
Feb 12, 2013, 12:38:26 AM2/12/13
to f3-fra...@googlegroups.com

collaborating with the idea, so use it... errors are available with a GET for other uses and also automatically with ONERROR


$f3->route('GET /error/@code','errors');

function errors($f3,$params) {
        $log = new Log(LOG_FILE);
        $log->write(__FILE__."|".LOG_DEBUG."|Index|errors");
       
        $f3->set('code', $f3->get('PARAMS.code'));
       
        switch ($f3->get('PARAMS.code')) {
            case 404:
                echo View::instance()->render('modules/errors/error404.php');
            break;
       
            default:
                echo View::instance()->render('modules/errors/error.php');
            break;

        }
    }
   
   
   
    $f3->set('ONERROR',    function($f3) {
        $log = new Log(LOG_FILE);
        $log->write(__FILE__."|".LOG_DEBUG."|Index|Error-Request|");
        $log->write(__FILE__."|".LOG_DEBUG."|Index|Error-Request|".$f3->get('ERROR.code'));           
        $f3->reroute('/error/'.$f3->get('ERROR.code'));
    });

mariondorsett

unread,
Feb 19, 2013, 11:50:46 PM2/19/13
to f3-fra...@googlegroups.com
I setup an if condition to determine if the app is in production mode, and if so I trigger a controller and render a 404 page of my choosing:

if(APP_MODE == 'production')
{
  $app->set('ONERROR', function() use($app)
  {
    $page = new Controller();
    $page->beforeroute();
    $page->error_page();
    $page->afterroute();
    exit;
  });  
} // end if

... otherwise I just look at the error logs and see what's going on.
Reply all
Reply to author
Forward
0 new messages