[iso-core] fix error toJSON in node environments

Summary:
Node's native `Error` object does NOT implement toJSON. We attempt to call
toJSON when reporting errors. This wasn't noticed until now because
bunyan's pretty logger (which only run in dev mode) started JSONifying
errors

Test Plan:
Try and API auth with a bad username with local setup. See that it throws
toJSON error. After patch, error properly serializes

Reviewers: spang, halla, jerm, juan

Reviewed By: juan

Differential Revision: https://phab.nylas.com/D3975
This commit is contained in:
Evan Morikawa 2017-02-17 17:31:36 -08:00
parent c149e9171d
commit 5577d1b4d3

View file

@ -1,7 +1,11 @@
class NylasError extends Error {
toJSON() {
const json = super.toJSON() || {}
let json = {}
if (super.toJSON) {
// Chromium `Error`s have a `toJSON`, but Node `Error`s do NOT!
json = super.toJSON()
}
Object.getOwnPropertyNames(this).forEach((key) => {
json[key] = this[key];
});