XH 1.7: Introduce Exceptions

Discussions and requests related to new CMSimple features, plugins, templates etc. and how to develop.
Please don't ask for support at this forums!
Post Reply
cmb
Posts: 14225
Joined: Tue Jun 21, 2011 11:04 am
Location: Bingen, RLP, DE
Contact:

XH 1.7: Introduce Exceptions

Post by cmb » Thu Feb 26, 2015 8:16 pm

Hello Community,

I like to suggest that we introduce exceptions in the core of CMSimple_XH 1.7. I want to point out one particular use case, where it would have been really simplifying matters, if we had been able to use it for CMSimple_XH 1.6 (in the following I'm referring to XH 1.6.5, but the issue was quite similar in 1.6).

Consider XH_Controller::handleMenumanager(), which calls $pd_router->refresh_from_menu_manager(), which calls $this->model->refresh(), which calls $this->save(), which calls XH_saveContents(), which calls XH_writeFile(). Only the last function is able to tell whether saving succeeded, and therefore it has been necessary that all functions pass the boolean result along the call chain up to XH_Controller::handleMenumager, which actually displays an error message, if saving fails. That is clumsy and error prone, but even worse it is even necessary to know what's going on behind the scenes in XH_Controller::handleMenumanager. Otherwise, how should we know that $pd_router->refresh_from_menu_manager() only returns FALSE, if content.htm couldn't be saved? There may have been other potential errors, in which case we would have had to introduce some "advanced" error reporting (such as a string, or maybe an object).

Consider how simple that could have been solved with an Exception. No need to pass the result along the call chain. Just throw an exception, if the file could not be stored, and catch it in Consider XH_Controller::handleMenumanager().

Of course, the example is somewhat bogus, as the handling of the menumanager was not really used by XH 1.6, and it might be removed for XH 1.7, but certainly there are other use cases, though most likely not as complex as this one.

I do not have a concrete proposal, but a few points for consideration and discussion:
  1. It appears to be useful to define a basic exception class XH_Exception, which can be extended by plugins.
  2. It might be a good idea (or not) to localize the exception message as soon as an exception is thrown (doing so for the core; making it a convention for plugins).
  3. It might be reasonable to wrap most of the code in a try-catch statement, to be able to catch inadvertently uncaught exceptions, and to give a sensible error message if that happens. Otherwise an uncaught exception will trigger a fatal error, what still might be somewhat useful -- or even more useful, as at least Xdebug would print a stack backtrace.
I have to admit that I have close to zero experience with exceptions in PHP, so I hope that others join the discussion.
Christoph M. Becker – Plugins for CMSimple_XH

Holger
Site Admin
Posts: 3470
Joined: Mon May 19, 2008 7:10 pm
Location: Hessen, Germany

Re: XH 1.7: Introduce Exceptions

Post by Holger » Thu Feb 26, 2015 11:14 pm

cmb wrote:I have to admit that I have close to zero experience with exceptions in PHP, so I hope that others join the discussion.
Hmm, have used exceptions in a project while accessing a DB

Code: Select all

try {
        $db = new SQLite3($dbpath);
        $sql = "SELECT * FROM anbieter WHERE id = $anbieter_id";
        $stmt = $db->query($sql);
        $res = $stmt->fetchArray(SQLITE3_ASSOC);
        $stmt = NULL;
        $db->close();
    } catch (Exception $e) {
        //return 'Debug only!: Exception : ' . $e->getMessage();
        $error = 1002; //Failed to read from db
        return ddl_Message($error, $sn . '?' . $su);
    }
 
I have no real experience with exceptions too so what should I say: it works and helped me a lot to find my stupid errors...

cmb
Posts: 14225
Joined: Tue Jun 21, 2011 11:04 am
Location: Bingen, RLP, DE
Contact:

Re: XH 1.7: Introduce Exceptions

Post by cmb » Thu Feb 26, 2015 11:47 pm

Holger wrote:I have no real experience with exceptions too so what should I say: it works and helped me a lot to find my stupid errors...
Yes, at least generally, exceptions are really useful. It happens just too easily that you forget to check a return value for a failure condition -- an exception won't go unnoticed. When programming Delphi long time ago such exceptions really helped me a lot to avoid my stupid mistakes -- but I rarely caught them (I used mainly `finally` clauses for memory management, what's not so much an issue with PHP, and `finally` isn't available for us yet, anyway) and even less often thrown them myself. Nonetheless it worked great in a GUI environment with an implicit top-level exception handler -- even if I did not catch an exception programmatically, the user could usually proceed after acknowledging the error dialog.

I'm not sure, how that works out in CMSimple_XH, and I'm not sure with regard to throwing own (internationalized) exceptions. Probably further investigation is reasonable.
Christoph M. Becker – Plugins for CMSimple_XH

cmb
Posts: 14225
Joined: Tue Jun 21, 2011 11:04 am
Location: Bingen, RLP, DE
Contact:

Re: XH 1.7: Introduce Exceptions

Post by cmb » Tue Jun 30, 2015 12:34 pm

cmb wrote:It might be reasonable to wrap most of the code in a try-catch statement, to be able to catch inadvertently uncaught exceptions, and to give a sensible error message if that happens. Otherwise an uncaught exception will trigger a fatal error, what still might be somewhat useful -- or even more useful, as at least Xdebug would print a stack backtrace.
After some further consideration I like to suggest to stick with the general error handler, and to not use an additional "global" try-catch statement. It seems to me that it's best to handle execptions individually, and to treat any uncaught exception as a programming error. CMSimple_XH already handles that nicely; usually only the info about a fatal error is printed (with a note about debug mode), and in debug mode the necessary details are available.
cmb wrote:It appears to be useful to define a basic exception class XH_Exception, which can be extended by plugins.
That wouldn't help much, if we don't have a "global" try-catch statement in place.
cmb wrote:It might be a good idea (or not) to localize the exception message as soon as an exception is thrown (doing so for the core; making it a convention for plugins).
I'm completely at a loss here. Any ideas/suggestions welcome.
Christoph M. Becker – Plugins for CMSimple_XH

Post Reply