allyc has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks,

I am having a problem trapping errors when using Mime::Lite. I am using Mime::Lite in a CGI Script which uses CGI:Carp with fatalsToBrowser turned on.

At the moment, Mime::Lite sends e-mails fine, but if something goes wrong (the biggest example is when Mime::Lite or Net::SMTP can’t connect to the SMTP server. When this happens Mime::Lite croaks.

The problem with this is that when Mime::Lite croaks, fatalsToBrowser is called and the script dies.

Is there any way of turning off this behaviour just when sending messages using Mime::Lite or making Mime::Lite return an error to my script if it can’t connect to the Mail Server?

Any help would be appreciated.

Alistair

Replies are listed 'Best First'.
Re: Stopping Module Croaking
by Chady (Priest) on May 11, 2005 at 09:10 UTC

    try to eval

    eval { do_things(); do_something_that_might_die(); do_more_things(); }; if ($@) { print "something that might die has, indeed, died.\n"; print "message: ", $@, "\n"; }

    Or something along these lines.


    He who asks will be a fool for five minutes, but he who doesn't ask will remain a fool for life.
    Chady | http://chady.net/
    Are you a Linux user in Lebanon? join the Lebanese GNU/Linux User Group.
Re: Stopping Module Croaking
by b10m (Vicar) on May 11, 2005 at 09:24 UTC

    According to the POD you can use the "quiet" class method.

    Besides, I would really want you to reconsider using fatalsToBrowser. Do you really want your users to see what goes wrong? Just check the error log files of your webserver.

    --
    b10m

    All code is usually tested, but rarely trusted.

      People keep saying this, but overlook that CGI::Carp has a convenient mechanism to control what gets sent to the browser with fatalsToBrowser activated:

      #!/usr/bin/perl + use strict; + use CGI::Carp qw(fatalsToBrowser set_message); + + my $sub_message = sub { my ($message) = @_; if ($ENV{REMOTE_ADDR} eq '127.0.0.1') { print "Oops: $message"; } else { print "Unable to process, please try later" +; } }; + set_message($sub_message); + die "aiee!";
      Of course you can use whatever condition you like to control whether the text of the die message gets displayed or not - a debugging flag or similar might make more sense in some circumstances.

      fatalsToBrowser provides a simple mechanism for trapping exceptions in the code which might otherwise result in at best a confusing 500 status in the users browser, and the advantage of doing something like the above is that you can use the same code for debugging as you do in production thus reducing the possibility of introducing new bugs when removing diagnostic code.

      You should bear in mind that not everyone has the luxury of access to the error logs when deploying a web application to production, indeed some servers (such as IIS) make it very difficult to get that information.

      /J\

Re: Stopping Module Croaking
by tlm (Prior) on May 11, 2005 at 10:11 UTC

    I second b10m's comments on fatalsToBrowser. This feature should be used only during development; it is a security risk to leave it on in production code. merlyn has a column on this.

    the lowliest monk

Re: Stopping Module Croaking
by thcsoft (Monk) on May 11, 2005 at 12:00 UTC
    and if you said:
    { no Carp; # perform sensitive stuff here }
    maybe in combination with the hint about 'eval' you could get along...

    language is a virus from outer space.
      Thanks Guys,

      I tried the Quiet option in Mime::Lite but that still caused a Croak.

      Shall try the Eval and no Carp ideas as they sound like what I need... for now I have just used Mime::Lite to generate the E-mail message and Net::SMTP to send it which gives me more control over the return codes and errors.

      I agree that the FatalstoBrowser should only be used in test situations... and we only allow the error to be shown if the user has the application in debug mode.

      Thanks again for all your help!!

      Alistair