PGObject/Util/DBException.pm

filter: Statementstmt 90.0% 27 / 30 Branchbran 20.0% 2 / 10 Conditioncond 50.0% 1 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 87.5% 7 / 8 Podpod 100% 4 / 4 totaltotal 74.5% 41 / 55 CC 7 SCAR 14.7
CC
7
Cov
71%
new
12.0
internal
3.6
log_msg
3.1
CRAP 4.4
« PGObject/Type/Registry.pm ↑ Summary
1 =head1 NAME
2  
3 PGObject::Util::DBException -- Database Exceptions for PGObject
4  
5 =cut
6  
7 package PGObject::Util::DBException;
8  
9 =head1 VERSION
10  
11 2.4.0
12  
13 =cut
14  
15 7 our $VERSION = '2.4.0';
16  
17 =head1 SYNOPSIS
18  
19 use PGObject::Util::DBException;
20  
21 $dbh->execute(@args) || die
22 PGObject::Util::DBException->new($dbh, $query, @args);
23  
24 # if you need something without dbh:
25  
26 die PGObject::Util::DBException->internal($state, $string, $query, $args);
27  
28 # if $dbh is undef, then we assume it is a connection error and ask DBI
29  
30 # in a handler you can check
31 try {
32 some_db_func();
33 } catch {
34 if ($_->isa('PGObject::Util::DBException')){
35 if ($_->{state} eq '23505') {
36 warn "Duplicate data detected.";
37 }
38 log($_->log_msg);
39 die $_;
40 }
41 else {
42 die $_;
43 }
44  
45 =cut
46  
47 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
48 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
49 7 ▶use overload '""' => 'short_string';
Subroutine BEGIN: called CC=1 SCAR=0.0
50 7 ▶use DBI;
Subroutine BEGIN: called CC=1 SCAR=0.0
51  
52 7 our $STRINGIFY_STACKTRACE = 1;
53  
54 =head1 DESCRIPTION
55  
56 Database errors occur sometimes for a variety of reasons, including bugs,
57 environmental, security, or user access problems, or a variety of other
58 reasons. For applications to appropriately handle database errors, it is often
59 necessary to be able to act on categories of errors, while if we log errors for
60 later analysis we want more information there. For debugging (or even logging)
61 we might even want to capture stack traces in order to try to understand where
62 errors came from. On the other hand, if we just want to display an error, we
63 want to get an appropriate error string back.
64  
65 This class provides both options. On one side, it provides data capture for
66 logging, introspection, and analysis. On the other it provides a short string
67 form for display purposes.
68  
69 This is optimized around database errors. It is not intended to be a general
70 exception class outside the database layer.
71  
72 If C<Devel::StackTrace> is loaded we also capture a stack trace.
73  
74 =head2 Internal Error Codes
75  
76 In order to handle internal PGObject errors, we rely on the fact that no
77 current SQL subclasses contian the letter 'A' which we will use to mean
78 Application. We therefore take existing SQLState classes and use AXX
79 (currently only A01 is used currently) to handle these errors.
80  
81 =over
82  
83 =item 26A01
84  
85 Function not found. No function with the discovery criteria set was found.
86  
87 =item 42A01
88  
89 Function not unique. Multiple functions for the discovery criteria were
90 found.
91  
92 =back
93  
94 =head2 Stack Traces
95  
96 If C<Devel::StackTrace> is loaded, we will capture stack traces starting at the
97 exception class call itself.
98  
99 In order to be unobtrusive, these are stringified by default. This is to avoid
100 problems of reference counting and lifecycle that can happen when capturing
101 tracing information, If you want to capture the whole stack trace without
102 stringification, then you can set the following variable to 0:
103 C<PGObject::Util::DBException::STRINGIFY_STACKTRACE>. Naturally this is best
104 done using the C<local> keyword.
105  
106 Note that non-stringified stacktraces are B<not> weakened and this can cause
107 things like database handles to persist for longer than they ordinarily would.
108 For this reason, turning off stringification is best reserved for cases where
109 it is absolutely required.
110  
111 =head1 CONSTRUCTORS
112  
113 All constructors are called exclusively via C<$class->method> syntax.
114  
115 =head2 internal($state, $errstr, $query, $args);
116  
117 Used for internal application errors. Creates an exception of this type with
118 these attributes. This is useful for appication errors within the PGObject
119 framework.
120  
121 =cut
122  
123 1 ▶sub internal ($$$$@) {
Subroutine internal: called CC=3 SCAR=12.7
Pod documented
124 1 my ($class, $state, $errstr, $query, @args) = @_;
125 1 my $self = {
126 state => $state,
127 errstr => $errstr,
128 query => $query,
129 args => \@args,
130 trace => undef,
131 };
132 1 ▶ if (scalar grep { $_ eq 'Devel/StackTrace.pm' } keys %INC){
Branch: 1 branch
BranchTrueFalseTotal
if (scalar grep({$_ eq "Devel/StackTrace.pm";} keys %INC)) 0 1 1
133 0 ▶ $self->{trace} = $STRINGIFY_STACKTRACE ? Devel::StackTrace->new->as_string
Branch: 1 branch
BranchTrueFalseTotal
$PGObject::Util::DBException::STRINGIFY_STACKTRACE ? : 0 0 0
134 : Devel::StackTrace->new;
135 }
136 1 bless $self, $class;
137 }
138  
139  
140 =head2 new($dbh, $query, @args)
141  
142 This creates a new exception object. The SQL State is taken from the C<$dbh>
143 database handle if it is defined, and the C<DBI> module if it is not.
144  
145 =cut
146  
147 0 ▶sub new ($$$@) {
Subroutine new: not called CC=3 SCAR=24.8
Pod documented
148 0 my ($class, $dbh, $query, @args) = @_;
149 0 ▶ return $class->internal(
Branch: 2 branches
BranchTrueFalseTotal
defined $dbh ? : 0 0 0
defined $dbh ? : 0 0 0
150 (defined $dbh ? $dbh->state : $DBI::state ),
151 (defined $dbh ? $dbh->errstr : $DBI::errstr ),
152 $query, @args
153 );
154 }
155  
156 =head1 Stringificatoin
157  
158 This module provides two methods for string representation. The first, for
159 human-focused error messages also overloads stringification generally. The
160 second is primarily intended for logging purposes.
161  
162 =head2 short_string
163  
164 The C<short_string> method returns a short string of C<state: errstr> for human
165 presentation.
166  
167 =cut
168  
169 3 ▶sub short_string ($) {
Subroutine short_string: called CC=1 SCAR=0.0
Pod documented
170 3 my $self = shift;
171 3 return "$self->{state}: $self->{errstr}";
172 }
173  
174 =head2 log_msg
175  
176 As its name suggests, C<log_msg> aimes to provide full infomation for logging
177 purposes.
178  
179 The format here is:
180  
181 STATE state, errstr
182 Query: query
183 Args: joun(',', @args)
184 Trace: Stacktrace
185  
186  
187 =cut
188  
189 1 ▶sub log_msg ($) {
Subroutine log_msg: called CC=3 SCAR=11.3
Pod documented
190 1 my $self = shift;
191 1 ▶ my $query = ( $self->{query} // 'None' );
Condition50%1/2
$self->{"query"} // "None"
l!l
10
MC/DC0%0/1
$self->{"query"} // "None"
$self->{"query"}
Truth table50%1/2
A
A= $self->{"query"}
Aresult
11
00
192 my $string = join "\n",
193 "STATE $self->{state}, $self->{errstr}",
194 "Query: $query",
195 1 ▶ "Args: " . (join ',', @{$self->{args}}),
Branch: 1 branch
BranchTrueFalseTotal
$self->{'trace'} ? : 0 1 1
196 1 ($self->{trace} ? "Trace: $self->{trace}" : ());
197 1 return $string;
198 }
199  
200 7 1;
« PGObject/Type/Registry.pm ↑ Summary