This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
process | |
.on('unhandledRejection', (reason, promise) => { | |
// Handle failed Promise | |
}) | |
.on('uncaughtException', err => { | |
// Handle failed Error | |
process.exit(1); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
window.addEventListener('error', e => { | |
// Get the error properties from the error event object | |
const { message, filename, lineno, colno, error } = e; | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var oldOnErrorHandler = window.onerror; | |
window.onerror = (msg, url, line, column, err) => { | |
If (oldOnErrorHandler) { | |
// Call any previously assigned handler. | |
oldOnErrorHandler.apply(this, arguments); | |
} | |
// The rest of your code | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
window.onerror = (msg, url, line, column, err) => { | |
// ... handle error … | |
return false; | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function asyncFoo(x, callback) { | |
// Some async code... | |
} | |
asyncFoo(‘testParam’, (err, result) => { | |
If (err) { | |
// Handle error. | |
} | |
// Do some other work. | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function foo(x) { | |
return new Promise((resolve, reject) => { | |
if (typeof x !== 'number') { | |
reject('x is not a number'); | |
} | |
resolve(x); | |
}); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
try { | |
foo(‘test’) | |
.then(x => console.log(x)) | |
.catch(err => console.log(err)); // The error is handled here. | |
} catch(err) { | |
// This block is not reached since the thrown error is inside of a Promise. | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function foo(x) { | |
return new Promise((resolve, reject) => { | |
if (typeof x !== 'number') { | |
throw new TypeError('x is not a number'); | |
} | |
resolve(x); | |
}); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
try { | |
foo(‘test’) | |
.then(x => console.log(x)) | |
.catch(err => console.log(err)); | |
} catch(err) { | |
// Now the error is handed | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
foo(‘test’) | |
.then(x => console.log(x)) | |
.catch(err => console.log(err)); |