Using: https://www.npmjs.com/package/snekfetch
I am trying to catch the following error: UnhandledPromiseRejectionWarning: Error: 404 Not Found
On the website it shows: {"error":"not-found"}
Code:
const promise = new Promise(function(resolve, reject) {
snekfetch.get('https://www.website.com/api/public/users?name=' + user).then(body => {
const json = JSON.parse(body.text);
const name = json.name;
console.log(json);
json ? reject(Error('It broke')) : resolve(name);
});
});
promise.then((result) => {
console.log(result);
console.log('works');
}).catch((error) => {
console.log(error);
console.log('does not work');
});
I tried to check if there is json: json ? reject(Error('It broke')) : resolve(name);
and the resolve works but I can not seem to catch the 404 Not Found error, how can I do this?