File Coverage

blib/lib/JSON/Relaxed/ErrorCodes.pm
Criterion Covered Total %
statement 17 18 94.4
branch 3 4 75.0
condition 1 3 33.3
subroutine 4 4 100.0
pod 0 1 0.0
total 25 30 83.3


line stmt bran cond sub pod time code
1             #! perl
2              
3 3     3   50 use v5.26;
  3         45  
4 3     3   20 use utf8;
  3         34  
  3         25  
5              
6             package JSON::Relaxed::ErrorCodes;
7              
8 3     3   220 use JSON::Relaxed::Parser; our $VERSION = $JSON::Relaxed::Parser::VERSION;
  3         5  
  3         2149  
9              
10             =head1 JSON::Relaxed::ErrorCodes -- Error messages
11              
12             If the document cannot be parsed, JSON::Relaxed will normally throw an
13             exception.
14              
15             In legacy mode, JSON::Relaxed returns an undefined
16             value instead and sets the following error indicators:
17              
18             =over 4
19              
20             =item * $JSON::Relaxed::err_id
21              
22             A unique code for a specific error.
23              
24             =item * $JSON::Relaxed::err_msg
25              
26             An English description of the error, including an indication where the
27             error occurs.
28              
29             =back
30              
31             When using object-oriented mode, these can be easily retrieved using
32             the parser methods err_id() and err_msg().
33              
34             Following is a list of all error codes in JSON::Relaxed:
35              
36             =over 4
37              
38             =item * C
39              
40             No input was found. This can be caused by:
41              
42             $parser->decode()
43             $parser->decode(undef)
44              
45             =item * C
46              
47             The string to be parsed has no content beside whitespace and comments.
48              
49             $parser->decode('')
50             $parser->decode(' ')
51             $parser->decode('/* whatever */')
52              
53             =item * C
54              
55             A comment was started with /* but was never closed. For example:
56              
57             $parser->decode('/*')
58              
59             =item * C
60              
61             The document opens with an invalid structural character like a comma or colon.
62             The following examples would trigger this error.
63              
64             $parser->decode(':')
65             $parser->decode(',')
66             $parser->decode('}')
67             $parser->decode(']')
68              
69             =item * C
70              
71             The document has multiple structures. JSON and RJSON only allow a document to
72             consist of a single hash, a single array, or a single string. The following
73             examples would trigger this error.
74              
75             $parse->decode('{}[]')
76             $parse->decode('{} "whatever"')
77             $parse->decode('"abc" "def"')
78              
79             =item * C
80              
81             A hash key may only be followed by the closing hash brace or a colon. Anything
82             else triggers C. So, the following examples would
83             trigger this error.
84              
85             $parse->decode("{a [ }") }
86             $parse->decode("{a b") }
87              
88             =item * C
89              
90             The parser encountered something besides a string where a hash key should be.
91             The following are examples of code that would trigger this error.
92              
93             $parse->decode('{{}}')
94             $parse->decode('{[]}')
95             $parse->decode('{]}')
96             $parse->decode('{:}')
97              
98             =item * C
99              
100             A hash has an opening brace but no closing brace. For example:
101              
102             $parse->decode('{x:1')
103              
104             =item * C
105              
106             An array has an opening brace but not a closing brace. For example:
107              
108             $parse->decode('["x", "y"')
109              
110             =item * C
111              
112             In a hash, a colon must be followed by a value. Anything else triggers this
113             error. For example:
114              
115             $parse->decode('{"a":,}')
116             $parse->decode('{"a":}')
117              
118             =item * C
119              
120             In an array, a comma must be followed by a value, another comma, or the closing
121             array brace. Anything else triggers this error. For example:
122              
123             $parse->decode('[ "x" "y" ]')
124             $parse->decode('[ "x" : ]')
125              
126             =item * C
127              
128             This error exists just in case there's an invalid token in an array that
129             somehow wasn't caught by C. This error
130             shouldn't ever be triggered. If it is please L.
131              
132             =item * C
133              
134             This error is triggered when a quote isn't closed. For example:
135              
136             $parse->decode("'whatever")
137             $parse->decode('"whatever') }
138              
139             =back
140              
141             =cut
142              
143             my %msg =
144             ( 'missing-input' => 'the string to be parsed is empty or undefined',
145             'unknown-array-token' => 'unexpected array token',
146             'empty-input' =>
147             'the string to be parsed has no content',
148             'unclosed-inline-comment' =>
149             'a comment was started with /* but was never closed',
150             'invalid-structure-opening-character' =>
151             'expected opening brace or opening bracket',
152             'multiple-structures' =>
153             'the string being parsed contains more than one structure',
154             'unknown-token-after-key' =>
155             'expected colon, comma or closing brace after a hash key',
156             'unknown-token-for-hash-key' =>
157             'expected string, comma, or closing brace in a hash key',
158             'unclosed-hash-brace' =>
159             'missing closing brace for hash',
160             'unclosed-array-brace' =>
161             'missing closing brace for array',
162             'unexpected-token-after-colon' =>
163             'expected a value after a colon in a hash',
164             'missing-comma-between-array-elements' =>
165             'expected comma or closing array brace',
166             'unknown-array-token' =>
167             'unexpected token in array',
168             'unclosed-quote' =>
169             'missing closing quote for string'
170             );
171              
172             sub message {
173 4     4 0 11 my ( $self, $id, $aux ) = @_;
174 4   33     15 my $msg = $msg{$id} // ($id =~ s/-/ /gr);
175 4 50       17 if ( $aux ) {
176             # Calculate line/col from offset.
177 4         12 my @a = split( /\r\n?|\n/, $aux->parent->data );
178 4 100       14 if ( @a > 1 ) {
179 2         7 @a = split( /\r\n?|\n/, substr( $aux->parent->data, 0, $aux->offset ) );
180 2         21 $msg .= sprintf( " (line %d, col %d, before %s)",
181             0+@a, length($a[-1]), $aux->as_string );
182             }
183             else {
184             # Single line, assume string.
185 2         3 $msg .= sprintf( " (offset %d, before %s)",
186             $aux->offset, $aux->as_string );
187             }
188             }
189             else {
190 0         0 $msg .= " (at end of string)";
191             }
192 4         13 $msg;
193             }
194              
195             1;