Skip to content

Commit c6ed846

Browse files
committed
documentation updates
1 parent 85b18a7 commit c6ed846

File tree

1 file changed

+33
-3
lines changed

1 file changed

+33
-3
lines changed

README.md

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ Whatever you decide is best for your use case, **Lambda API** is there to suppor
6262
- [cookie()](#cookiename-value-options)
6363
- [cors()](#corsoptions)
6464
- [download()](#downloadfile--filename--options--callback)
65-
- [error()](#errormessage)
65+
- [error()](#errorcode-message-detail)
6666
- [etag()](#etagboolean)
6767
- [getHeader()](#getheaderkey)
6868
- [getLink()](#getlinks3path-expires-callback)
@@ -261,6 +261,36 @@ api.get('/users', (req,res) => {
261261

262262
**IMPORTANT:** You must either use a callback like `res.send()` **OR** `return` a value. Otherwise the execution will hang and no data will be sent to the user. Also, be sure not to return `undefined`, otherwise it will assume no response.
263263

264+
### A Note About Flow Control
265+
266+
While callbacks like `res.send()` and `res.error()` will trigger a response, they will not necessarily terminate execution of the current route function. Take a look at the following example:
267+
268+
```javascript
269+
api.get('/users', (req,res) => {
270+
271+
if (req.headers.test === 'test') {
272+
res.error('Throw an error')
273+
}
274+
275+
return { foo: 'bar' }
276+
})
277+
```
278+
279+
The example above would not have the intended result of displaying an error. `res.error()` would signal Lambda API to execute the error handling, but the function would continue to run. This would cause the function to `return` a response that would override the intended error. In this situation, you could either wrap the return in an `else` clause, or a cleaner approach would be to `return` the call to the `error()` method, like so:
280+
281+
```javascript
282+
api.get('/users', (req,res) => {
283+
284+
if (req.headers.test === 'test') {
285+
return res.error('Throw an error')
286+
}
287+
288+
return { foo: 'bar' }
289+
})
290+
```
291+
292+
`res.error()` does not have a return value (meaning it is `undefined`). However, the `return` tells the function to stop executing, and the call to `res.error()` handles and formats the appropriate response. This will allow Lambda API to properly return the expected results.
293+
264294
## Route Prefixing
265295

266296
Lambda API makes it easy to create multiple versions of the same api without changing routes by hand. The `register()` method allows you to load routes from an external file and prefix all of those routes using the `prefix` option. For example:
@@ -382,7 +412,7 @@ The `status` method allows you to set the status code that is returned to API Ga
382412

383413
```javascript
384414
api.get('/users', (req,res) => {
385-
res.status(401).error('Not Authorized')
415+
res.status(304).send('Not Modified')
386416
})
387417
```
388418

@@ -998,7 +1028,7 @@ api.use((req,res,next) => {
9981028
req.authorized = true
9991029
next() // continue execution
10001030
} else {
1001-
res.status(401).error('Not Authorized')
1031+
res.error(401,'Not Authorized')
10021032
}
10031033
})
10041034
```

0 commit comments

Comments
 (0)