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