現在の方法は次のとおりです。
sub new {
my ($package, $message) = (shift, shift);
my %params = @_;
return bless { message => $message, %params }, $package;
}
このメソッドは基本的にエラー文字列を返しますが、新しいエラー構造を取り、エラー ハッシュから文字列を返すこともできるように変更したいと考えています。bless の使い方がわかりません。%params は実行時パラメーターを受け入れますが、今のところ無視できます。
エラー構造は次のとおりです。
# this constant is an example of an error library (just included 1 error for example)
use constant {
CABLING_ERROR => {
errorCode => 561,
message => "Cabling incorrect to device in server A4r, contact DCT ",
tt => { template => 'cable'},
fatal => 1,
link =>'http://www.error-fix.com/cabling
},
};
start にいくつかのコードを投げ始めたところです。これは貧弱な試みですが、これが new() メソッドの変更を開始した方法です。
sub new {
my ($package, $message) = (shift, shift);
# defining new error hash
my $hash($errorCode, $message, $tt, $fatal, $link) = (shift, shift, shift, shift);
my %params = @_;
if(exists $hash->{errorCode}) {
return bless { errorCode => $errorCode, message => $message, tt => $tt, fatal => $fatal, link => $link}, %params;
}
else {
return bless { message => $message, %params }, $package;
}
}
bless についての私の理解は、それがハッシュ参照のオブジェクトになるということです。エラーは定数リストに保持されます。実行方法の例を次に示します。
if(!device_model) {
die ARC::Builder::Error->new(CABLING_ERROR);
}
更新:ソリューション @simbabque の単体テストを試みていますが、エラー メッセージ文字列ではなく、戻り値として空の文字列を取得し続けます。たぶん、正しく設定されていないのは私のテストですか?以下は、私が作成したテストの例です。
my $error = CABLING_ERROR;
my $exp_out_A = ($error->{message});
my $error_in = new($error);
diag($error_in);
is($error_in, $exp_out_A, 'Correct message output');