File tree Expand file tree Collapse file tree 2 files changed +24
-1
lines changed Expand file tree Collapse file tree 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)
8888 *
8989 * @see http://php.net/json_decode [JSON decode native function]
9090 *
91+ * @throws \RuntimeException When decode fails.
92+ *
9193 * @return mixed
9294 */
9395 public function decode ($ json , $ assoc = false , $ depth = 512 , $ options = 0 )
9496 {
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 ;
96110 }
97111}
Original file line number Diff line number Diff line change @@ -27,6 +27,15 @@ public function testDecode($json)
2727 $ this ->assertArrayHasKey ('b ' , $ actual );
2828 }
2929
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+
3039 public function theTests ()
3140 {
3241 return [
You can’t perform that action at this time.
0 commit comments