flourishlib / flourish-classes

The class files for Flourish
http://flourishlib.com
182 stars 78 forks source link

Can you advice me how to catch all errors and print them nicely using the flourish fCore? #229

Open oliveratgithub opened 7 years ago

oliveratgithub commented 7 years ago

Hi there, I'm struggling a bit with the fCore & fException try-catch mechanism.

What I want to accomplish is, that all errors (and exceptions) get catched while parsing the php-script and collected, so I can run a foreach ($errors as $error)-loop in the end and nicely print any errors that may have occurred to the HTML output.

Unfortunately I cannot get this to work - and I am confused by the documentation of these error catching features... Here's how I thought it should work – can someone advice what I'm doing wrong? :)

<?php
function __autoload($class_name) {
    $flourish_root = FLOURISH_DIR;
    $file = $flourish_root . $class_name . '.php';
    if (file_exists($file)) {
        include $file;
        return;
    } 
    throw new Exception('The class ' . $class_name . ' could not be loaded');
}
fCore::enableErrorHandling('html');
fCore::enableExceptionHandling('html'); // I do not understand the callback parameters that can be used here
?><html>
<head>
<title>Flourish Exception handling test</title>
</head><?php
fCore::startErrorCapture();
$db  = new fDatabase('mysql', 'testdb', 'root', 'root'); // 'testdb' does NOT exists => connection error
$errors = fCore::stopErrorCapture();
?><body>
<h1>Hello world</h1>
<p>it all looks good here</p>
<h2>Here come some PHP errors and exceptions:</h2><?php
// let's throw another exception just for demonstration purpose
throw new fValidationException('This is a forced ValidationException');
throw new fProgrammerException('This is a forced fProgrammerException');

foreach ($errors as $error)
{
    printf('<div class="alert alert-warning">%s</div>', $error['string']);
}
?></body>
</html>

As you may see, I would like to have the errors & exceptions "collected" and then only printed nicely in the foreach-loop before closing /body.

Is something like this possible and what would I need to change in order to accomplish this with Flourish?

Thanks for any help & hints! Oliver

uBizzy commented 7 years ago

If you wrap your code with try & catch you can get all errors thrown on it. You can either be specific and catch a "fSQLException" or catch all expeptions thrown on this block with "fException".

Hope it helps! I'm glad to see that flourish is still in use!

EDITED: All non catch exceptions will be outputted if you choose 'html' or emailed if you choose an email address. To print exceptions nicely on your HTML code you need to catch each expected exception.

oliveratgithub commented 7 years ago

Thanks for your feedback @uBizzy

So when I use try-catch, I can get it to work.

<?php
try{
    $db  = new fDatabase('mysql', 'testdb', 'root', 'root'); // 'testdb' does NOT exists => connection error
    $db->connect();
}
catch(Exception $e) {
    array_push($errors, $e->getMessage());
}
?>

However, I assumed, that using

fCore::startErrorCapture();
some code
$errors = fCore::stopErrorCapture();

Is an equivalent to try-catch – but it isn't then? Could you elaborate, why I should use this after all?

Because using try-catch and outputting the catched errors/exception to HTML also works with plain PHP, no need using any of these Flourish classes:

fCore::enableErrorHandling('html');
fCore::enableExceptionHandling('html');
fCore::startErrorCapture();
uBizzy commented 7 years ago

Well, I suppose that's because you are outputting all errors and exceptions to HTML, so fCore will output them and only after that the "startErrorCapture()" and "stopErrorCapture()" are available for you to output.

Try to change the error and exception to file or email and test it. Then tell me if it worked.