如果 Moose 中未提供必需的属性,如何获得更好的错误消息?
我对驼鹿是全新的。到目前为止,我们的环境一直是 Perl 5.8.2,它不支持 Moose。
我正在研究一些示例,我认为属性上的“required => 1”设置会很方便,但是当我尝试使用该选项时,返回的错误消息实际上并不可用。
这里有一个例子:
cat.pl:
#!/usr/bin/perl
{
package Cat;
use Moose;
use Modern::Perl;
has 'name' => (
is => 'ro',
required => 1,
);
sub meow {
my $self = shift;
say 'Meow!';
}
}
use Modern::Perl;
my $alarm = Cat->new();
$alarm->meow();
$alarm->meow();
$alarm->meow();
当我运行它时:
Attribute (name) is required at /app/perl5/perl-5.10.1/lib/site_perl/5.10.1/aix-thread-multi-64all/Class/MOP/Class.pm line 581
Class::MOP::Class::_construct_instance('Moose::Meta::Class=HASH(0x110ac1a00)', 'HASH(0x110c3b3c0)') called at /app/perl5/perl-5.10.1/lib/site_perl/5.10.1/aix-thread-multi-64all/Class/MOP/Class.pm line 554
Class::MOP::Class::new_object('Moose::Meta::Class=HASH(0x110ac1a00)', 'HASH(0x110c3b3c0)') called at /app/perl5/perl-5.10.1/lib/site_perl/5.10.1/aix-thread-multi-64all/Moose/Meta/Class.pm line 258
Moose::Meta::Class::new_object('Moose::Meta::Class=HASH(0x110ac1a00)', 'HASH(0x110c3b3c0)') called at /app/perl5/perl-5.10.1/lib/site_perl/5.10.1/aix-thread-multi-64all/Moose/Object.pm line 28
Moose::Object::new('Cat') called at cat.pl line 20
如果我们的一个非 Perl 操作员看到这样的错误消息,他们可能会吓坏了。我担心他们不会意识到错误消息中的所有 5 行实际上都是同一错误的一部分。
如果未提供必需的属性,有没有办法获得不错的错误消息?
像呱呱的声音,我可以想象这样的消息:
Attribute (name) is required at cat.pl line 20
再说一遍,我是驼鹿的新手,所以这可能是我所缺少的一个简单的设置。
提前致谢!
I'm brand new to Moose. Up until today our environments have been on Perl 5.8.2 which would not support Moose.
I'm working through some examples, and I thought that the "required => 1" setting on an attribute would be handy, however when I try using that option, the error message that is returned is not really usable.
Here's an example:
cat.pl:
#!/usr/bin/perl
{
package Cat;
use Moose;
use Modern::Perl;
has 'name' => (
is => 'ro',
required => 1,
);
sub meow {
my $self = shift;
say 'Meow!';
}
}
use Modern::Perl;
my $alarm = Cat->new();
$alarm->meow();
$alarm->meow();
$alarm->meow();
When I run it:
Attribute (name) is required at /app/perl5/perl-5.10.1/lib/site_perl/5.10.1/aix-thread-multi-64all/Class/MOP/Class.pm line 581
Class::MOP::Class::_construct_instance('Moose::Meta::Class=HASH(0x110ac1a00)', 'HASH(0x110c3b3c0)') called at /app/perl5/perl-5.10.1/lib/site_perl/5.10.1/aix-thread-multi-64all/Class/MOP/Class.pm line 554
Class::MOP::Class::new_object('Moose::Meta::Class=HASH(0x110ac1a00)', 'HASH(0x110c3b3c0)') called at /app/perl5/perl-5.10.1/lib/site_perl/5.10.1/aix-thread-multi-64all/Moose/Meta/Class.pm line 258
Moose::Meta::Class::new_object('Moose::Meta::Class=HASH(0x110ac1a00)', 'HASH(0x110c3b3c0)') called at /app/perl5/perl-5.10.1/lib/site_perl/5.10.1/aix-thread-multi-64all/Moose/Object.pm line 28
Moose::Object::new('Cat') called at cat.pl line 20
If one of our non-perl operators see an error message like that, they will probably freak out. I'm afraid they will not realize that all 5 lines in the error message are actually a part of the same error.
Is there a way to get a nice error message if a required attribute is not supplied?
Something like croak, I can imagine a message like this:
Attribute (name) is required at cat.pl line 20
Again, I'm new to Moose so this may be an easy setting that I am missing.
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我想我可能已经找到了满足我要求的解决方案,但我不确定这是否是最好的解决方案。
而且,正如 @Tanktalus 指出的那样,拥有详细的错误消息是有价值的。
就我的问题而言, MooseX::Constructor::AllErrors扩展似乎有效:
在跑步时,我得到:
这就是我的想法。
由于我对 Moose 一点也不熟悉,所以这个扩展可以使用吗,或者它会“抑制”所有错误消息吗?
I think I may have found a solution to my requirement, but I'm not sure if it is the best solution.
And, as @Tanktalus points out, there is value to having a detailed error message.
For the purposes of my question, the MooseX::Constructor::AllErrors extension seems to work:
On running, I get:
Which is what I was thinking.
Since I am not familiar at all with Moose, is this extension okay to use, or will it "muffle" all of the error messages?
我更喜欢长错误消息 - 因为如果我缺少必需的参数,我希望我的非 Perl 用户会惊慌失措:我显然错过了开发、单元测试和系统测试中的一些内容,如果它会一直向用户发送此类消息。
请注意,当我投入生产时,我也会打开致命警告。我希望我的失败是引人注目的,这样我就不会无意中忽视它们。
I prefer the long error message - because if I'm missing a required parameter, I want my non-perl users to freak out: I obviously missed something in development, unit-test, and system test if it gets all the way to a user with this type of message.
Mind you, I also leave fatal warnings turned on when I go to production. I prefer my failures to be spectacular so that I can't accidentally ignore them.
由于“name”是必需的,因此您需要做的是从构造函数填充此属性。像这样:
这应该可以解决您的问题。
Since "name" is required, what you need to do is populate this attribute from the constructor. Like this:
This should fix your problem.