0

This code is using axios and its error object, but is wrong because the key not exist,

const axios = require('axios');
axios.get('http://w3c.org/error_here').then(response => {
    console.log(response.data);
  }).catch(error => {
    console.log(error.Error) // not exist
  });

This other code is working fine, but there are no clues about how to solve the problem to output only the error message (supposed before error.Error).

const axios = require('axios');

axios.get('http://w3c.org/error_here')
  .then(response => {
    console.log(response.data);
  }).catch(error => {
    delete error.request
    delete error.config
    delete error.response
    console.log(error)  // a string here!
    console.log(JSON.stringify(error))  // where??
  });

The first console.log() output is a string

   Error: Request failed with status code 404 ...

the second is an empty object,

    {}

So, where the string (or the object)? How to do something as console.log(error.Error) to show only the error message?

Peter Krauss
  • 13,174
  • 24
  • 167
  • 304
  • what exactly are you looking for, not very clear with your question? – 1565986223 Apr 13 '19 at 13:41
  • Thanks @naga-elixir-jar, I edited, check if it make sense now – Peter Krauss Apr 13 '19 at 13:50
  • `Request failed with status code 404` this is message and its availabe as `error.message` – 1565986223 Apr 13 '19 at 13:55
  • If you want to see the full error object check https://stackoverflow.com/questions/10729276/how-can-i-get-the-full-object-in-node-jss-console-log-rather-than-object `console.log(JSON.stringify(error))` will throw an error reason being `error` object is circular – 1565986223 Apr 13 '19 at 13:58
  • @naga-elixir-jar, YES (!), `console.log(error.message)` is working! Well, the answer of the question must be also theoretic , "why `JSON.stringify(error))` was empty? – Peter Krauss Apr 13 '19 at 13:59
  • on futher testing looks like the `error.message` is part of the stack trace, so somewhere on the `prototype` chain (set `hidden: true` in the link above) – 1565986223 Apr 13 '19 at 14:07
  • @naga-elixir-jar, hum... 1. when run `node myScript.js > out.txt` there are no STDERR message, so it is not a confusion; 2. adding `util.inspect(error, {showHidden: false})` I not see the key "message", so no clues about the existence of a "hidden key" in the object, very very strange, seems a **bug** in the NodeJS compiler or axios lib. – Peter Krauss Apr 13 '19 at 14:17
  • set `showHidden` to `true` and you'll see the object. – 1565986223 Apr 13 '19 at 14:25
  • @naga-elixir-jar oops, but no changes, I tested both, same obscure text, no clues about "message" key. – Peter Krauss Apr 13 '19 at 14:36

1 Answers1

1

Collecting the comments into an answer:

When you used delete, you deleted all the enumerable members of the object:

delete error.request
delete error.config
delete error.response
console.log(Object.keys(error)); // Object.keys gets all non-enumerable properties' key of the object into an array
// outputs
[ 'config', 'request', 'response' ]

message is a non-enumerable property

console.log(error.propertyIsEnumerable('message'));
// outputs
false

Finally, JSON.stringify(error) is empty because stringify does not serialize non-enumerable

More about enumerable on MDN and SO


How to list the non-enumerable properties of the error object? That is, how to discover that a key named "message" exist? List all with:

console.log(Object.getOwnPropertyNames(error));

After deletes, what's left is [ 'stack', 'message' ].

Peter Krauss
  • 13,174
  • 24
  • 167
  • 304
1565986223
  • 6,420
  • 2
  • 20
  • 33