Robustness in toJSON for errors