Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?

Mason error capturing

by inblosam (Monk)
on Feb 02, 2005 at 15:51 UTC ( #427309=perlquestion: print w/replies, xml ) Need Help??
inblosam has asked for the wisdom of the Perl Monks concerning the following question:

I am using HTML::Mason for my sites and I love it. I am now going live with a site and turned the error handling to "fatal" so it doesn't show my users the scary errors when they occur, and instead they see a custom Error 500 page. Now I am facing the issue of not being able to get to these errors so I can fix them. I would like to:

  • Capture the error and put it in an HTML comment on the error 500 page, so I can just "view source" and see it.
  • Capture the error in the error_log file, which it doesn't seem to be doing right now.
    How do I capture the error message? I tried dumping $r and saw a ton of stuff, but nothing that looked like the error messages I had been getting that were so informative before.


    Michael Jensen
  • Replies are listed 'Best First'.
    Re: Mason error capturing
    by rdfield (Priest) on Feb 02, 2005 at 16:18 UTC
      (Note: untested) Try setting $m->error_mode to fatal. The errors should end up in the server log.


        As I stated, I already set my error mode to fatal. I noticed the errors are now showing up in my error log, so that point is settled.

        Still need some help capturing the error on the actual Error page to hide in HTML comments...anyone???

        Michael Jensen
          The docs state that the error mode is either fatal or output with no in between. I guess you could sub-class the constructor and re-write the fatals sub to do both.

          The problem, I guess, is that the error is (usually) during the interpreter phase, and doesn't get anywhere near your code, so you can't do anything too fancy. You could, of course, write a PerlLogHandler module that lets you know when an error's occured via email or somesuch.


    Re: Mason error capturing
    by martell (Friar) on Feb 02, 2005 at 22:51 UTC


      I'm also using HTML::Mason for development. To solve this problem I left the error handling at output and I created a special module that writes a custom error to the user when an error occurs.

      Mason uses the standard Exception::Class module to handle errors. If an error thrown is a string, it will convert it to a 'HTML::Mason::Exception' otherwise it will rethrow it.(cfr here)

      This means that you can catch the errors and handle like you do in regular code.

      So in my mason files I put now:

      <%init> eval { #code that can throw errors }; if ($debug and ( UNIVERSAL::isa( $@,'HTML::Mason::Exception' )) { # + let all Mason errors pass like usual if in debug mode $@->rethrow; } elsif ($@) { # catch all errors #stringify the error and call an error component $m->comp('error.mhtml', 'text' => "$@"); } </%init> #HTML code

      The "$@" takes care of showing the error by stringifying it. The error module does some additional tasks like destroying the session, format the error, ... It could be expanded with error writing although.

      The $debug variable is a global one specified in the apache config file and takes the role as switch to activate the mason error handling. (Remark, in this case, all none mason errors will always be treated by the error component.)

      It was for me the simplest way of doing this. But I admit it is maybe not the standard mason way to do it. I had to add this code in all my mason components. This is not big problem because I have only a limited number of components that can expect errors, but it could be a bit tendious to do.

      So I advice you to post your question on the mason-users list on The people over there are most kindly to help you.



    Re: Mason error capturing
    by Jenda (Abbot) on Feb 06, 2005 at 00:14 UTC

      You would not. The first thing. Anyone could "view source" and see the error. Error messages often contain lots of information, sometimes sensitive, sometimes just revealing too much about the script and the server. So you never want to send the details to the users.

      I understand though that you'd like to be able to see them easily when it's you or your coleagues accessing the site. What I do (sorry, not using HTML::Mason) is I send the details only to people who have ... well ... something set in their browser that gets sent to the server and allows me to distinguish between the developers and admins and the ordinary users. Not 100% since I'm using plain HTTP, but IMHO good enough for me.

      We'd like to help you learn to help yourself
      Look around you, all you see are sympathetic eyes
      Stroll around the grounds until you feel at home
         -- P. Simon in Mrs. Robinson

    Re: Mason error capturing
    by kprasanna_79 (Hermit) on Feb 02, 2005 at 17:00 UTC
      (suggestion) Y cant u use eval {..}; some statements and croak it to STDERR so that it is very readable and easy to track
      we always go for croak.

    Log In?

    What's my password?
    Create A New User
    Node Status?
    node history
    Node Type: perlquestion [id://427309]
    Approved by Corion
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others browsing the Monastery: (5)
    As of 2018-06-23 11:07 GMT
    Find Nodes?
      Voting Booth?
      Should cpanminus be part of the standard Perl release?

      Results (125 votes). Check out past polls.