Skip to content

Commit

Permalink
Deprecates onError and removes references from README.
Browse files Browse the repository at this point in the history
  • Loading branch information
Rick Harrison committed Oct 9, 2016
1 parent 400318a commit bc2ab7f
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 15 deletions.
18 changes: 3 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,12 @@ $ npm start
### API

#### micro
**`micro(fn, { onError = null })`**
**`micro(fn)`**

- This function is exposed as the `default` export.
- Use `require('micro')`.
- Returns a [`http.Server`](https://nodejs.org/dist/latest-v4.x/docs/api/http.html#http_class_http_server) that uses the provided `fn` as the request handler.
- The supplied function is run with `await`. It can be `async`!
- The `onError` function is invoked with `req, res, err` if supplied (see [Error Handling](#error-handling))
- Example:

```js
Expand Down Expand Up @@ -166,7 +165,7 @@ $ npm start
**`sendError(req, res, error)`**

- Use `require('micro').sendError`.
- Used as the default handler for `onError`.
- Used as the default handler for errors thrown.
- Automatically sets the status code of the response based on `error.statusCode`.
- Sends the `error.message` as the body.
- During development (when `NODE_ENV` is set to `'development'`), stacks are printed out with `console.error` and also sent in responses.
Expand Down Expand Up @@ -234,18 +233,7 @@ If the error is based on another error that **Micro** caught, like a `JSON.parse

If a generic error is caught, the status will be set to `500`.

In order to set up your own error handling mechanism, you can pass a custom `onError` function to micro:

```js
const myErrorHandler = async (req, res, err) => {
// your own logging here
res.writeHead(500);
res.end('error!');
};
micro(handler, { onError: myErrorHandler });
```

**However**, generally you want to instead use simple composition:
In order to set up your own error handling mechanism, you can use composition in your handler:

```js
module.exports = handleErrors(async (req, res) => {
Expand Down
2 changes: 2 additions & 0 deletions bin/micro
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ if ('function' !== typeof mod) {

if ('function' !== typeof onError) {
onError = null
} else {
console.warn('[DEPRECATED] onError is deprecated and will be removed in a future release. Please use your own try/catch as needed.')
}

const { port, host } = args
Expand Down
4 changes: 4 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ exports.sendError = sendError
exports.createError = createError

function serve(fn, {onError = null} = {}) {
if (onError) {
console.warn('[DEPRECATED] onError is deprecated and will be removed in a future release. Please use your own try/catch as needed.')
}

return server((req, res) => {
run(req, res, fn, onError || sendError)
})
Expand Down

0 comments on commit bc2ab7f

Please sign in to comment.