Created
September 19, 2017 08:14
-
-
Save indolering/6d97e9184f822969a5cbdf81944cf781 to your computer and use it in GitHub Desktop.
Illustration of error handling in ES6
This file contains 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
<!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