[pve-devel] r4920 - in pve-manager/pve2: . lib/PVE

svn-commits at proxmox.com svn-commits at proxmox.com
Wed Jul 21 12:32:50 CEST 2010


Author: dietmar
Date: 2010-07-21 10:32:50 +0000 (Wed, 21 Jul 2010)
New Revision: 4920

Added:
   pve-manager/pve2/lib/PVE/Exception.pm
Modified:
   pve-manager/pve2/ChangeLog
   pve-manager/pve2/lib/PVE/Makefile.am
Log:
	* lib/PVE/Exception.pm: a way to add more information to
	exceptions (see man perlfunc (die))



Modified: pve-manager/pve2/ChangeLog
===================================================================
--- pve-manager/pve2/ChangeLog	2010-07-21 08:28:19 UTC (rev 4919)
+++ pve-manager/pve2/ChangeLog	2010-07-21 10:32:50 UTC (rev 4920)
@@ -1,3 +1,8 @@
+2010-07-21  Proxmox Support Team  <support at proxmox.com>
+
+	* lib/PVE/Exception.pm: a way to add more information to
+	exceptions (see man perlfunc (die))
+
 2010-07-19  Proxmox Support Team  <support at proxmox.com>
 
 	* lib/PVE/RESTHandler.pm (AUTOLOAD): make accessors for methods

Added: pve-manager/pve2/lib/PVE/Exception.pm
===================================================================
--- pve-manager/pve2/lib/PVE/Exception.pm	                        (rev 0)
+++ pve-manager/pve2/lib/PVE/Exception.pm	2010-07-21 10:32:50 UTC (rev 4920)
@@ -0,0 +1,80 @@
+#!/usr/bin/perl -w
+
+# a way to add more information to exceptions (see man perlfunc (die))
+# use PVE::Exception qw(raise);
+# raise (400, "my error message", param1 => 1, param2 => 2);
+
+package PVE::Exception;
+
+use strict;
+use vars qw(@ISA @EXPORT_OK);
+require Exporter;
+
+ at ISA = qw(Exporter);
+
+use overload '""' => sub {local $@; shift->stringify};
+
+ at EXPORT_OK = qw(raise);
+
+sub new {
+    my ($class, $code, $msg, %params) = @_;
+
+    $class = ref($class) || $class;
+
+    my $self = {
+	code => $code,
+	msg => $msg,
+    };
+
+    foreach my $p (keys %params) {
+	next if defined($self->{$p}); 
+	$self->{$p} = $params{$p};
+    }
+
+    return bless $self;
+}
+
+sub raise {
+
+    my ($pkg, $filename, $line) = caller;
+
+    my $exc = PVE::Exception->new(@_);
+    
+    $exc->{filename} = $filename;
+    $exc->{line} = $line;
+
+    die $exc;
+}
+
+sub stringify {
+    my $self = shift;
+    
+    my $msg = $self->{msg};
+
+    if ($msg !~ m/\n$/) {
+
+	if ($self->{filename} && $self->{line}) {
+	    $msg .= " at $self->{filename} line $self->{line}";
+	}
+
+	$msg .= "\n";
+    }
+
+    if ($self->{propagate}) {
+	foreach my $pi (@{$self->{propagate}}) {
+	    $msg .= "\t...propagated at $pi->[0] line $pi->[1]\n";
+	}
+    }
+
+    return $msg;
+}
+
+sub PROPAGATE {
+    my ($self, $file, $line) = @_;
+
+    push @{$self->{propagate}}, [$file, $line]; 
+
+    return $self;
+}
+
+1;


Property changes on: pve-manager/pve2/lib/PVE/Exception.pm
___________________________________________________________________
Added: svn:executable
   + *

Modified: pve-manager/pve2/lib/PVE/Makefile.am
===================================================================
--- pve-manager/pve2/lib/PVE/Makefile.am	2010-07-21 08:28:19 UTC (rev 4919)
+++ pve-manager/pve2/lib/PVE/Makefile.am	2010-07-21 10:32:50 UTC (rev 4920)
@@ -4,6 +4,7 @@
 
 PERLSOURCE = 			\
 	JSONSchema.pm		\
+	Exception.pm		\
 	API2.pm			\
 	API2Client.pm		\
 	APIDaemon.pm		\



More information about the pve-devel mailing list