annotate Lib/IMPL/Exception.pm @ 22:da5bc24b3d3c

wmi tests
author Sergey
date Wed, 30 Sep 2009 17:41:32 +0400
parents 03e58a454b20
children 16ada169ca75
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
1 package IMPL::Exception;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
2 use strict;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
3 use overload
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
4 '""' => \&ToString,
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
5 'fallback' => 1;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
6 use Carp qw(longmess shortmess);
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
7 use Scalar::Util qw(refaddr);
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
8
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
9 BEGIN {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
10 require Error;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
11 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
12
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
13 use base qw(IMPL::Object::Accessor Error);
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
14
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
15 BEGIN {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
16 __PACKAGE__->mk_accessors( qw(Message Args CallStack Source) );
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
17 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
18
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
19 sub indent {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
20 my ($str,$level) = @_;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
21 $level ||= 0;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
22 $str = '' unless defined $str;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
23 join ("\n", map( "\t"x$level.$_ , split(/\n/,$str) ) );
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
24 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
25
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
26 sub new {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
27 my $class = shift;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
28 $class = ref $class || $class;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
29
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
30 my $this = $class->Error::new() or die "Failed to create an exception";
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
31
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
32 $this->callCTOR(@_);
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
33 $this->{-text} = $this->Message;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
34
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
35 local $Carp::CarpLevel = 0;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
36
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
37 $this->CallStack(longmess);
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
38 $this->Source(shortmess);
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
39
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
40 return $this;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
41 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
42
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
43 sub CTOR {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
44 my ($this,$message,@args) = @_;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
45 $this->Message($message || '');
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
46 die new IMPL::Exception("Fatal erorr: cyclic structure in the exceptions were detected, do not use \$\@ while throwing the exception!") if grep ref $_ ? refaddr($this) == refaddr($_) : 0 , @args;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
47 $this->Args([map defined $_ ? $_ : 'undef', @args]);
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
48 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
49
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
50 sub save {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
51 my ($this,$ctx) = @_;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
52
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
53 $ctx->AddVar(Message => $this->Message) if $this->Message;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
54 $ctx->AddVar(Args => $this->Args) if @{$this->Args};
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
55 $ctx->AddVar(Source => $this->Source);
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
56 $ctx->AddVar(CallStack => $this->CallStack);
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
57 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
58
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
59 sub restore {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
60 my ($class,$data,$instance) = @_;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
61
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
62 my %args = @$data;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
63
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
64 if ($instance) {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
65 $instance->callCTOR($args{Message},@{$args{Args}});
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
66 } else {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
67 $instance = $class->new($args{Message},@{$args{Args}});
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
68 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
69
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
70 $instance->Source($args{Source});
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
71 $instance->CallStack($args{CallStack});
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
72
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
73 return $instance;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
74 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
75
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
76 sub ToString {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
77 my $this = shift;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
78
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
79 $this->toString();
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
80 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
81
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
82 sub toString {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
83 my ($this,$notrace) = @_;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
84 $this->Message . join("\n",'',map { my $s = $_; local $_; indent("$s",1) } @{$this->Args} ) . ( $notrace ? '' : "\n" . $this->CallStack);
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
85 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
86
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
87 package IMPL::InvalidOperationException;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
88 our @ISA = qw(IMPL::Exception);
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
89 __PACKAGE__->PassThroughArgs;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
90
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
91 package IMPL::InvalidArgumentException;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
92 our @ISA = qw(IMPL::Exception);
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
93 __PACKAGE__->PassThroughArgs;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
94
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
95 package IMPL::DuplicateException;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
96 our @ISA = qw(IMPL::Exception);
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
97 __PACKAGE__->PassThroughArgs;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
98
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
99 package IMPL::NotImplementedException;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
100 our @ISA = qw(IMPL::Exception);
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
101 __PACKAGE__->PassThroughArgs;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
102
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
103 package Exception;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
104 our @ISA = qw(IMPL::Exception);
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
105 __PACKAGE__->PassThroughArgs;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
106
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
107 package IMPL::DeprecatedException;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
108 our @ISA = qw(IMPL::Exception);
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
109 our %CTOR = (
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
110 'IMPL::Exception' => sub { @_ ? @_ : "The method is deprecated" }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
111 );
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
112
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
113 1;