json-machine icon indicating copy to clipboard operation
json-machine copied to clipboard

Getting un catchable errors on non JSON files

Open flavinus opened this issue 3 years ago • 4 comments

When calling fromFile on a wrong file format I get some uncatchable errors. I had this issue with both 0.7.0 and 1.1.1

\JsonMachine\JsonMachine::fromFile($filename, "/myattribute");

I get the following fatal error with a zip file, but I can also have similar issues with text files

message: Undefined variable $P script: .../halaxa/json-machine/src/Parser.php line: 115

Testing vars before $tokenType = ${$token[0]}; seems to be helful.

if($token == null || !isset($token[0])|| !isset(${$token[0]})) { throw new JsonMachineException("Error parsing stream."); }

flavinus avatar Sep 12 '22 15:09 flavinus

If I get it right, do you mean when you feed JSON Machine a compressed zip file? That's usually not what you want to do in the first place, right? :)

halaxa avatar Sep 12 '22 18:09 halaxa

No, this is not what I want in my application but this can happen because of user input file. Of course I will verify the file type before trying to parse it, but it could be interesting to improve robustness of your library (thanks it is very usefull :) )

flavinus avatar Sep 12 '22 19:09 flavinus

I was just curious. You're right of course. The reason for this error is that the library is heavily optimized for performance thus ignoring some edge-case lexical errors, which a json token starting with capital P is. Those produce not very meaningful errors, such as yours. I'll look into handling such errors without hitting the performance.

halaxa avatar Sep 12 '22 21:09 halaxa

I'm failing to reproduce the error. Can you post the data or attach a failing test with this error? Place it in ParserTest please.

halaxa avatar Sep 19 '22 12:09 halaxa

Looking at it closer, the line $tokenType = ${$token[0]}; you mention is not there since version 1.0.1. It was removed here eeb2051bec8b7024d392ab28337ed7d2a68d95d8. That's why I haven't been able to replicate it in 1.1.1. The problem still persists as the Undefined array key error though. I'll look into it.

halaxa avatar Sep 29 '22 11:09 halaxa

Fixed on master. Check it out.

halaxa avatar Sep 29 '22 14:09 halaxa

Released in 1.1.2

halaxa avatar Sep 29 '22 15:09 halaxa

Thank you very much!!! Sorry I was too busy and I didn't took time to help you for rproduction.

flavinus avatar Sep 29 '22 16:09 flavinus

No problem. I decided to early-release after the comment. I hope the unit test is enough. 😁 However feel free to reach out if you have any problems.

halaxa avatar Sep 29 '22 16:09 halaxa