File Coverage

blib/lib/Form/Tiny/Error.pm
Criterion Covered Total %
statement 56 60 93.3
branch 2 2 100.0
condition 5 5 100.0
subroutine 20 23 86.9
pod 0 6 0.0
total 83 96 86.4


line stmt bran cond sub pod time code
1             package Form::Tiny::Error;
2             $Form::Tiny::Error::VERSION = '2.26';
3 53     53   809 use v5.10;
  53         217  
4 53     53   284 use strict;
  53         270  
  53         1848  
5 53     53   374 use warnings;
  53         149  
  53         3430  
6 53     53   23896 use Moo;
  53         330356  
  53         340  
7 53     53   75908 use Types::Standard qw(Maybe Str InstanceOf);
  53         141  
  53         434  
8 53     53   102368 use Types::TypeTiny qw(StringLike);
  53         104  
  53         489  
9 53     53   185928 use Carp qw(confess);
  53         110  
  53         3721  
10              
11             use overload
12 53         437 q{""} => 'as_string',
13 53     53   335 fallback => 1;
  53         114  
14              
15             has 'field_def' => (
16             is => 'ro',
17             isa => InstanceOf['Form::Tiny::FieldDefinition'],
18             writer => 'set_field_def',
19             predicate => 'has_field_def',
20             );
21              
22             has 'field' => (
23             is => 'ro',
24             isa => Str,
25             writer => 'set_field',
26             predicate => 'has_field',
27             );
28              
29             has 'error' => (
30             is => 'ro',
31             isa => StringLike,
32             writer => 'set_error',
33             builder => 'default_error',
34             );
35              
36             sub BUILD
37             {
38 162     162 0 93560 my ($self) = @_;
39              
40 162 100 100     1759 if (!$self->field && $self->field_def) {
41 105         2771 $self->set_field($self->field_def->name);
42             }
43             }
44              
45             sub default_error
46             {
47 0     0 0 0 confess 'no error message supplied';
48 0         0 return 'Unknown error';
49             }
50              
51             sub get_error
52             {
53 26     26 0 4079 my ($self) = @_;
54              
55 26         154 return $self->error;
56             }
57              
58             sub as_string
59             {
60 11     11 0 10701 my ($self) = @_;
61              
62 11   100     152 my $field = $self->field // 'general';
63 11         28 my $error = $self->get_error;
64 11         43 return "$field - $error";
65             }
66              
67             # in-place subclasses
68             {
69              
70             # Internal use only
71             package Form::Tiny::Error::NestedFormError;
72             $Form::Tiny::Error::NestedFormError::VERSION = '2.26';
73 53     53   41149 use parent -norequire, 'Form::Tiny::Error';
  53         15147  
  53         384  
74              
75             }
76              
77             {
78              
79             package Form::Tiny::Error::InvalidFormat;
80             $Form::Tiny::Error::InvalidFormat::VERSION = '2.26';
81 53     53   5063 use parent -norequire, 'Form::Tiny::Error';
  53         112  
  53         250  
82              
83             sub default_error
84             {
85 8     8   6271 return 'input data format is invalid';
86             }
87             }
88              
89             {
90              
91             package Form::Tiny::Error::Required;
92             $Form::Tiny::Error::Required::VERSION = '2.26';
93 53     53   4756 use parent -norequire, 'Form::Tiny::Error';
  53         117  
  53         210  
94              
95             sub default_error
96             {
97 21     21   12250 return 'field is required';
98             }
99             }
100              
101             {
102              
103             package Form::Tiny::Error::IsntStrict;
104             $Form::Tiny::Error::IsntStrict::VERSION = '2.26';
105 53     53   4410 use Moo;
  53         105  
  53         370  
106 53     53   22876 use Types::Standard qw(Str);
  53         142  
  53         480  
107              
108             extends 'Form::Tiny::Error';
109              
110             has 'extra_field' => (
111             is => 'ro',
112             isa => Str,
113             required => 1,
114             );
115              
116             sub default_error
117             {
118 0     0 0 0 return 'input data has unexpected fields';
119             }
120              
121             sub get_error
122             {
123 12     12 0 35 my ($self) = @_;
124              
125 12         25 my $field = $self->extra_field;
126 12         36 my $error = $self->error;
127 12         31 return "$field: $error";
128             }
129             }
130              
131             {
132              
133             package Form::Tiny::Error::DoesNotValidate;
134             $Form::Tiny::Error::DoesNotValidate::VERSION = '2.26';
135 53     53   158749 use parent -norequire, 'Form::Tiny::Error';
  53         127  
  53         300  
136              
137             sub default_error
138             {
139 0     0     return 'data validation failed';
140             }
141             }
142              
143             1;
144              
145             __END__