为应用程序设计错误消息系统的最 scalable/extensible 方法是什么?我将使用 Perl。
What would be the most scalable/extensible way to design an error messaging system for an application? I will be using Perl.
我只需要这个设计的通用架构。目前我正在考虑创建一个脚本,其中包含所有错误消息的哈希值。错误消息本身也是包含应用程序中的状态代码和错误消息的哈希值。然后在应用程序代码中发生错误的地方,我只有 die 函数,它创建一个新错误并使用包含错误列表的脚本中的错误名称。我不知道这是否是设计此类系统的最佳或最有效的方法。我正在尝试以这种方式设计它,以便如果必须添加更多错误。任何想法或指导将不胜感激。
当你说;
错误消息本身也是包含应用程序中的状态代码和错误消息的哈希值。然后在应用程序代码中发生错误的地方我只有 die 函数,它创建一个新错误并使用包含错误列表的脚本中的错误名称。
...您或多或少在描述异常对象。这个简单的异常 class (taken from here)
package Exception;
sub new
{
my $class = shift;
bless { msg=>"@_" }, $class;
}
sub what
{
my ($self) = @_;
return sprintf "Exception: %s\n", $self->{msg};
}
它的大小完成了很多,这样使用;
sub try(&) { eval {$_[0]->()} }
sub throw($) { die $_[0] }
sub catch(&) { $_[0]->($@) if $@ }
# The main program
try {
throw Exception->new("A real exception");
};
catch {
print $@->what;
};
这个对象只有一个属性,msg(称为->what),但是你显然可以有错误代码的属性、应用程序、应用程序阶段等。请注意 &throw
是 die 的一个小包装器,异常对象是在需要的地方创建的——很像你的语句 "...应用程序代码中发生错误我只有 die 函数会创建一个新错误..."
您的 "script with a hash of all the error messages" 将成为您应用程序的一个(子)模块,专用于
异常对象
1) 预先创建它们并通过一些错误代码对它们进行索引;或
2)(更好)创建一个通用的小层次结构
越来越具体的异常 classes,您同样可以在检测到错误时从中创建即时异常对象(与上述非常相似)
最后,根据您的具体情况,您可能希望使用 cpan 提供的预制的、功能齐全的解决方案。我自己没用过,但是 Exception::System 看起来不错。
Exception::Base非常棒。
This class implements a fully OO exception mechanism similar to Exception::Class
or Class::Throwable
. It provides a simple interface allowing programmers to declare exception classes. These classes can be thrown and caught. Each uncaught exception prints full stack trace if the default verbosity is increased for debugging purposes.
The features of Exception::Base
:
fast implementation of the exception class
fully OO without closures and source code filtering
does not mess with $SIG{__DIE__}
and $SIG{__WARN__}
no external run-time modules dependencies, requires core Perl modules only
the default behavior of exception class can be changed globally or just for the thrown exception
matching the exception by class, message or other attributes
matching with string, regex or closure function
creating automatically the derived exception classes ("use" in perlfunc interface)
easily expendable, see Exception::System
class for example
prints just an error message or dumps full stack trace
can propagate (rethrow) an exception
can ignore some packages for stack trace output
some defaults (i.e. verbosity) can be different for different exceptions
我只需要这个设计的通用架构。目前我正在考虑创建一个脚本,其中包含所有错误消息的哈希值。错误消息本身也是包含应用程序中的状态代码和错误消息的哈希值。然后在应用程序代码中发生错误的地方,我只有 die 函数,它创建一个新错误并使用包含错误列表的脚本中的错误名称。我不知道这是否是设计此类系统的最佳或最有效的方法。我正在尝试以这种方式设计它,以便如果必须添加更多错误。任何想法或指导将不胜感激。
当你说;
错误消息本身也是包含应用程序中的状态代码和错误消息的哈希值。然后在应用程序代码中发生错误的地方我只有 die 函数,它创建一个新错误并使用包含错误列表的脚本中的错误名称。
...您或多或少在描述异常对象。这个简单的异常 class (taken from here)
package Exception;
sub new
{
my $class = shift;
bless { msg=>"@_" }, $class;
}
sub what
{
my ($self) = @_;
return sprintf "Exception: %s\n", $self->{msg};
}
它的大小完成了很多,这样使用;
sub try(&) { eval {$_[0]->()} }
sub throw($) { die $_[0] }
sub catch(&) { $_[0]->($@) if $@ }
# The main program
try {
throw Exception->new("A real exception");
};
catch {
print $@->what;
};
这个对象只有一个属性,msg(称为->what),但是你显然可以有错误代码的属性、应用程序、应用程序阶段等。请注意 &throw
是 die 的一个小包装器,异常对象是在需要的地方创建的——很像你的语句 "...应用程序代码中发生错误我只有 die 函数会创建一个新错误..."
您的 "script with a hash of all the error messages" 将成为您应用程序的一个(子)模块,专用于
异常对象1) 预先创建它们并通过一些错误代码对它们进行索引;或
2)(更好)创建一个通用的小层次结构 越来越具体的异常 classes,您同样可以在检测到错误时从中创建即时异常对象(与上述非常相似)
最后,根据您的具体情况,您可能希望使用 cpan 提供的预制的、功能齐全的解决方案。我自己没用过,但是 Exception::System 看起来不错。
Exception::Base非常棒。
This class implements a fully OO exception mechanism similar to
Exception::Class
orClass::Throwable
. It provides a simple interface allowing programmers to declare exception classes. These classes can be thrown and caught. Each uncaught exception prints full stack trace if the default verbosity is increased for debugging purposes.The features of
Exception::Base
:
fast implementation of the exception class
fully OO without closures and source code filtering
does not mess with
$SIG{__DIE__}
and$SIG{__WARN__}
no external run-time modules dependencies, requires core Perl modules only
the default behavior of exception class can be changed globally or just for the thrown exception
matching the exception by class, message or other attributes
matching with string, regex or closure function
creating automatically the derived exception classes ("use" in perlfunc interface)
easily expendable, see
Exception::System
class for exampleprints just an error message or dumps full stack trace
can propagate (rethrow) an exception
can ignore some packages for stack trace output
some defaults (i.e. verbosity) can be different for different exceptions