crack icon indicating copy to clipboard operation
crack copied to clipboard

Crack treats JSON parsing errors differently depending on the underlying YAML parser

Open matt-glover opened this issue 13 years ago • 0 comments

When a call to Crack::JSON.parse with non-JSON text, such as arbitrary HTML, fails and the underlying system is utilizing the old 'syck' parser a Crack::ParseError will bubble up to the caller. When the same call fails and the underlying system is utilizing the newer 'psych' parser the error bubbles up as a child of SyntaxError and is passed through Crack without wrapping it as a Crack::ParseError.

This makes it difficult for callers to use Crack::JSON.parse and know what error conditions to expect. For more information on the error bubbled up from Psych and why they claim this is the correct error to raise see the following issue: https://github.com/tenderlove/psych/issues/23.

psych falls back on a C extension to interface with a system library for YAML parsing. The C code in question throws an error that inherits from SyntaxError instead of StandardError: https://github.com/tenderlove/psych/blob/master/ext/psych/parser.c#L379

As a result a plain rescue statement will not catch this condition, however one specifically targeted at the SyntaxError type (or a parent type like Exception) will handle this problem. Catching a SyntaxError is necessarily the correct solution but hopefully this information serves to explain the source of the different behaviors.

matt-glover avatar Sep 15 '11 21:09 matt-glover