api: FileRestore: decode and return proper error of file-restore listing

since commit
ba690c40 ("file-restore: remove 'json-error' parameter from list_files")

in proxmox-backup, the file-restore binary will return the error as json
when called with '--output-format json' (which we do in PVE::PBSClient)

here, we assume that 'file-restore' will fail in that case, and we try
to use the return value as an array ref which fails, and the user never
sees the real error message.

To fix that, check the ref type of the return value and act accordingly

Signed-off-by: Dominik Csapak <d.csapak@proxmox.com>
This commit is contained in:
Dominik Csapak
2022-11-10 11:36:32 +01:00
committed by Thomas Lamprecht
parent 676b2e9094
commit 14f99a1614

View File

@ -121,13 +121,24 @@ __PACKAGE__->register_method ({
my $client = PVE::PBSClient->new($scfg, $storeid);
my $ret = $client->file_restore_list($snap, $path, $base64);
# 'leaf' is a proper JSON boolean, map to perl-y bool
# TODO: make PBSClient decode all bools always as 1/0?
foreach my $item (@$ret) {
$item->{leaf} = $item->{leaf} ? 1 : 0;
if (ref($ret) eq "HASH") {
my $msg = $ret->{message};
if (my $code = $ret->{code}) {
die PVE::Exception->new("$msg\n", code => $code);
} else {
die "$msg\n";
}
} elsif (ref($ret) eq "ARRAY") {
# 'leaf' is a proper JSON boolean, map to perl-y bool
# TODO: make PBSClient decode all bools always as 1/0?
foreach my $item (@$ret) {
$item->{leaf} = $item->{leaf} ? 1 : 0;
}
return $ret;
}
return $ret;
die "invalid proxmox-file-restore output";
}});
__PACKAGE__->register_method ({