Exception.pm untested

filter: Statementstmt 0.0% 0 / 55 Branchbran 0.0% 0 / 20 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 0.0% 0 / 14 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 100 CC 11 SCAR 27.0
CC
11
Cov
0%
rethrowDefaults
30.0
handleDefaults
30.0
toTextUnknown
12.0
CRAP 14.9
1 # Copyright (C) 2017 Koha-Suomi
2 #
3 # This file is part of Pootle-Client.
4  
5 package Pootle::Exception;
6  
7 use Modern::Perl '2015';
8 use utf8;
9 binmode STDOUT, ':encoding(UTF-8)';
10 binmode STDERR, ':encoding(UTF-8)';
11 use feature 'signatures'; no warnings "experimental::signatures";
12 use Carp::Always;
13 use Try::Tiny;
14 use Scalar::Util qw(blessed);
15  
16 use Exception::Class (
17 'Pootle::Exception' => {
18 description => 'Pootle exceptions base class',
19 },
20 );
21  
22 sub newFromDie {
23 my ($class, $die) = @_;
24 return Pootle::Exception->new(error => "$die");
25 }
26  
27 =head2 rethrowDefault
28  
29 Because there are so many different types of exception classes with different
30 interfaces, use this to rethrow if you dont know exactly what you are getting.
31  
32 @PARAM1 somekind of monster
33  
34 =cut
35  
36 sub rethrowDefaults {
37 my ($e) = @_;
38  
39 die $e unless blessed($e);
40 die $e if $e->isa('Mojo::Exception'); #Dying a Mojo::Exception actually rethrows it.
41 $e->rethrow if ref($e) eq 'Pootle::Exception'; #If this is THE 'Pootle::Exception', then handle it here
42 $e->rethrow if $e->isa('Pootle::Exception');
43 $e->rethrow; #Exception classes are expected to implement rethrow like good exceptions should!!
44 }
45  
46 =head2 handleDefaults
47  
48 Handles all the boring exception cases in a default way. Saving you a lot of typing.
49  
50 =cut
51  
52 sub handleDefaults {
53 my ($e) = @_;
54  
55 return $e unless blessed($e);
56 return toTextMojo($e) if $e->isa('Mojo::Exception');
57 return $e->toText if ref($e) eq 'Pootle::Exception'; #If this is THE 'Pootle::Exception', then handle it here
58 return $e->toText if $e->isa('Pootle::Exception'); #If this is a subclass of 'Pootle::Exception', then let it through
59 return toTextUnknown($e);
60 }
61  
62 =head2 toText
63  
64 @RETURNS String, a textual representation of this exception,
65 Full::module::package :> error message, other supplied error keys
66  
67 =cut
68  
69 sub toText {
70 my ($self) = @_;
71  
72 my @sb;
73 push(@sb, ref($self).' :> '.$self->error);
74 # You can override global exception handling behaviour here.
75 # Maybe throw stack traces or somehow automatically identify
76 # supplementary exception payloads to show?
77 #
78 # while (my ($k, $v) = each(%$self)) {
79 # next if $k eq 'error';
80 # push(@sb, "$k => '$v'");
81 # }
82 return join(', ', @sb);
83 }
84  
85 =head2 toTextUnknown
86 @STATIC
87  
88 @RETURNS String, a textual representation of this exception,
89 Full::module::package :> error message, other supplied error keys
90  
91 =cut
92  
93 sub toTextUnknown {
94 my ($e) = @_;
95  
96 my @sb;
97 if (ref($e) eq 'HASH' || blessed($e)) {
98 while (my ($k, $v) = each(%$e)) {
99 push(@sb, "$k => '$v'");
100 }
101 }
102 elsif (ref($e) eq 'ARRAY') {
103 @sb = @$e;
104 }
105 else {
106 push(@sb, $e);
107 }
108 return join(', ', @sb);
109 }
110  
111 =head2 toTextMojo
112  
113 Returns a text representation of a Mojo::Exception
114  
115 =cut
116  
117 sub toTextMojo {
118 my ($e) = @_;
119 return $e->verbose(1)->to_string;
120 }
121  
122 1;