使用 mod_cgi 和 mod_cgi 捕获错误 mod_perl

发布于 2024-07-29 08:40:21 字数 1250 浏览 5 评论 0原文

提前感谢大家。

我一直在对错误处理进行一些研究,但我觉得我并没有对我应该做什么有充分的了解。

前言:我的代码位于 Apache 中并在浏览器中执行,我的目标不包括命令行执行。

我希望具有 CGI::Carp (fatalsToBrowser) 的行为,能够捕获输出并能够将其放入我自己的模板页面中,通过电子邮件发送等...我确实注意到 fatalsToBrowser 不起作用与 mod_perl。 有谁知道为什么? Apache/mod_perl 是如何阻碍的?


第一个目标:我想将一些东西放在一起,以便在使用 mod_perl 或 mod_cgi 执行代码时可以正常工作。

第二个目标:我希望有一个高级方法来捕获类似于.NET 的 Application_Error (在 global.asax 中)和 PHP 的 set_exception_handler() 和 set_error_handler() 方法的所有错误。 这些允许您在引发错误时进行控制,而无需将代码包装在混乱的 /gross try-catch 语句中。


我读过/评论过的东西:

1.) OO 异常处理在 Perl 中,但不是我正在寻找的。 我想捕捉的大多数东西都是 die()ing。 下一个链接还说这篇文章已过时且已弃用。

2.) Perl:$SIG{__DIE__}、eval { } 和堆栈跟踪,但我并没有从中得到太多与我的目标相关的东西。

3.) Perl 实用模式 (O'Reilly),第 21 章“错误处理和调试”。 值得庆幸的是,我所有的 Perl 代码都使用严格的并且启用了警告,并且第 6 章“用 mod_perl 进行编码”中提到的最重要的事情已经完成。

4.) 我翻遍了《Learning Perl》、《Perl Cookbook》、《Programming Perl》和《Higher Order Perl》的目录,但没有看到任何让我印象深刻的内容。 如果您认为我错过了某些内容,请告诉我。 :)


我不记得在哪里(也许在“实用 mod_perl”中,但我读到你不应该弄乱 $SIG{__DIE__}。

Thanks to everyone in advance.

I've been doing some research on error handling and I don't feel like I'm getting a solid understanding of what I should do.

Preamble: My code is living in Apache and executed in the browser, my goals don't include command line execution.

I'd like to have the behavior of CGI::Carp (fatalsToBrowser) with the ability to capture the output and be able to throw it in my own templated page, email it etc... I did notice that fatalsToBrowser doesn't work with mod_perl. Does anyone know why? How is Apache/mod_perl getting in the way?


First Goal: I'd like to put something together that works if the code is being executed with mod_perl or mod_cgi.

Second Goal: I'd like to have a high-level method(s) that catches all the errors similar to .NET's Application_Error (in global.asax) and PHP's set_exception_handler() and set_error_handler() methods. These allow you to take control when an error is raised, without wrapping code in messy/gross try-catch statements.


Things I've read/reviewed:

1.) OO Exception Handling in Perl, but wasn't what I was looking for. Most of the stuff I want to catch is die()ing. The next link also says that this article is out of date and deprecated.

2.) Perl: $SIG{__DIE__}, eval { } and stack trace, but I didn't get much from this related to my goals.

3.) Practical Mode Perl (O'Reilly), Chapter 21 "Error Handling and Debugging". Thankfully all my perl code uses strict and warnings are enabled, and most important things mentioned in Chapter 6 "Coding with mod_perl in Mind" are already done.

4.) I've dug through the tables of contents in "Learning Perl", "Perl Cookbook", "Programming Perl" and "Higher Order Perl" and didn't see anything that stuck out at me. If you think I missed something there please let me know. :)


I don't remember where (maybe in "Practical mod_perl", but I've read that you should not mess with $SIG{__DIE__}.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

寄居人 2024-08-05 08:40:21

您是否阅读了 mod_perl 网站关于 替代方案的内容异常处理技术? 它讨论了如何通过使用覆盖全局 die() 函数而不是使用 $SIG{__DIE__} 来捕获未捕获的异常。 一种更干净的方法,但并不完美。

Have you read the mod_perl website's bit on Alternative Exception Handling Techniques? It discusses about how you can catch uncaught exceptions though the use of overriding the global die() function instead of using $SIG{__DIE__}. A much cleaner method but not perfect.

浅黛梨妆こ 2024-08-05 08:40:21

您想捕获什么类型的错误? 自定义错误页面不足以满足您的目的吗?

我的 CGI 脚本很短(好吧,这确实很简单 - 并且未经测试):

#!/usr/bin/perl

use strict;
use warnings;

use My::App;
use My::ErrorReporter qw( error_to_html );

run();

sub run {
    my $app = eval {
        My::App->new(
            'some_param',
            'another_param',
        )
    };

    unless ( $app ) {
        print error_to_html( $@ );
        return;
    }

    eval {
        $app->handle_request;        
    } and return;

    print error_to_html( $@ );
    return;
}

__END__

现在,fatalsToBrowser 不适合您的用户。 这对你来说是一个发展援助。 用户看到的错误消息不应传达有关程序的信息。 因此,例如,在打开并读取配置文件的例程中,您应该执行以下操作:

sub read_my_config {
    my $self = shift;

    open my $config_h, '<', $self->config_file;

    unless ( $config_h ) {
        # This goes to the Apache error log where you can read it
        warn sprintf(
            "Cannot open '%s': %s", 
            $self->config_file, $!
        );
        # This is for web site visitors to see
        die "Cannot open configuration file";
    }

    # rest of the code
}

What type of errors are you trying to catch? Are custom error pages not sufficient for your purposes?

My CGI scripts are short (OK, this is really bare bones — and untested):

#!/usr/bin/perl

use strict;
use warnings;

use My::App;
use My::ErrorReporter qw( error_to_html );

run();

sub run {
    my $app = eval {
        My::App->new(
            'some_param',
            'another_param',
        )
    };

    unless ( $app ) {
        print error_to_html( $@ );
        return;
    }

    eval {
        $app->handle_request;        
    } and return;

    print error_to_html( $@ );
    return;
}

__END__

Now, fatalsToBrowser is not for your users. That is a development aid for you. The error messages users see should not convey information about the program. So, for example, in a routine that opens and reads a configuration file, you should do something like:

sub read_my_config {
    my $self = shift;

    open my $config_h, '<', $self->config_file;

    unless ( $config_h ) {
        # This goes to the Apache error log where you can read it
        warn sprintf(
            "Cannot open '%s': %s", 
            $self->config_file, $!
        );
        # This is for web site visitors to see
        die "Cannot open configuration file";
    }

    # rest of the code
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文