js-yaml icon indicating copy to clipboard operation
js-yaml copied to clipboard

Improve "unacceptable kind of an object to dump" error message

Open miso-belica opened this issue 3 years ago • 3 comments

Hello, today I was hunting for a bug during serializing JSON into YAML. I was getting unacceptable kind of an object to dump [object Undefined] but there was no undefined in the JSON. After an hour I was able to find the cause and it was this fragment 1: [null] in the JSON below:

{
	START: {
		CALL: [{ 1: [null], variable: 'utterance' }],
	},
}

The relevant code is at https://github.com/nodeca/js-yaml/blob/86aabf133f5c008b297593cc30a51e44cf63553d/lib/js-yaml/dumper.js#L779

The JSON was much bigger and nested that is why it was not easy. But I would like to ask for a better error message so it will be much easier in the future. Would be nice to write the path to the invalid value. For example like this unacceptable kind of an object to dump at path.to.object.START.CALL.0."1".0. Got: ${type}.

I didn't go deeply into the code but if this is possible to get and if you are OK with it I can even try to implement it and send PR.

Thanks for the answer and have a nice day :)

miso-belica avatar Sep 18 '20 13:09 miso-belica

I didn't go deeply into the code but if this is possible to get and if you are OK with it I can even try to implement it and send PR.

You can try. Unfortunately, I don't think path to the object is stored anywhere at the moment. Adding it might be a hard task and can run into performance issues.

rlidwka avatar Dec 18 '20 14:12 rlidwka

@miso-belica did you found solution for this?

ks221197 avatar Jul 08 '22 12:07 ks221197

@ks221197 yes, the problem is undefined/null somewhere in the JSON. You just have to do inspect(data) before serialising to catch it and fix it.

miso-belica avatar Jul 11 '22 07:07 miso-belica