JsonSQL/Error.pm

filter: Statementstmt 76.4% 13 / 17 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 71.4% 5 / 7 Podpod 100% 4 / 4 totaltotal 78.5% 22 / 28 CC 1 SCAR 2.5
CC
1
Cov
76%
type
2.0
stringify
2.0
BEGIN
1.0
CRAP 1.3
« JsonSQL/Query/Query.pm ↑ Summary JsonSQL/Param/Condition.pm »
1 # ABSTRACT: Defines an error object to be used at various stages of JSON validation and SQL generation.
2  
3  
4 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use 5.014;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 package JsonSQL::Error;
9  
10 3 our $VERSION = '0.4'; # VERSION
11  
12  
13  
14 sub new {
15 21 ▶ my ( $class, $type, $message ) = @_;
Subroutine new: called CC=1 SCAR=0.0
Pod documented
16  
17 21 return bless {
18 message => $message,
19 type => $type
20 }, $class;
21 }
22  
23  
24 13 ▶sub is_error { 1 }
Subroutine is_error: called CC=1 SCAR=0.0
Pod documented
25  
26  
27 sub type {
28 0 ▶ my $this = shift;
Subroutine type: not called CC=1 SCAR=6.9
Pod documented
29 0 return $this->{type};
30 }
31  
32  
33 sub stringify {
34 0 ▶ my $this = shift;
Subroutine stringify: not called CC=1 SCAR=6.9
Pod documented
35 0 return "Error($this->{type}): $this->{message}";
36 }
37  
38  
39 3 1;
40  
41 __END__
42  
43 =pod
44  
45 =encoding UTF-8
46  
47 =head1 NAME
48  
49 JsonSQL::Error - Defines an error object to be used at various stages of JSON validation and SQL generation.
50  
51 =head1 VERSION
52  
53 version 0.4
54  
55 =head1 SYNOPSIS
56  
57 To use this:
58  
59 return JsonSQL::Error->new(<error_type>, <error_msg>);
60  
61 To check return values for errors:
62  
63 my $retval = myFunc();
64 if ( eval { $retval->is_error } ) {
65 return "An error occurred: $retval->{message}";
66 } else {
67 ...
68 }
69  
70 This module was inspired by Brian D Foy's post on The Effective Perler,
71 L<https://www.effectiveperlprogramming.com/2011/10/return-error-objects-instead-of-throwing-exceptions/>
72  
73 =head1 METHODS
74  
75 =head2 Constructor new($type, $message)
76  
77 Instantiates and returns a new JsonSQL::Error object.
78  
79 $type => Any string to group error messages by.
80 $message => The error message.
81  
82 =head2 ObjectMethod is_error -> 1
83  
84 Returns a true value. Used for conveniently catching errors:
85  
86 if ( eval { $result->is_error } ) {
87 die $result->{message};
88 }
89  
90 =head2 ObjectMethod type() -> $type
91  
92 Returns the type property. Useful for adding for/when loops to error handlers:
93  
94 for ( $result->type ) {
95 when ( 'validate' ) {
96 $err = "JSON schema validation error: <br />";
97 $err .= "$result->{message} <br />";
98 $err =~ s/\n/\<br \/\>/;
99 }
100 default {
101 $err = "An unspecified error occurred. <br />";
102 }
103 }
104  
105 =head2 ObjectMethod stringify() -> $string
106  
107 Stringifies the error object and returns it.
108  
109 =head1 AUTHOR
110  
111 Chris Hoefler <bhoefler@draper.com>
112  
113 =head1 COPYRIGHT AND LICENSE
114  
115 This software is copyright (c) 2017 by Chris Hoefler.
116  
117 This is free software; you can redistribute it and/or modify it under
118 the same terms as the Perl 5 programming language system itself.
119  
120 =cut
« JsonSQL/Query/Query.pm ↑ Summary JsonSQL/Param/Condition.pm »