Last active
June 12, 2018 16:58
-
-
Save wesleyduff/82be89c5e4c907ef4ca70c41db563200 to your computer and use it in GitHub Desktop.
Separate Error Handling from your code : Clean Code : Error Handling
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
class CustomException extends Error { | |
constructor(message, errorCode) { | |
super(message); | |
this.name = 'MyError'; | |
this.code = errorCode || 510; | |
} | |
} | |
class Logger { | |
constructor(error){ | |
this.error = error; | |
console.log(`${error.message}, : ${error.name}, : ${error.code}`) | |
} | |
} | |
class RunMethods { | |
static run(){ | |
try{ | |
throw new CustomException('this is my custom error', 520) | |
} catch(e){ | |
new Logger(e); | |
} | |
} | |
static runTwo(){ | |
try{ | |
throw new CustomException('this is my custom error part 2') | |
} catch(e){ | |
new Logger(e); | |
} | |
} | |
} | |
console.log('Run your code without having to manage error handling within your method calls.') | |
RunMethods.run(); | |
RunMethods.runTwo(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment