mirror of
git://git.proxmox.com/git/pve-apiclient.git
synced 2025-01-03 05:17:58 +03:00
add make target to copy and include files from pve-common - update Exception.pm
This commit is contained in:
parent
23002ffc3f
commit
d3e90048d0
7
Makefile
7
Makefile
@ -9,6 +9,9 @@ DESTDIR=
|
|||||||
PERL5DIR=${DESTDIR}/usr/share/perl5
|
PERL5DIR=${DESTDIR}/usr/share/perl5
|
||||||
DOCDIR=${DESTDIR}/usr/share/doc/${PACKAGE}
|
DOCDIR=${DESTDIR}/usr/share/doc/${PACKAGE}
|
||||||
|
|
||||||
|
PVE_COMMON_FILES= \
|
||||||
|
Exception.pm
|
||||||
|
|
||||||
all: ${DEB}
|
all: ${DEB}
|
||||||
|
|
||||||
.PHONY: deb
|
.PHONY: deb
|
||||||
@ -27,6 +30,10 @@ install:
|
|||||||
install -m 0755 examples/example2.pl ${DOCDIR}/examples
|
install -m 0755 examples/example2.pl ${DOCDIR}/examples
|
||||||
install -m 0755 examples/perftest1.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
|
.PHONY: upload
|
||||||
upload: ${DEB}
|
upload: ${DEB}
|
||||||
tar cf - ${DEB} | ssh -X repoman@repo.proxmox.com upload --product pmg,pve --dist stretch
|
tar cf - ${DEB} | ssh -X repoman@repo.proxmox.com upload --product pmg,pve --dist stretch
|
||||||
|
@ -6,20 +6,21 @@ package PVE::APIClient::Exception;
|
|||||||
|
|
||||||
use strict;
|
use strict;
|
||||||
use warnings;
|
use warnings;
|
||||||
|
use vars qw(@ISA @EXPORT_OK);
|
||||||
use base 'Exporter';
|
require Exporter;
|
||||||
|
use Storable qw(dclone);
|
||||||
use Storable qw(dclone);
|
|
||||||
use HTTP::Status qw(:constants);
|
use HTTP::Status qw(:constants);
|
||||||
|
|
||||||
|
@ISA = qw(Exporter);
|
||||||
|
|
||||||
use overload '""' => sub {local $@; shift->stringify};
|
use overload '""' => sub {local $@; shift->stringify};
|
||||||
use overload 'cmp' => sub {
|
use overload 'cmp' => sub {
|
||||||
my ($a, $b) = @_;
|
my ($a, $b) = @_;
|
||||||
local $@;
|
local $@;
|
||||||
return "$a" cmp "$b"; # compare as string
|
return "$a" cmp "$b"; # compare as string
|
||||||
};
|
};
|
||||||
|
|
||||||
our @EXPORT_OK = qw(raise);
|
@EXPORT_OK = qw(raise raise_param_exc raise_perm_exc);
|
||||||
|
|
||||||
sub new {
|
sub new {
|
||||||
my ($class, $msg, %param) = @_;
|
my ($class, $msg, %param) = @_;
|
||||||
@ -31,7 +32,7 @@ sub new {
|
|||||||
};
|
};
|
||||||
|
|
||||||
foreach my $p (keys %param) {
|
foreach my $p (keys %param) {
|
||||||
next if defined($self->{$p});
|
next if defined($self->{$p});
|
||||||
my $v = $param{$p};
|
my $v = $param{$p};
|
||||||
$self->{$p} = ref($v) ? dclone($v) : $v;
|
$self->{$p} = ref($v) ? dclone($v) : $v;
|
||||||
}
|
}
|
||||||
@ -42,7 +43,52 @@ sub new {
|
|||||||
sub raise {
|
sub raise {
|
||||||
|
|
||||||
my $exc = PVE::APIClient::Exception->new(@_);
|
my $exc = PVE::APIClient::Exception->new(@_);
|
||||||
|
|
||||||
|
my ($pkg, $filename, $line) = caller;
|
||||||
|
|
||||||
|
$exc->{filename} = $filename;
|
||||||
|
$exc->{line} = $line;
|
||||||
|
|
||||||
|
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;
|
my ($pkg, $filename, $line) = caller;
|
||||||
|
|
||||||
$exc->{filename} = $filename;
|
$exc->{filename} = $filename;
|
||||||
@ -53,13 +99,15 @@ sub raise {
|
|||||||
|
|
||||||
sub stringify {
|
sub stringify {
|
||||||
my $self = shift;
|
my $self = shift;
|
||||||
|
|
||||||
my $msg = $self->{code} ? "$self->{code} $self->{msg}" : $self->{msg};
|
my $msg = $self->{code} ? "$self->{code} $self->{msg}" : $self->{msg};
|
||||||
|
|
||||||
if ($msg !~ m/\n$/) {
|
if ($msg !~ m/\n$/) {
|
||||||
|
|
||||||
if ($self->{filename} && $self->{line}) {
|
if ($self->{filename} && $self->{line}) {
|
||||||
$msg .= " at $self->{filename} line $self->{line}";
|
$msg .= " at $self->{filename} line $self->{line}";
|
||||||
}
|
}
|
||||||
|
|
||||||
$msg .= "\n";
|
$msg .= "\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -86,7 +134,7 @@ sub stringify {
|
|||||||
sub PROPAGATE {
|
sub PROPAGATE {
|
||||||
my ($self, $file, $line) = @_;
|
my ($self, $file, $line) = @_;
|
||||||
|
|
||||||
push @{$self->{propagate}}, [$file, $line];
|
push @{$self->{propagate}}, [$file, $line];
|
||||||
|
|
||||||
return $self;
|
return $self;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user