2

My code (fiddle) is:

let count = 0;
replacer = (k, v) => {
  console.log(`count: ${count++}`);
  if (v === undefined)
    return null;
  else
    return v;
}

try {
  const x = y;
} catch (err) {
  console.log(err);
  console.log("=====");
  console.log(`typeof err is ${typeof err}`);
  console.log(JSON.stringify(err));
  console.log("-----");
  console.log(JSON.stringify(err, replacer));
}

The output is:

ReferenceError: y is not defined
    at window.onload ((index):42)
=====
typeof err is object
{}
-----
count: 0
{}

Is err really {}? If not, how do I get a JSON string representation of it?

8
  • What browser are you using? I'm getting {"line":42,"column":18,"sourceURL":"https://fiddle.jshell.net/OldGeezer/w2gzkntv/3/show/"} in Safari. Commented Jul 18, 2019 at 4:09
  • Chrome. @Eddie converted the post to a snippet, but the result output is not the same as the Fiddle's. The snippet gives {} for console.log(err). Commented Jul 18, 2019 at 4:15
  • 1
    It may be that the error object itself has no enumerable properties, they may be on its [[Prototype]], or they might be getters (so are functions and ignored by JSON.stringify). Try for..in. Commented Jul 18, 2019 at 4:20
  • Chrome, Edge, FF and Opera are showing {}, but Safari is showing the expected result, as I said before in my comment. Therefore, this seems to be just a browser implementation difference. Commented Jul 18, 2019 at 4:31
  • 1
    @RobG Getters aren’t ignored by JSON.stringify. Commented Jul 18, 2019 at 4:40

3 Answers 3

1

It seems that the properties of error instances are inherited from Error.prototype and are not own properties, see ECMA-262 §19.5.4.

Safari has additional properties on the instance itself (line, column, sourceURL), however name and message are on the prototype.

You can convert it to JSON with your own function, e.g.

// Create plain object, copy error name and message properties.
// Copy own properties of error instance, then JSON.stringify it
function errToJSON(err) {
  let props = Object.assign({name:err.name, message:err.message}, err);
  return JSON.stringify(props);
}

try {
  var x = y;
} catch (e) {
  console.log('JSON.stringify(e):\n' + JSON.stringify(e));
  console.log('errToJSON(e):\n' + errToJSON(e));
  console.log('e.toString():\n' + e.toString());
}

Sign up to request clarification or add additional context in comments.

Comments

0

In this case err doesn't have any own enumerable properties. Only its prototype ReferenceError. You can inspect err by its inherited properties, like described in MDN

Comments

0
  • It seems that the properties of error instances are inherited from Error.prototype and are not own properties, see ECMA-262 §19.5.4.

  • JSON.stringify return {} because the err object is not pure object there is no key value paired. so it would return {}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.