Skip to content

Instantly share code, notes, and snippets.

@indolering
Created September 19, 2017 08:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save indolering/6d97e9184f822969a5cbdf81944cf781 to your computer and use it in GitHub Desktop.
Save indolering/6d97e9184f822969a5cbdf81944cf781 to your computer and use it in GitHub Desktop.
Illustration of error handling in ES6
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Error Illustration</title>
<script defer>
'use strict';
class CustomError extends Error {}
class Throw {
static string(){
throw 'string';
}
static newError(){
throw new Error('new');
}
static bareError(){
throw Error('bare')
}
static customErrorNew(){
throw new CustomError('customErrorNew');
}
static customErrorBare(){
throw CustomError('customErrorBare');
}
}
Object.getOwnPropertyNames(Throw).filter(prop=>{
class GenericClass {}
return !Object.getOwnPropertyNames(GenericClass).includes(prop);
}).forEach(method=>{
try {
Throw[method]();
} catch (e) {
let name = `Throw.${method}`;
let func = Throw[method].toString().split('\n').slice(1, -1).join('').trim();
let title = name + " = " + func;
console.group(title);
if(e instanceof TypeError){
console.error('Unable to throw:', e);
} else {
let type = typeof e;
console.log(`type: ${type}`);
let isError = e instanceof Error;
console.log(`instance of Error: ${isError}`);
if(isError){
console.log('')
}
console.log(e);
}
console.log('');
console.groupEnd();
}
});
</script>
</head>
<body>
<h2>Check your browser console :)</h2>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment