|
8 | 8 | 5. [Classes](#classes)
|
9 | 9 | 6. [Testing](#testing)
|
10 | 10 | 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) |
13 | 14 |
|
14 | 15 | ## Introduction
|
15 | 16 | **
|
@@ -1318,7 +1319,7 @@ class DOMTraverser {
|
1318 | 1319 |
|
1319 | 1320 | let $ = new DOMTraverser({
|
1320 | 1321 | rootNode: document.getElementsByTagName('body'),
|
1321 |
| - options: { |
| 1322 | + options: { |
1322 | 1323 | animationModule: function() {}
|
1323 | 1324 | }
|
1324 | 1325 | });
|
@@ -1793,6 +1794,79 @@ async function getCleanCodeArticle() {
|
1793 | 1794 | **[⬆ back to top](#table-of-contents)**
|
1794 | 1795 |
|
1795 | 1796 |
|
| 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 | + |
1796 | 1870 | ## **Formatting**
|
1797 | 1871 | Formatting is subjective. Like many rules herein, there is no hard and fast
|
1798 | 1872 | rule that you must follow. The main point is DO NOT ARGUE over formatting.
|
|
0 commit comments