]>
git.proxmox.com Git - pve-common.git/blob - src/PVE/Exception.pm
1 package PVE
::Exception
;
3 # a way to add more information to exceptions (see man perlfunc (die))
4 # use PVE::Exception qw(raise);
5 # raise ("my error message", code => 400, errors => { param1 => "err1", ...} );
9 use vars
qw(@ISA @EXPORT_OK);
11 use Storable
qw(dclone);
12 use HTTP
::Status
qw(:constants);
16 use overload
'""' => sub {local $@; shift-
>stringify};
17 use overload
'cmp' => sub {
20 return "$a" cmp "$b"; # compare as string
23 @EXPORT_OK = qw(raise raise_param_exc raise_perm_exc);
26 my ($class, $msg, %param) = @_;
28 $class = ref($class) || $class;
34 foreach my $p (keys %param) {
35 next if defined($self->{$p});
37 $self->{$p} = ref($v) ? dclone
($v) : $v;
45 my $exc = PVE
::Exception-
>new(@_);
47 my ($pkg, $filename, $line) = caller;
49 $exc->{filename
} = $filename;
58 my $param = { code
=> HTTP_FORBIDDEN
};
60 my $msg = "Permission check failed";
62 $msg .= " ($what)" if $what;
64 my $exc = PVE
::Exception-
>new("$msg\n", %$param);
66 my ($pkg, $filename, $line) = caller;
68 $exc->{filename
} = $filename;
77 return $self->{code
} && $self->{code
} eq HTTP_BAD_REQUEST
;
81 my ($errors, $usage) = @_;
84 code
=> HTTP_BAD_REQUEST
,
88 $param->{usage
} = $usage if $usage;
90 my $exc = PVE
::Exception-
>new("Parameter verification failed.\n", %$param);
92 my ($pkg, $filename, $line) = caller;
94 $exc->{filename
} = $filename;
103 my $msg = $self->{code
} ?
"$self->{code} $self->{msg}" : $self->{msg
};
105 if ($msg !~ m/\n$/) {
107 if ($self->{filename
} && $self->{line
}) {
108 $msg .= " at $self->{filename} line $self->{line}";
114 if ($self->{errors
}) {
115 foreach my $e (keys %{$self->{errors
}}) {
116 $msg .= "$e: $self->{errors}->{$e}\n";
120 if ($self->{propagate
}) {
121 foreach my $pi (@{$self->{propagate
}}) {
122 $msg .= "\t...propagated at $pi->[0] line $pi->[1]\n";
126 if ($self->{usage
}) {
127 $msg .= $self->{usage
};
128 $msg .= "\n" if $msg !~ m/\n$/;
135 my ($self, $file, $line) = @_;
137 push @{$self->{propagate
}}, [$file, $line];