我使用Net :: Stripe处理信用卡。如果信用卡号有效,它可以正常工作。但是当我测试一个不好的信用卡号码时,我会看到Stripe.pm的第676行:
676: die $e;
并产生此错误:
1 /usr/share/perl/5.14/perl5db.pl第7799行。
导致浏览器出现内部服务器错误。
Stripe.pm中以die语句结尾的代码块是:
if ($resp->code == 200) {
return _hash_to_object(decode_json($resp->content));
} elsif ($resp->code == 500) {
die Net::Stripe::Error->new(
type => "HTTP request error",
code => $resp->code,
message => $resp->status_line . " - " . $resp->content,
);
}
my $e = eval {
my $hash = decode_json($resp->content);
Net::Stripe::Error->new($hash->{error})
};
if ($@) {
Net::Stripe::Error->new(
type => "Could not decode HTTP response: $@",
message => $resp->status_line . " - " . $resp->content,
);
};
warn "$e\n" if $self->debug;
die $e;
Stripe返回的响应代码是402。
我正在尝试使用以下代码处理来自Stripe的错误消息:
my $card_token = $stripe->post_token(
card => {
number => $FORM{'Credit Card #'},
exp_month => $FORM{'Expiration Month'},
exp_year => $FORM{'Expiration Year'},
currency => 'usd',
cvc => $FORM{'Credit Card Security Code'},
name => $FORM{'Name on Card'},
address_line1 => $FORM{'Address'},
address_city => $FORM{'City'},
address_state => $FORM{'State'},
address_zip => $FORM{'Zip'}
}
);
if ($card_token->{failure_message}) {
$error = $card_token->{failure_message};
print "Content-type: text/html\n\n";
print "There was an error processing your credit card:<br><br>$error";
}
但是代码在退出试图检索令牌的代码块之前就已经死了。
有没有办法压制&#34;死&#34;声明?
答案 0 :(得分:0)
您可以使用eval来捕获此异常,然后继续工作。
my $card_token;
my $ok = eval {
$card_token = $stripe->post_token(
card => {
number => $FORM{'Credit Card #'},
exp_month => $FORM{'Expiration Month'},
exp_year => $FORM{'Expiration Year'},
currency => 'usd',
cvc => $FORM{'Credit Card Security Code'},
name => $FORM{'Name on Card'},
address_line1 => $FORM{'Address'},
address_city => $FORM{'City'},
address_state => $FORM{'State'},
address_zip => $FORM{'Zip'}
}
);
1;
}
if (! $ok) {
# error object is in $@. You'll need to investigate its structure
# before using it
my $error = $@;
...;
}
else {
# do stuff with $card_token
}