]>
git.proxmox.com Git - pve-common.git/blob - data/PVE/Exception.pm
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", ...} );
7 package PVE
::Exception
;
10 use vars
qw(@ISA @EXPORT_OK);
12 use Storable
qw(dclone);
13 use HTTP
::Status
qw(:constants);
17 use overload
'""' => sub {local $@; shift-
>stringify};
18 use overload
'cmp' => sub {
21 return "$a" cmp "$b"; # compare as string
24 @EXPORT_OK = qw(raise raise_param_exc raise_perm_exc);
27 my ($class, $msg, %param) = @_;
29 $class = ref($class) || $class;
35 foreach my $p (keys %param) {
36 next if defined($self->{$p});
38 $self->{$p} = ref($v) ? dclone
($v) : $v;
46 my $exc = PVE
::Exception-
>new(@_);
48 my ($pkg, $filename, $line) = caller;
50 $exc->{filename
} = $filename;
59 my $param = { code
=> HTTP_FORBIDDEN
};
61 my $msg = "Permission check failed";
63 $msg .= " ($what)" if $what;
65 my $exc = PVE
::Exception-
>new("$msg\n", %$param);
67 my ($pkg, $filename, $line) = caller;
69 $exc->{filename
} = $filename;
78 return $self->{code
} && $self->{code
} eq HTTP_BAD_REQUEST
;
82 my ($errors, $usage) = @_;
85 code
=> HTTP_BAD_REQUEST
,
89 $param->{usage
} = $usage if $usage;
91 my $exc = PVE
::Exception-
>new("Parameter verification failed.\n", %$param);
93 my ($pkg, $filename, $line) = caller;
95 $exc->{filename
} = $filename;
104 my $msg = $self->{code
} ?
"$self->{code} $self->{msg}" : $self->{msg
};
106 if ($msg !~ m/\n$/) {
108 if ($self->{filename
} && $self->{line
}) {
109 $msg .= " at $self->{filename} line $self->{line}";
115 if ($self->{errors
}) {
116 foreach my $e (keys %{$self->{errors
}}) {
117 $msg .= "$e: $self->{errors}->{$e}\n";
121 if ($self->{propagate
}) {
122 foreach my $pi (@{$self->{propagate
}}) {
123 $msg .= "\t...propagated at $pi->[0] line $pi->[1]\n";
127 if ($self->{usage
}) {
128 $msg .= $self->{usage
};
129 $msg .= "\n" if $msg !~ m/\n$/;
136 my ($self, $file, $line) = @_;
138 push @{$self->{propagate
}}, [$file, $line];