Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
oliveratgithub opened this issue Oct 7, 2017 · 3 comments

Comments

@oliveratgithub
Copy link

oliveratgithub commented Oct 7, 2017

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
Copy link

uBizzy commented Oct 7, 2017

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
Copy link
Author

oliveratgithub commented Oct 7, 2017

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
Copy link

uBizzy commented Oct 7, 2017

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants