add make target to copy and include files from pve-common - update Exception.pm

This commit is contained in:
Dietmar Maurer 2018-06-14 09:59:17 +02:00
parent 23002ffc3f
commit d3e90048d0
2 changed files with 64 additions and 9 deletions

View File

@ -9,6 +9,9 @@ DESTDIR=
PERL5DIR=${DESTDIR}/usr/share/perl5
DOCDIR=${DESTDIR}/usr/share/doc/${PACKAGE}
PVE_COMMON_FILES= \
Exception.pm
all: ${DEB}
.PHONY: deb
@ -27,6 +30,10 @@ install:
install -m 0755 examples/example2.pl ${DOCDIR}/examples
install -m 0755 examples/perftest1.pl ${DOCDIR}/examples
update-pve-common:
for i in ${PVE_COMMON_FILES}; do cp ../pve-common/src/PVE/$$i PVE/APIClient/; done
for i in ${PVE_COMMON_FILES}; do sed -i 's/PVE::/PVE::APIClient::/g' PVE/APIClient/$$i; done
.PHONY: upload
upload: ${DEB}
tar cf - ${DEB} | ssh -X repoman@repo.proxmox.com upload --product pmg,pve --dist stretch

View File

@ -6,12 +6,13 @@ package PVE::APIClient::Exception;
use strict;
use warnings;
use base 'Exporter';
use vars qw(@ISA @EXPORT_OK);
require Exporter;
use Storable qw(dclone);
use HTTP::Status qw(:constants);
@ISA = qw(Exporter);
use overload '""' => sub {local $@; shift->stringify};
use overload 'cmp' => sub {
my ($a, $b) = @_;
@ -19,7 +20,7 @@ use overload 'cmp' => sub {
return "$a" cmp "$b"; # compare as string
};
our @EXPORT_OK = qw(raise);
@EXPORT_OK = qw(raise raise_param_exc raise_perm_exc);
sub new {
my ($class, $msg, %param) = @_;
@ -51,15 +52,62 @@ sub raise {
die $exc;
}
sub raise_perm_exc {
my ($what) = @_;
my $param = { code => HTTP_FORBIDDEN };
my $msg = "Permission check failed";
$msg .= " ($what)" if $what;
my $exc = PVE::APIClient::Exception->new("$msg\n", %$param);
my ($pkg, $filename, $line) = caller;
$exc->{filename} = $filename;
$exc->{line} = $line;
die $exc;
}
sub is_param_exc {
my ($self) = @_;
return $self->{code} && $self->{code} eq HTTP_BAD_REQUEST;
}
sub raise_param_exc {
my ($errors, $usage) = @_;
my $param = {
code => HTTP_BAD_REQUEST,
errors => $errors,
};
$param->{usage} = $usage if $usage;
my $exc = PVE::APIClient::Exception->new("Parameter verification failed.\n", %$param);
my ($pkg, $filename, $line) = caller;
$exc->{filename} = $filename;
$exc->{line} = $line;
die $exc;
}
sub stringify {
my $self = shift;
my $msg = $self->{code} ? "$self->{code} $self->{msg}" : $self->{msg};
if ($msg !~ m/\n$/) {
if ($self->{filename} && $self->{line}) {
$msg .= " at $self->{filename} line $self->{line}";
}
$msg .= "\n";
}