You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/backend-web-development/express-error-handling.md
+12-12Lines changed: 12 additions & 12 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -40,7 +40,7 @@ Within an error handler, you typically need to do one of two things:
40
40
- call `next(err)` to pass the execution to the next error handler
41
41
42
42
```js
43
-
app.use(function (err, req, res, next) {
43
+
app.use((err, req, res, next) => {
44
44
res.status(500);
45
45
res.send(
46
46
`Error: ${err} </br>
@@ -59,19 +59,19 @@ We can define properties to the error object such as a `code` so that we can ret
59
59
app.get("/", (req, res) => {
60
60
// synchronous error
61
61
error =newError(" 😱! Error! Error!");
62
-
error.code=200;
62
+
error.statusCode=200;
63
63
throw error;
64
64
});
65
65
```
66
66
67
-
Return the correct status code in the response header if `err.code` is defined. Else we will default to the error code `500`.
67
+
Return the correct status code in the response header if `err.statusCode` is defined. Else we will default to the error code `500`.
68
68
69
69
```js
70
-
app.use(function(err, req, res, next) {
71
-
res.status(err.code||500);
70
+
app.use((err, req, res, next)=> {
71
+
res.status(err.statusCode||500);
72
72
res.send(
73
73
`Error: ${err} </br>
74
-
Error Status Code: ${err.code} <br>
74
+
Error Status Code: ${err.statusCode} <br>
75
75
Error stack: ${err.stack}`
76
76
);
77
77
});
@@ -84,7 +84,7 @@ Refer to the script: [Express.js playground: error_handler_example_3](https://gi
84
84
If you call an asynchronous API in a route handler and you would like to handle errors returned/thrown by those asynchronous operations, you just need to call `next(err)` when some error happens. That is, you call the `next` callback (which is an argument of every middleware and route handler) with an instance of _Error_.
85
85
86
86
```js
87
-
app.get("/", function(req, res, next) {
87
+
app.get("/", (req, res, next)=> {
88
88
// assume some asynchronous error happens because of an network issue
89
89
consterr=newError("Unexpected network error");
90
90
next(err);
@@ -94,15 +94,15 @@ app.get("/", function (req, res, next) {
94
94
Let's add a route handler that will not be called because it will be skipped when the error is thrown.
95
95
96
96
```js
97
-
app.get("/", function(req, res, next) {
97
+
app.get("/", (req, res, next)=> {
98
98
console.log("You should not see this line in the console 😡");
99
99
});
100
100
```
101
101
102
102
Add a custom error handler that will catch the error and then pass it on to the next error handler.
103
103
104
104
```js
105
-
app.use(function(err, req, res, next) {
105
+
app.use((err, req, res, next)=> {
106
106
if (err.message==="Unexpected network error") {
107
107
console.log("I don't know how to handle network error. Pass it on.");
Copy file name to clipboardExpand all lines: docs/backend-web-development/express-routers.md
+6Lines changed: 6 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -198,6 +198,12 @@ Now we can support both API versions!
198
198
What do you see when you go to http://localhost:3000/v1 on the browser?
199
199
How about http://localhost:3000/v2 ?
200
200
201
+
## How to really solve the versioning problem?
202
+
203
+
Should we stick to versioning like what we do in REST or change to an evolution style where we still try to support old endpoints as much as possible? There is no easy answer.
0 commit comments