https://github.com/rickharrison/micro-json-error
Outputs errors from your micro service in JSON.
https://github.com/rickharrison/micro-json-error
Last synced: 12 months ago
JSON representation
Outputs errors from your micro service in JSON.
- Host: GitHub
- URL: https://github.com/rickharrison/micro-json-error
- Owner: rickharrison
- License: mit
- Created: 2016-10-08T22:51:31.000Z (almost 10 years ago)
- Default Branch: master
- Last Pushed: 2016-10-09T00:23:58.000Z (almost 10 years ago)
- Last Synced: 2025-02-04T03:52:09.774Z (over 1 year ago)
- Language: JavaScript
- Size: 6.84 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# micro-json-error
Wraps your [micro](https://github.com/zeit/micro) service function to catch errors and display them as JSON.
### Installation
```
npm install micro-json-error --save
```
### Usage
```javascript
const { createError } = require('micro');
const jsonError = require('micro-json-error');
module.exports = jsonError(async function (req, res) {
throw createError(400, 'Invalid data');
});
```
### API
```
jsonError(fn, { httpStatusCode = null, onError = null });
```
- `httpStatusCode` - Overrides the `statusCode` from the error for the http response status code. Useful if you always want to return a 200, and show the original `statusCode` in the JSON body.
- `onError` - Callback function that will be passed the `error`. Useful for logging.