Elegant error reporting, possible?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • steve

    Elegant error reporting, possible?

    Hi,
    In my script (phpnuke), whenever there is access to database, there is
    this line of code:

    message_die(GEN ERAL_ERROR, ’some error msg’, ’’, __LINE__, __FILE__,
    $sql);

    Is there a more elegant way of reporting line number besides putting
    this line everywhere I access db. I want to just write a function,
    which also globally knows about the current line number(?) and in case
    of error reports it.

    Is there any way to do that?

    --
    Posted using the http://www.dbforumz.com interface, at author's request
    Articles individually checked for conformance to usenet standards
    Topic URL: http://www.dbforumz.com/PHP-Elegant-...ict206147.html
    Visit Topic URL to contact author (reg. req'd). Report abuse: http://www.dbforumz.com/eform.php?p=703259
  • R. Rajesh Jeba Anbiah

    #2
    [FAQ] How to handle the script or database errors elegantly? (Was Re: Elegant error reporting, possible?)

    Q: How to handle the script or database errors elegantly?
    A: You can route the errors to a handler/your custom function using
    set_error_handl er() function. However, you may not capture all errors.

    Refer:


    Generates a user-level error/warning/notice message


    Q: I want to capture fatal errors too. But, set_error_handl er() doesn't
    capture. Is it a bug?
    A: It's a defined behavior. You may use output buffering techniques to
    capture fatal errors. That is, you capture the whole output with a
    output buffering callback function and then parse the content to find
    out the fatal errors.

    Refer:




    +++++
    @todo Grammar fix. Better hack?

    Comment

    • Kenneth Downs

      #3
      Re: Elegant error reporting, possible?

      steve wrote:
      [color=blue]
      > Hi,
      > In my script (phpnuke), whenever there is access to database, there is
      > this line of code:
      >
      > message_die(GEN ERAL_ERROR, ’some error msg’, ’’, __LINE__, __FILE__,
      > $sql);
      >
      > Is there a more elegant way of reporting line number besides putting
      > this line everywhere I access db. I want to just write a function,
      > which also globally knows about the current line number(?) and in case
      > of error reports it.
      >
      > Is there any way to do that?
      >[/color]

      One typical way is to first off wrap all of your data access commands in a
      single function, this is mine, which hits Postgres:

      function SQLExec($sql_co mmand) {
      global $dbconn
      $errlevel = error_reporting (0);
      pg_send_query($ dbconn,$sql);
      $results=pg_get _result($dbconn );
      $t=pg_result_er ror($results);
      if ($t) {
      ErrorAdd($t);
      ErrorAdd("Comma nd was: $sql");
      }
      error_reporting ($errlevel);
      return $results;
      }

      Another advantage of this is that you are a step closer to platform
      independence on the db side.

      But for error handling you put this at the top of your dispatcher:

      $GLOBALS["errors"]=array();

      Now you toss this function at the bottom of the dispatcher. I don't do line
      numbers myself in PHP, but if you want them it would be something like
      this:

      function ErrorAdd($strin g,$line,$file) {
      $GLOBALS["errors"][] =
      "Error in $file at line $line: $string";
      }

      Now give yourself this function:

      function Errors() { return count($GLOBALS["errors"]>0; }

      which allows to control execution based on existence of prior errors. Some
      code maybe needs to run no matter what, and some should not run if there
      have been earlier errors.

      The icing on the cake is:

      function ErrorsHTML() {
      if (!Errors()) { return ""; }
      $HTML_errs="";
      foreach($GLOBAL S["error"] as $err) {
      $HTML_errs.=$er r."<br>\n";
      }
      return '<div class="errors"> '.$HTML_errs.'</div>';
      }

      so you can now put the following unconditional code at the top of each page,
      or in your dispatcher:

      <?php echo ErrorsHTML(); ?>

      ....then you move on to other things in life.

      Hope this helps.
      --
      Kenneth Downs
      Secure Data Software, Inc.
      (Ken)nneth@(Sec )ure(Dat)a(.com )

      Comment

      • Chung Leong

        #4
        Re: [FAQ] How to handle the script or database errors elegantly? (Was Re: Elegant error reporting, possible?)


        "R. Rajesh Jeba Anbiah" <ng4rrjanbiah@r ediffmail.com> wrote in message
        news:1110771493 .119321.58090@g 14g2000cwa.goog legroups.com...[color=blue]
        > Q: I want to capture fatal errors too. But, set_error_handl er() doesn't
        > capture. Is it a bug?
        > A: It's a defined behavior. You may use output buffering techniques to
        > capture fatal errors. That is, you capture the whole output with a
        > output buffering callback function and then parse the content to find
        > out the fatal errors.[/color]

        Are you sure that works? IIRC, a fatal error would cause the output buffer
        to get flushed.


        Comment

        • Chung Leong

          #5
          Re: Elegant error reporting, possible?

          "steve" <UseLinkToEmail @dbForumz.com> wrote in message
          news:4_703259_1 e8c2956b514a921 086c731313b922e 1@www.dbforumz. com...[color=blue]
          > Hi,
          > In my script (phpnuke), whenever there is access to database, there is
          > this line of code:
          >
          > message_die(GEN ERAL_ERROR, ’some error msg’, ’’, __LINE__, __FILE__,
          > $sql);
          >
          > Is there a more elegant way of reporting line number besides putting
          > this line everywhere I access db. I want to just write a function,
          > which also globally knows about the current line number(?) and in case
          > of error reports it.
          >
          > Is there any way to do that?[/color]

          Yes. Use debug_backtrace ().


          Comment

          • ng4rrjanbiah@rediffmail.com

            #6
            Re: [FAQ] How to handle the script or database errors elegantly? (Was Re: Elegant error reporting, possible?)

            Chung Leong wrote:[color=blue][color=green]
            > > Q: I want to capture fatal errors too. But, set_error_handl er()[/color][/color]
            doesn't[color=blue][color=green]
            > > capture. Is it a bug?
            > > A: It's a defined behavior. You may use output buffering techniques[/color][/color]
            to[color=blue][color=green]
            > > capture fatal errors. That is, you capture the whole output with a
            > > output buffering callback function and then parse the content to[/color][/color]
            find[color=blue][color=green]
            > > out the fatal errors.[/color]
            >
            > Are you sure that works? IIRC, a fatal error would cause the output[/color]
            buffer[color=blue]
            > to get flushed.[/color]

            I'm sure, it works. 'coz I have written and using a proprietary
            class that does all these stuffs like displaying sources with
            highlighted error line, etc; though it won't capture parser error.

            --
            <?php echo 'Just another PHP saint'; ?>
            Email: rrjanbiah-at-Y!com Blog: http://rajeshanbiah.blogspot.com/

            Comment

            Working...