Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 63ca186

Browse files
author
Nils Schönwald
committed
Merge branch 'master' of github.com:ryanmcdermott/clean-code-javascript
* 'master' of github.com:ryanmcdermott/clean-code-javascript: Add: Error Handling Section Correct spelling of Cessna airplane Adjusted the commentthat show the result
2 parents 2200e40 + ddd0181 commit 63ca186

File tree

1 file changed

+79
-5
lines changed

1 file changed

+79
-5
lines changed

README.md

Lines changed: 79 additions & 5 deletions
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
@@ -507,7 +508,7 @@ function createMenu(config) {
507508
cancellable: true
508509
}, config);
509510

510-
// config now equals: {title: "Order", body: "Bar", buttonText: "Send", cancellable: true}
511+
// config now equals: {title: "Foo", body: "Bar", buttonText: "Baz", cancellable: true}
511512
// ...
512513
}
513514

@@ -766,7 +767,7 @@ class Airplane {
766767
return getMaxAltitude() - getPassengerCount();
767768
case 'Air Force One':
768769
return getMaxAltitude();
769-
case 'Cesna':
770+
case 'Cessna':
770771
return getMaxAltitude() - getFuelExpenditure();
771772
}
772773
}
@@ -793,7 +794,7 @@ class AirForceOne extends Airplane {
793794
}
794795
}
795796

796-
class Cesna extends Airplane {
797+
class Cessna extends Airplane {
797798
//...
798799
getCruisingAltitude() {
799800
return getMaxAltitude() - getFuelExpenditure();
@@ -1792,6 +1793,79 @@ async function getCleanCodeArticle() {
17921793
**[⬆ back to top](#table-of-contents)**
17931794
17941795
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+
17951869
## **Formatting**
17961870
Formatting is subjective. Like many rules herein, there is no hard and fast
17971871
rule that you must follow. The main point is DO NOT ARGUE over formatting.

0 commit comments

Comments
 (0)