Skip to content

Commit 666c355

Browse files
[mabel] Edit error handling, joi, routers for express.js
1 parent eaa8758 commit 666c355

File tree

4 files changed

+22
-28
lines changed

4 files changed

+22
-28
lines changed

docs/backend-web-development/express-error-handling.md

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ Within an error handler, you typically need to do one of two things:
4040
- call `next(err)` to pass the execution to the next error handler
4141

4242
```js
43-
app.use(function (err, req, res, next) {
43+
app.use((err, req, res, next) => {
4444
res.status(500);
4545
res.send(
4646
`Error: ${err} </br>
@@ -59,19 +59,19 @@ We can define properties to the error object such as a `code` so that we can ret
5959
app.get("/", (req, res) => {
6060
// synchronous error
6161
error = new Error(" 😱! Error! Error!");
62-
error.code = 200;
62+
error.statusCode = 200;
6363
throw error;
6464
});
6565
```
6666

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`.
6868

6969
```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);
7272
res.send(
7373
`Error: ${err} </br>
74-
Error Status Code: ${err.code} <br>
74+
Error Status Code: ${err.statusCode} <br>
7575
Error stack: ${err.stack}`
7676
);
7777
});
@@ -84,7 +84,7 @@ Refer to the script: [Express.js playground: error_handler_example_3](https://gi
8484
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_.
8585

8686
```js
87-
app.get("/", function (req, res, next) {
87+
app.get("/", (req, res, next) => {
8888
// assume some asynchronous error happens because of an network issue
8989
const err = new Error("Unexpected network error");
9090
next(err);
@@ -94,15 +94,15 @@ app.get("/", function (req, res, next) {
9494
Let's add a route handler that will not be called because it will be skipped when the error is thrown.
9595

9696
```js
97-
app.get("/", function (req, res, next) {
97+
app.get("/", (req, res, next) => {
9898
console.log("You should not see this line in the console 😡");
9999
});
100100
```
101101

102102
Add a custom error handler that will catch the error and then pass it on to the next error handler.
103103

104104
```js
105-
app.use(function (err, req, res, next) {
105+
app.use((err, req, res, next) => {
106106
if (err.message === "Unexpected network error") {
107107
console.log("I don't know how to handle network error. Pass it on.");
108108
next(err);
@@ -116,9 +116,9 @@ app.use(function (err, req, res, next) {
116116
Add a final custom error handler that will return a response to the client.
117117

118118
```js
119-
app.use(function (err, req, res, next) {
120-
res.status(500);
121-
res.send({ error: "internal server error" });
119+
app.use((err, req, res, next) => {
120+
err.statusCode = err.statusCode || 500;
121+
res.status(err.statusCode).send(err.message);
122122
});
123123
```
124124

docs/backend-web-development/express-joi-validation.md

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,7 @@ const schema = Joi.object({
8484
password: Joi.string().pattern(new RegExp("^[a-zA-Z0-9]{3,30}$")),
8585
repeat_password: Joi.ref("password"), // must equal to password
8686

87-
birthyear: Joi.number()
88-
.integer()
89-
.min(1900)
90-
.max(2013), // an integer between 1900 and 2013
87+
birthyear: Joi.number().integer().min(1900).max(2013), // an integer between 1900 and 2013
9188

9289
//email a valid email address string
9390
//must have two domain parts e.g. example.com
@@ -132,12 +129,8 @@ We could put the schema in a function that will return the result object.
132129
function validateSong(song) {
133130
const schema = Joi.object({
134131
id: Joi.number().integer(),
135-
name: Joi.string()
136-
.min(3)
137-
.required(),
138-
artist: Joi.string()
139-
.min(3)
140-
.required(),
132+
name: Joi.string().min(3).required(),
133+
artist: Joi.string().min(3).required(),
141134
});
142135
return schema.validate(song);
143136
}
@@ -151,6 +144,6 @@ if (validation.error) {
151144
const error = new Error(validation.error.details[0].message);
152145
// 400 Bad Request
153146
error.statusCode = 400;
154-
return next(error);
147+
next(error);
155148
}
156149
```

docs/backend-web-development/express-lab.md

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -139,10 +139,6 @@ Implement tests with Jest and Supertest (include testing for errors)
139139

140140
Integrate Express.js routers to organise your "who is next" routes (put into routes folder)
141141

142-
## Refactoring to controllers folder
143-
144-
Lab: refactoring of routes
145-
146142
## Error
147143

148144
Integrate a default error handler middleware for your "who is next" routes
@@ -156,6 +152,5 @@ Integrate Joi validation library to validate data
156152
- package.json
157153
- package-lock.json
158154
- src
159-
- controllers
160155
- routes
161156
- \_\_tests\_\_

docs/backend-web-development/express-routers.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,12 @@ Now we can support both API versions!
198198
What do you see when you go to http://localhost:3000/v1 on the browser?
199199
How about http://localhost:3000/v2 ?
200200

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.
204+
205+
https://github.com/graphql/graphql-spec/issues/134
206+
201207
## Exercises
202208

203209
Now that we have both movies and songs routes on the same App.js file, we should split them into a movies router and songs router.

0 commit comments

Comments
 (0)