]>
git.proxmox.com Git - pve-apiclient.git/blob - PVE/APIClient/Exception.pm
1 package PVE
::APIClient
::Exception
;
3 # NOTE: derived from pve-common's PVE::Execption by copying and then:
4 # sed -i 's/PVE::/PVE::APIClient::/g' Exception.pm
6 # a way to add more information to exceptions (see man perlfunc (die))
7 # use PVE::APIClient::Exception qw(raise);
8 # raise ("my error message", code => 400, errors => { param1 => "err1", ...} );
12 use vars
qw(@ISA @EXPORT_OK);
14 use Storable
qw(dclone);
15 use HTTP
::Status
qw(:constants);
19 use overload
'""' => sub {local $@; shift-
>stringify};
20 use overload
'cmp' => sub {
23 return "$a" cmp "$b"; # compare as string
26 @EXPORT_OK = qw(raise raise_param_exc raise_perm_exc);
29 my ($class, $msg, %param) = @_;
31 $class = ref($class) || $class;
37 foreach my $p (keys %param) {
38 next if defined($self->{$p});
40 $self->{$p} = ref($v) ? dclone
($v) : $v;
48 my $exc = PVE
::APIClient
::Exception-
>new(@_);
50 my ($pkg, $filename, $line) = caller;
52 $exc->{filename
} = $filename;
61 my $param = { code
=> HTTP_FORBIDDEN
};
63 my $msg = "Permission check failed";
65 $msg .= " ($what)" if $what;
67 my $exc = PVE
::APIClient
::Exception-
>new("$msg\n", %$param);
69 my ($pkg, $filename, $line) = caller;
71 $exc->{filename
} = $filename;
80 return $self->{code
} && $self->{code
} eq HTTP_BAD_REQUEST
;
84 my ($errors, $usage) = @_;
87 code
=> HTTP_BAD_REQUEST
,
91 $param->{usage
} = $usage if $usage;
93 my $exc = PVE
::APIClient
::Exception-
>new("Parameter verification failed.\n", %$param);
95 my ($pkg, $filename, $line) = caller;
97 $exc->{filename
} = $filename;
106 my $msg = $self->{msg
};
107 if (my $code = $self->{code
}) {
108 if ($msg !~ /^\s*\Q$code\E[\s:,]/) { # avoid duplicating the error code heuristically
113 if ($msg !~ m/\n$/) {
115 if ($self->{filename
} && $self->{line
}) {
116 $msg .= " at $self->{filename} line $self->{line}";
122 if ($self->{errors
}) {
123 foreach my $e (keys %{$self->{errors
}}) {
124 $msg .= "$e: $self->{errors}->{$e}\n";
128 if ($self->{propagate
}) {
129 foreach my $pi (@{$self->{propagate
}}) {
130 $msg .= "\t...propagated at $pi->[0] line $pi->[1]\n";
134 if ($self->{usage
}) {
135 $msg .= $self->{usage
};
136 $msg .= "\n" if $msg !~ m/\n$/;
143 my ($self, $file, $line) = @_;
145 push @{$self->{propagate
}}, [$file, $line];