File tree 2 files changed +24
-1
lines changed
2 files changed +24
-1
lines changed Original file line number Diff line number Diff line change @@ -88,10 +88,24 @@ protected function hasCommentEnded($char, $charnext)
88
88
*
89
89
* @see http://php.net/json_decode [JSON decode native function]
90
90
*
91
+ * @throws \RuntimeException When decode fails.
92
+ *
91
93
* @return mixed
92
94
*/
93
95
public function decode ($ json , $ assoc = false , $ depth = 512 , $ options = 0 )
94
96
{
95
- return \json_decode ($ this ->strip ($ json ), $ assoc , $ depth , $ options );
97
+ $ decoded = \json_decode ($ this ->strip ($ json ), $ assoc , $ depth , $ options );
98
+
99
+ if (\JSON_ERROR_NONE !== $ err = \json_last_error ()) {
100
+ $ msg = 'JSON decode failed ' ;
101
+
102
+ if (\function_exists ('json_last_error_msg ' )) {
103
+ $ msg .= ': ' . \json_last_error_msg ();
104
+ }
105
+
106
+ throw new \RuntimeException ($ msg , $ err );
107
+ }
108
+
109
+ return $ decoded ;
96
110
}
97
111
}
Original file line number Diff line number Diff line change @@ -27,6 +27,15 @@ public function testDecode($json)
27
27
$ this ->assertArrayHasKey ('b ' , $ actual );
28
28
}
29
29
30
+ /**
31
+ * @expectedException \RuntimeException
32
+ * @expectedExceptionMessage JSON decode failed
33
+ */
34
+ public function testDecodeThrows ()
35
+ {
36
+ (new Comment )->decode ('{"a":1, /* comment */, "b":} ' , true );
37
+ }
38
+
30
39
public function theTests ()
31
40
{
32
41
return [
You can’t perform that action at this time.
0 commit comments