[pve-devel] [PATCH pve-apiclient] add make target to copy and include files from pve-common - update Exception.pm

Dietmar Maurer dietmar at proxmox.com
Thu Jun 14 10:03:54 CEST 2018


Signed-off-by: Dietmar Maurer <dietmar at proxmox.com>
---
 Makefile                   |  7 +++++
 PVE/APIClient/Exception.pm | 66 +++++++++++++++++++++++++++++++++++++++-------
 2 files changed, 64 insertions(+), 9 deletions(-)

diff --git a/Makefile b/Makefile
index ad1efda..ec67f2d 100644
--- a/Makefile
+++ b/Makefile
@@ -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 at repo.proxmox.com upload --product pmg,pve --dist stretch
diff --git a/PVE/APIClient/Exception.pm b/PVE/APIClient/Exception.pm
index 3f9843c..9e42bc9 100644
--- a/PVE/APIClient/Exception.pm
+++ b/PVE/APIClient/Exception.pm
@@ -6,20 +6,21 @@ package PVE::APIClient::Exception;
 
 use strict;
 use warnings;
-
-use base 'Exporter';
-
-use Storable qw(dclone);
+use vars qw(@ISA @EXPORT_OK);
+require Exporter;
+use Storable qw(dclone);       
 use HTTP::Status qw(:constants);
 
+ at ISA = qw(Exporter);
+
 use overload '""' => sub {local $@; shift->stringify};
 use overload 'cmp' => sub {
     my ($a, $b) = @_;
-    local $@;
+    local $@;  
     return "$a" cmp "$b"; # compare as string
 };
 
-our @EXPORT_OK = qw(raise);
+ at EXPORT_OK = qw(raise raise_param_exc raise_perm_exc);
 
 sub new {
     my ($class, $msg, %param) = @_;
@@ -31,7 +32,7 @@ sub new {
     };
 
     foreach my $p (keys %param) {
-	next if defined($self->{$p});
+	next if defined($self->{$p}); 
 	my $v = $param{$p};
 	$self->{$p} = ref($v) ? dclone($v) : $v;
     }
@@ -42,7 +43,52 @@ sub new {
 sub raise {
 
     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;
 
     $exc->{filename} = $filename;
@@ -53,13 +99,15 @@ sub raise {
 
 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";
     }
 
@@ -86,7 +134,7 @@ sub stringify {
 sub PROPAGATE {
     my ($self, $file, $line) = @_;
 
-    push @{$self->{propagate}}, [$file, $line];
+    push @{$self->{propagate}}, [$file, $line]; 
 
     return $self;
 }
-- 
2.11.0




More information about the pve-devel mailing list