Skip to content

Instantly share code, notes, and snippets.

@prof3ssorSt3v3
Last active November 17, 2022 18:37
Embed
What would you like to do?
Sample network error script
let responseObj = new Response('http://example.com/', { status: 200, statusText: 'All good' });
//Network errors
class NetworkError extends Error {
constructor(msg, response) {
super(msg);
this.name = 'NetworkError';
this.response = response;
this.status = response.status;
this.text = response.statusText;
}
}
let ne = new NetworkError('some message', responseObj);
console.log({ ne });
throw ne;
/*
Alternate non-class syntax version
const NetworkError2 = function (msg, response) {
this.message = msg;
this.response = response;
this.status = response.status;
this.text = response.statusText;
};
Object.setPrototypeOf(NetworkError2.prototype, Error.prototype);
//or NetworkError2.prototype.__proto__ = Error.prototype
let ne2 = new NetworkError2('some message', responseObj);
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment