File Coverage

blib/lib/JSON/Schema/Modern/Error.pm
Criterion Covered Total %
statement 75 76 98.6
branch 4 4 100.0
condition n/a
subroutine 25 26 96.1
pod 1 2 50.0
total 105 108 97.2


line stmt bran cond sub pod time code
1 46     46   595 use strict;
  46         190  
  46         1518  
2 46     46   233 use warnings;
  46         82  
  46         2865  
3             package JSON::Schema::Modern::Error;
4             # vim: set ts=8 sts=2 sw=2 tw=100 et :
5             # ABSTRACT: Contains a single error from a JSON Schema evaluation
6              
7             our $VERSION = '0.641';
8              
9 46     46   601 use 5.020;
  46         129  
10 46     46   183 use Moo;
  46         99  
  46         359  
11             with 'JSON::Schema::Modern::ResultNode';
12 46     46   16236 use strictures 2;
  46         395  
  46         1558  
13 46     46   16825 use stable 0.031 'postderef';
  46         680  
  46         293  
14 46     46   8014 use experimental 'signatures';
  46         97  
  46         183  
15 46     46   2436 no autovivification warn => qw(fetch store exists delete);
  46         73  
  46         374  
16 46     46   3175 use if "$]" >= 5.022, experimental => 're_strict';
  46         122  
  46         1090  
17 46     46   3021 no if "$]" >= 5.031009, feature => 'indirect';
  46         133  
  46         2712  
18 46     46   200 no if "$]" >= 5.033001, feature => 'multidimensional';
  46         87  
  46         2335  
19 46     46   233 no if "$]" >= 5.033006, feature => 'bareword_filehandles';
  46         74  
  46         2361  
20 46     46   200 no if "$]" >= 5.041009, feature => 'smartmatch';
  46         64  
  46         1574  
21 46     46   176 no feature 'switch';
  46         65  
  46         1023  
22 46     46   154 use MooX::TypeTiny;
  46         68  
  46         352  
23 46     46   31734 use Types::Standard qw(Str Bool Enum Tuple);
  46         304  
  46         475  
24 46     46   83864 use Types::Common::Numeric qw(PositiveInt);
  46         1079542  
  46         427  
25 46     46   33494 use builtin::compat 'refaddr';
  46         85  
  46         426  
26 46     46   25781 use Mojo::Message::Response;
  46         2232945  
  46         464  
27 46     46   2089 use Carp 'croak';
  46         90  
  46         2293  
28 46     46   221 use namespace::clean;
  46         68  
  46         401  
29              
30             use overload
31 0     0   0 '""' => sub { $_[0]->stringify },
32 46     46   15683 fallback => 1;
  46         94  
  46         309  
33              
34             has error => (
35             is => 'ro',
36             isa => Str,
37             required => 1,
38             );
39              
40             has exception => (
41             is => 'ro',
42             isa => Bool,
43             );
44              
45             has mode => (
46             is => 'ro',
47             isa => Enum[qw(traverse evaluate)],
48             required => 1,
49             );
50              
51             has recommended_response => (
52             is => 'ro',
53             isa => Tuple[PositiveInt, Str],
54             );
55              
56 7     7 0 29 sub stringify ($self) {
  7         6  
  7         9  
57 7 100       64 $self->mode eq 'traverse'
58             ? '\''.$self->keyword_location.'\': '.$self->error
59             : '\''.$self->instance_location.'\': '.$self->error;
60             }
61              
62 2     2 1 27 sub clone ($self, %overrides) {
  2         3  
  2         6  
  2         3  
63             $self->new(
64             $self->%{qw(instance_location keyword_location keyword depth)},
65             (map +(exists $self->{$_} ? $self->%{$_} : ()), qw(absolute_keyword_location _uri recommended_response)),
66 2 100       97 $self->%{qw(error exception mode)},
67             %overrides,
68             );
69             }
70              
71 18546     18546   35523 sub __thing { 'error' }
72              
73             around BUILDARGS => sub ($orig, $class, @args) {
74             my $args = $class->$orig(@args);
75              
76             $args->{recommended_response}[1] =
77             Mojo::Message::Response->default_message($args->{recommended_response}[0]) // 'Unknown Error'
78             if $args->{recommended_response} and $args->{recommended_response}->@* == 1;
79              
80             croak 'instance_location must be defined when mode=evaluate'
81             if not defined $args->{instance_location} and ($args->{mode}//'') eq 'evaluate';
82              
83             return $args;
84             };
85              
86             1;
87              
88             __END__