SitePoint Sponsor

User Tag List

Page 1 of 2 12 LastLast
Results 1 to 25 of 37
  1. #1
    If it aint Dutch it aint much Kilroy's Avatar
    Join Date
    Oct 2003
    Location
    The Netherlands
    Posts
    406
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Exception error handling and templates

    Hi everyone!

    I'm working on a CMS (Content Management System) at the moment. It is a CMS meant for content-related, or informative websites. Which means it is not meant to be a portal and it is not meant to have advanced options for large corporate websites. The goal is to make it easy for e.g. a doctor to have his/her personal website detailing his/her skills. It could also be for something like a football club, a jokes website, a clan website, whatever. There are no differences in setup for it, the idea is just to have a no-frills CMS that is primarily there for managing the content. Later on I might expand it.

    I am writing it in PHP5, so I can use exceptions for error handling. Now, I have some questions.

    I am using a template system I wrote myself, which loads up one large file that contains HTML and PHP. The PHP calls functions and methods, which output information.

    I am using exceptions to handle fatal errors, like missing files, templates, database connections that do not work and stuff like that. These work fine with exceptions, as the exceptions shut down the program using a die() statement.

    1. How should I handle non-critical errors? For example, a non-numeric ID, which should be numeric. I should inform the user of this and stop the execution of a particular piece of code, but some other code should be executed anyway. What's the best way to handle this in PHP5?
    2. How can I make sure that the exception error messages can be translated? The method I have now is load up a language file, but what if there is an error before it is loaded? E.g. the error that the language file isn't there? How could that be translated?
    3. The problem I have with my template system as it is now, is that I cannot stop page execution at a certain point, because it will then present the user with a half-finished page. Is there any better way to do a template system? Like load everything up in a huge variable and echo that if everything went well?


    I would love to hear your ideas on how to arrange things like these. I would like to hear best practices.

    Please consider that I have a very limited time frame, so I do not have time for advanced options. Those might be present at a later point, but not now.

    Thank you for your time,

    - Peter

  2. #2
    SitePoint Wizard DougBTX's Avatar
    Join Date
    Nov 2001
    Location
    Bath, UK
    Posts
    2,498
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Kilroy
    1. How should I handle non-critical errors?
    How are you using your exceptions at the moment?

    I'm asking because imo, this is what exceptions were built for - to give a better set of options to non-critical errors.

    Example:

    PHP Code:
    // inside the language file loader:
    function loadfile($filename) {
      if (!
    file_exists($filename)) {
        throw new 
    File404Exception;
      }
      
    // code to do things with the file...
    }

    // and then inside the calling class:
    class MyClass {
      
      const 
    noLangFile 'Sorry, could not load any language files!';
      
      function 
    aFunction() {
        try {
          
    $myLangFileLoader->loadfile('en.txt');
        }
        catch (
    File404Exception $e) {
          
    $templatEngine->post_fatal_error(MyClass::noLangFile);
        }
      }

    There is no "die" because after the catch you are back into normalcode, it is as if nothing out of the ordinary had happened.

    For example, a non-numeric ID, which should be numeric.
    For this particular error, you don't need exceptions. A simple if(isnumeric($id)) would do the trick. If, on the other hand, it looked like a valid ID, but the database couldn't find the page, you could use an exception there:

    PHP Code:
      function aFunction() {
        try {
          
    $db->load_by_id($id);
        }
        catch (
    File404Exception $e) {
          
    $templatEngine->post_fatal_error($lang['AritcleNotFound']); // or however you do languages...
        
    }
      } 
    Douglas
    Hello World

  3. #3
    If it aint Dutch it aint much Kilroy's Avatar
    Join Date
    Oct 2003
    Location
    The Netherlands
    Posts
    406
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    At the moment, I am using exceptions like this:

    PHP Code:
    if(!file_exists($prefix 'includes/config.php')){
        throw new 
    Exception('Wave.CMS error: The configuration file is not available. Please make sure it is uploaded to the correct directory.');

    and then I have this:

    PHP Code:
    try {
        
    $wave = new System();
    } catch (
    Exception $ex) {
        die(
    $ex->getMessage());

    However I find that this is not extremely useful when something has already been outputted. It is very ugly to have errors in the middle of a page, when the page has not been executed fully yet, and then have the page execution stop.

    What you are saying is using something like $templateEngine->post_fatal_error($error_message), but the thing is that it will then appear in mid-page. So what I need is a way to manage templates and errors, so that this happens:

    1) When there is a non-critical error, show it in the middle of the page, where the content would appear otherwise and stop execution of things that rely on non-critical error.
    2) When there is a critical error, then it should not display all page things, just something like this: Error: could not load language file. That should be all that is on the page. How would I do this? Redirect to another page or maybe store the entire template output in a variable so I just don't echo the variable?

    Thanks for your help,

    Peter

  4. #4
    SitePoint Wizard DougBTX's Avatar
    Join Date
    Nov 2001
    Location
    Bath, UK
    Posts
    2,498
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Kilroy
    1) When there is a non-critical error, show it in the middle of the page, where the content would appear otherwise and stop execution of things that rely on non-critical error.
    2) When there is a critical error, then it should not display all page things, just something like this: Error: could not load language file. That should be all that is on the page. How would I do this? Redirect to another page or maybe store the entire template output in a variable so I just don't echo the variable?
    Aah, well then it is just a case of writing a better (or using someone else's, there are loads out there) tempalte engine rather than an exceptions problem.

    All you need is sometihng like this:

    PHP Code:
    // Main code...
    $template->addData($array_of_vars);

    // Somewhere in the main code this happens:
    $template->set_critical_error($error);

    // And then after the main code:
    $template->echo_out_HTML();

    // and inside $template
    function echo_out_HTML() {
      if (!
    $this->critical_error) {
        
    $this->load_template('normal.tpl');
      } else {
        
    $this->load_template('critical_error.tpl');
      }

    Lots of ways to write that code above,but that's the logic you probably want to look at.

    Douglas
    Last edited by DougBTX; Dec 18, 2004 at 13:48.
    Hello World

  5. #5
    If it aint Dutch it aint much Kilroy's Avatar
    Join Date
    Oct 2003
    Location
    The Netherlands
    Posts
    406
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I guess you're right and I should write a better template engine. The above example looks very cool, and I will definitely use something like that.

    Thanks,

    Peter

  6. #6
    SitePoint Evangelist
    Join Date
    Jun 2003
    Location
    Melbourne, Australia
    Posts
    440
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Of course, what doesn't help is that the try/catch construct and Exception (or any other SPL) class don't appear in the main documentation. It certainly leaves the impression that these are afterthoughts, not a main feature (despite the trumpetting of them).
    Zealotry is contingent upon 100 posts and addiction 200?

  7. #7
    eschew sesquipedalians silver trophy sweatje's Avatar
    Join Date
    Jun 2003
    Location
    Iowa, USA
    Posts
    3,749
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by auricle
    what doesn't help is that the try/catch construct and Exception (or any other SPL) class don't appear in the main documentation.
    http://www.php.net/manual/en/languag...exceptions.php ?
    Jason Sweat ZCE - jsweat_php@yahoo.com
    Book: PHP Patterns
    Good Stuff: SimpleTest PHPUnit FireFox ADOdb YUI
    Detestable (adjective): software that isn't testable.

  8. #8
    SitePoint Wizard DougBTX's Avatar
    Join Date
    Nov 2001
    Location
    Bath, UK
    Posts
    2,498
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    and Exceptions and try/catch aren't part of SPL anyway...
    Hello World

  9. #9
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Regardless of templating, if you don't want a broken page you can avoid this easily using output buffering. If therefore an error/exception arises you just flush the buffer and redirect to a 404 page, otherwise you fetch the contents of the buffer prior to output, no?

  10. #10
    If it aint Dutch it aint much Kilroy's Avatar
    Join Date
    Oct 2003
    Location
    The Netherlands
    Posts
    406
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    True, but the point being is that this will be a CMS not for private use, but for public use. In that case, some people might not want to have output buffering enabled. I thought of it before, though and it's not such a bad idea. However, I now have a nice templating system using DougBTX's way and I think it's going to prove to be very useful.

    Auricle, there is documentation on exceptions and try/catch error handling, but I found it hard to understand. That was one of the reasons why I posted this thread.

    - Peter

  11. #11
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What part of using Exceptions is that you don't understand? I'm a bit thick sometimes myself though I had no bother understanding them...

  12. #12
    If it aint Dutch it aint much Kilroy's Avatar
    Join Date
    Oct 2003
    Location
    The Netherlands
    Posts
    406
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I wonder how I can make subclasses of the exception class, to have different types of errors formatted in a different way. I can't seem to find anything on that atm.

  13. #13
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I see, well that isn't so difficult, for example

    PHP Code:
    ...
    class 
    SqlException extends Exception {
    public function 
    __construct$message ) {
    parent::__construct$message ) {
    }
    public function 
    getMessage() {
    return 
    $this -> message;
    }

    This is an example off the top of my head but for more on this there is a definitive article on the new features of PHP5 over at zend dot com which demonstrates the usage of Exceptions to a finer detail - worthwhile checking out

    Note also you may extend other methods of the Exception as well, not just 'get message'.

  14. #14
    If it aint Dutch it aint much Kilroy's Avatar
    Join Date
    Oct 2003
    Location
    The Netherlands
    Posts
    406
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks, I will have a look at the article.

    - Peter

  15. #15
    SitePoint Evangelist
    Join Date
    Jun 2003
    Location
    Melbourne, Australia
    Posts
    440
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, that's new. I don't trawl on-line. Rather, I download the manual periodically. The last time, that page didn't exist. Is it unreasonable to expect that what is promoted as a fantastic new feature would have its documentation finished before the release?
    Quote Originally Posted by DougBTX
    and Exceptions and try/catch aren't part of SPL anyway...
    Sure. I freely admit I have a lot to learn about the new features of 5 (and I suppose I will when I start playing with it regularly). Some reasonable documentation would help. Try/catch and throw() still aren't there. (At least, where would one expect to find documentation of try/catch but in http://www.php.net/manual/en/languag...structures.php?)
    Zealotry is contingent upon 100 posts and addiction 200?

  16. #16
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    http://www.zend.com/php5/articles/php5-exceptions.php

    The interesting part is where the example of using Reflection is shown. To me anyways

  17. #17
    SitePoint Wizard DougBTX's Avatar
    Join Date
    Nov 2001
    Location
    Bath, UK
    Posts
    2,498
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by auricle
    Some reasonable documentation would help. Try/catch and throw() still aren't there. (At least, where would one expect to find documentation of try/catch but in http://www.php.net/manual/en/languag...structures.php?)
    Yes, it is little odd that they've put them with the PHP5 OOP stuff. Exceptions can be used quite easily with basic procedural code too, even though they are objects them selves. I'll send off an email to the documentation mailing list.

    Don't go so far as to say that there is no "reasonable documentation". All you need to know about try/catch/throw is on the exceptions page, as it only applies to exceptions.

    Douglas
    Hello World

  18. #18
    SitePoint Evangelist
    Join Date
    Jun 2003
    Location
    Melbourne, Australia
    Posts
    440
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by DougBTX
    Don't go so far as to say that there is no "reasonable documentation". All you need to know about try/catch/throw is on the exceptions page, as it only applies to exceptions.
    From the page in question:
    PHP 5 has an exception model similar to that of other programming languages. An exception can be thrown, try and caught within PHP. A Try block must include at least one catch block. Multiple catch blocks can be used to catch different classtypes; execution will continue after that last catch block defined in sequence. Exceptions can be thrown within catch blocks.
    Here we have a new conncept - throwing - which appears to be a bit like a return without having to actually assign the return value to some variable in the calling scope.

    For someone not familiar with this construct (like me), this is not enough. Maybe you think this is a newbie's conceit, but the various authors of the PHP manual have gone out of their way to provide detailed explanations of language constructs. That's part of the appeal. Why the opacity now?
    Zealotry is contingent upon 100 posts and addiction 200?

  19. #19
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Proberly just an over sight but, and forgive me for saying this - being a number of years since I took to learning PHP - but doesn't 'throw' in it's self, ie the word, represent what it's action is?

    I'm not sure if you have use of other programming languages, or if PHP is your first break?

    Thankfully you do not need, for the moment anyways, need to learn 'finally' as well That just complicates the whole concept even more

  20. #20
    SitePoint Evangelist ghurtado's Avatar
    Join Date
    Sep 2003
    Location
    Wixom, Michigan
    Posts
    591
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    No, I fully agree with auricle, and I do understand the workings of exceptions rather well, from having worked with them in Java, and I still think that exceptions deserve much better documentation from the folks at PHP. It is, after all, a fundamentaly new paradigm for many people. I dont think that "throw" by itself is enough to explain what the construct does any better than say "switch", "case", "foreach" and you still had to learn all of those slowly and patiently
    Garcia

  21. #21
    If it aint Dutch it aint much Kilroy's Avatar
    Join Date
    Oct 2003
    Location
    The Netherlands
    Posts
    406
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    And what is that "finally"

  22. #22
    SitePoint Evangelist ghurtado's Avatar
    Join Date
    Sep 2003
    Location
    Wixom, Michigan
    Posts
    591
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Correct me if im wrong, but I think that the "finally" clause is meant to be executed after and only if no exceptions are caught during the "try" clause (ie: no errors have developed)
    Garcia

  23. #23
    SitePoint Evangelist
    Join Date
    May 2004
    Location
    New Jersey, USA
    Posts
    567
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by ghurtado
    Correct me if im wrong, but I think that the "finally" clause is meant to be executed after and only if no exceptions are caught during the "try" clause (ie: no errors have developed)
    That would be different from the semantics in other languages, where "finally" is always executed, regardless of the result of the "try" block.

    See Sun's Java tutorial for an explanation in Java.

    =Austin

  24. #24
    SitePoint Evangelist
    Join Date
    Jun 2003
    Location
    Melbourne, Australia
    Posts
    440
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Dr Livingston
    Proberly just an over sight
    From http://www.zend.com/php5/articles/php5-exceptions.php:
    In order to handle an Exception at the client end, we must use a try-catch statement. This consists of a try clause and at least one catch clause. Any code that invokes a method that might throw an Exception should be wrapped in the try clause. The catch clause is used to handle the Exception should it be thrown.
    That one paragraph helps immensely. How hard would it have been to put that in the text of the PHP manual's exceptions page?
    Zealotry is contingent upon 100 posts and addiction 200?

  25. #25
    SitePoint Evangelist ghurtado's Avatar
    Join Date
    Sep 2003
    Location
    Wixom, Michigan
    Posts
    591
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    My bad, I totally misunderstood the meaning of "finally". I guess I dont really see the point of using it then, since the code in the "finally" clause gets executed regardless of the results of the "try" block and as such it doesnt seem that different to me than any regular code that were to exist after the try-catch. Does php5 support finally? I couldn't find any information on this.
    Garcia


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •