file error handling

Permalink Browser Info Environment
HI ...
using this addon (... and I LOVE it) i have trouble if I have some external file opening issues.

Let me explain:
I want to open a .csv file and display data... it's a simple routine in PHP (see below) and works great BUT ONLY if csv file exists

If the csv file does not exists i get a c5 error (see attached) and then I have to go back on page to delete the version because there is no way to even see any more the block in that page

Would it be possible to handle the error in some way (maybe with a meaningful message or just an error warning) but leaving the block accessible for maintenance ?

Thank you so much in advance
Michele

<?php
// Create a table from a csv file 
if (($handle = fopen("test.csv", "r")) !== false) {
    echo "<html><body><table>\n\n";
    $f = fopen("test.csv", "r");
    while (($line = fgetcsv($f)) !== false) {
            $row = $line[0];    
            $cells = explode(";",$row);
            echo "<tr>";
            foreach ($cells as $cell) {
                echo "<td>" . htmlspecialchars($cell) . "</td>";
            }
            echo "</tr>\n";
    }
    fclose($f);

1 Attachment

Type: Discussion
Status: In Progress
View Replies:
lobar replied on at Permalink Reply
lobar
Hi,

I see the issue here, and this could be generalized to any exception raised during the execution of the code.

There are two possibilities:
1. I could update the plugin so that when an exception occurs, I print an explicit error message in place of the content. If I find the time, I may release an update.
2. The code should be protected against potential errors during the execution. For instance, in your case, you should add a try-catch statement like:
<?php
// Create a table from a csv file 
if (($handle = fopen("test.csv", "r")) !== false) {
    echo "<html><body><table>\n\n";
    try {
            $f = fopen("test.csv", "r");
            while (($line = fgetcsv($f)) !== false) {
                    $row = $line[0];    
                    $cells = explode(";",$row);
                    echo "<tr>";
                    foreach ($cells as $cell) {
                        echo "<td>" . htmlspecialchars($cell) . "</td>";
                    }
            echo "</tr>\n";
    catch (Exception $e) {
skau61 replied on at Permalink Reply
great... i've added try statement and worked out
But it would be really great if you would add error handling in next release

Thank you so much
Michele
lobar replied on at Permalink Reply
lobar
Happy you were able to fix the code :)

Have a great evening,
lobar

concrete5 Environment Information

c8 2.1

Browser User-Agent String

Mozilla/5.0 (Windows NT 10.0; rv:55.0) Gecko/20100101 Firefox/55.0

Hide Post Content

This will replace the post content with the message: "Content has been removed by an Administrator"

Hide Content

Request Refund

You may not request a refund that is not currently owned by you.