]>
git.proxmox.com Git - pve-apiclient.git/blob - PVE/APIClient/Exception.pm
1 package PVE
::APIClient
::Exception
;
3 # a way to add more information to exceptions (see man perlfunc (die))
4 # use PVE::APIClient::Exception qw(raise);
5 # raise ("my error message", code => 400, errors => { param1 => "err1", ...} );
12 use Storable
qw(dclone);
13 use HTTP
::Status
qw(:constants);
15 use overload
'""' => sub {local $@; shift-
>stringify};
16 use overload
'cmp' => sub {
19 return "$a" cmp "$b"; # compare as string
22 our @EXPORT_OK = qw(raise);
25 my ($class, $msg, %param) = @_;
27 $class = ref($class) || $class;
33 foreach my $p (keys %param) {
34 next if defined($self->{$p});
36 $self->{$p} = ref($v) ? dclone
($v) : $v;
44 my $exc = PVE
::APIClient
::Exception-
>new(@_);
46 my ($pkg, $filename, $line) = caller;
48 $exc->{filename
} = $filename;
57 my $msg = $self->{code
} ?
"$self->{code} $self->{msg}" : $self->{msg
};
60 if ($self->{filename
} && $self->{line
}) {
61 $msg .= " at $self->{filename} line $self->{line}";
66 if ($self->{errors
}) {
67 foreach my $e (keys %{$self->{errors
}}) {
68 $msg .= "$e: $self->{errors}->{$e}\n";
72 if ($self->{propagate
}) {
73 foreach my $pi (@{$self->{propagate
}}) {
74 $msg .= "\t...propagated at $pi->[0] line $pi->[1]\n";
79 $msg .= $self->{usage
};
80 $msg .= "\n" if $msg !~ m/\n$/;
87 my ($self, $file, $line) = @_;
89 push @{$self->{propagate
}}, [$file, $line];