Skip to content

Commit ddd0181

Browse files
Merge pull request ryanmcdermott#43 from jordalgo/jordalgo/errorHandling
Add: Error Handling Section
2 parents 2f1142f + 100628c commit ddd0181

File tree

1 file changed

+78
-4
lines changed

1 file changed

+78
-4
lines changed

README.md

+78-4
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@
88
5. [Classes](#classes)
99
6. [Testing](#testing)
1010
7. [Concurrency](#concurrency)
11-
8. [Formatting](#formatting)
12-
9. [Comments](#comments)
11+
8. [Error Handling](#error-handling)
12+
9. [Formatting](#formatting)
13+
10. [Comments](#comments)
1314

1415
## Introduction
1516
![Humorous image of software quality estimation as a count of how many expletives
@@ -379,7 +380,7 @@ function parseBetterJSAlternative(code) {
379380
let ast = lexer(tokens);
380381
ast.forEach((node) => {
381382
// parse...
382-
})
383+
})
383384
}
384385
```
385386
**[⬆ back to top](#table-of-contents)**
@@ -1318,7 +1319,7 @@ class DOMTraverser {
13181319

13191320
let $ = new DOMTraverser({
13201321
rootNode: document.getElementsByTagName('body'),
1321-
options: {
1322+
options: {
13221323
animationModule: function() {}
13231324
}
13241325
});
@@ -1793,6 +1794,79 @@ async function getCleanCodeArticle() {
17931794
**[⬆ back to top](#table-of-contents)**
17941795

17951796

1797+
## **Error Handling**
1798+
Thrown errors are a good thing! They mean the runtime has successfully
1799+
identified when something in your program has gone wrong and it's letting
1800+
you know by stopping function execution on the current stack, killing the
1801+
process (in Node), and notifying you in the console with a stack trace.
1802+
1803+
### Don't ignore caught errors
1804+
Doing nothing with a caught error doesn't give you the ability to ever fix
1805+
or react to said error. Logging the error to the console (`console.log`)
1806+
isn't much better as often times it can get lost in a sea of things printed
1807+
to the console. If you wrap any bit of code in a `try/catch` it means you
1808+
think an error may occur there and therefore you should have a plan,
1809+
or create a code path, for when it occurs.
1810+
1811+
**Bad:**
1812+
```javascript
1813+
try {
1814+
functionThatMightThrow();
1815+
} catch (error) {
1816+
console.log(error);
1817+
}
1818+
```
1819+
1820+
**Good:**
1821+
```javascript
1822+
try {
1823+
functionThatMightThrow();
1824+
} catch (error) {
1825+
// One option (more noisy than console.log):
1826+
console.error(error);
1827+
// Another option:
1828+
notifyUserOfError(error);
1829+
// Another option:
1830+
reportErrorToService(error);
1831+
// OR do all three!
1832+
}
1833+
```
1834+
1835+
### Don't ignore rejected promises
1836+
For the same reason you shouldn't ignore caught errors
1837+
from `try/catch`.
1838+
1839+
**Bad:**
1840+
```javascript
1841+
getdata()
1842+
.then(data => {
1843+
functionThatMightThrow(data);
1844+
})
1845+
.catch(error => {
1846+
console.log(error);
1847+
});
1848+
```
1849+
1850+
**Good:**
1851+
```javascript
1852+
getdata()
1853+
.then(data => {
1854+
functionThatMightThrow(data);
1855+
})
1856+
.catch(error => {
1857+
// One option (more noisy than console.log):
1858+
console.error(error);
1859+
// Another option:
1860+
notifyUserOfError(error);
1861+
// Another option:
1862+
reportErrorToService(error);
1863+
// OR do all three!
1864+
});
1865+
```
1866+
1867+
**[⬆ back to top](#table-of-contents)**
1868+
1869+
17961870
## **Formatting**
17971871
Formatting is subjective. Like many rules herein, there is no hard and fast
17981872
rule that you must follow. The main point is DO NOT ARGUE over formatting.

0 commit comments

Comments
 (0)