Comparing version 6.1.0 to 6.1.1
@@ -11,4 +11,12 @@ # appa change log | ||
## 6.1.0 - 2016-02-19 | ||
## 6.1.1 - 2017-02-25 | ||
## Fixed | ||
- uncaught errors inside request handlers are now caught and a `500 Internal server error` response is sent | ||
- Added docs to readme about error handling and logging | ||
- update tests/README.md | ||
## 6.1.0 - 2017-02-19 | ||
## Added | ||
@@ -15,0 +23,0 @@ - disable logging with `var app = appa({ logging: false })` |
12
index.js
@@ -96,2 +96,10 @@ var assert = require('assert') | ||
function respond (req, res, ctx) { | ||
try { | ||
return callback(req, res, ctx) | ||
} catch (e) { | ||
return error(500, 'Internal server error', e).pipe(res) | ||
} | ||
} | ||
if (req.method === 'POST' || req.method === 'PUT' || req.method === 'PATCH') { | ||
@@ -107,7 +115,7 @@ body(req, options, function (err, result) { | ||
return callback(req, res, ctx) | ||
return respond(req, res, ctx) | ||
}) | ||
} | ||
return callback(req, res, ctx) | ||
return respond(req, res, ctx) | ||
}) | ||
@@ -114,0 +122,0 @@ } |
{ | ||
"name": "appa", | ||
"version": "6.1.0", | ||
"version": "6.1.1", | ||
"description": "Quickly create simple JSON API services.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -21,6 +21,2 @@ # appa | ||
## About | ||
## Install | ||
@@ -51,5 +47,37 @@ | ||
## Error handling | ||
Any uncaught errors that occur in a request handler will be caught and a `500 Internal server error` response will be sent. | ||
Send error responses using the `appa/error` module: | ||
```js | ||
var error = require('appa/error') | ||
module.exports = function (req, res, ctx) { | ||
return error(404, 'Not found').pipe(res) | ||
} | ||
``` | ||
Sending an error response does not automatically log the error, so to add that you can do something like: | ||
```js | ||
var error = require('appa/error') | ||
var log = require('appa/log')() | ||
module.exports = function (req, res, ctx) { | ||
log.error(req.method, '500', errorStack) | ||
return error(500, 'Internal server error').pipe(res) | ||
} | ||
``` | ||
## Logging | ||
appa uses [pino](https://npmjs.com/pino) for logging. Pass options to pino with `options.log`: `appa({ log: pinoOptions })`. | ||
See [example pino usage](https://github.com/pinojs/pino#usage) and [all pino options](https://github.com/pinojs/pino/blob/master/docs/API.md#pinooptions-stream). | ||
Or disable logging completely by setting `options.log` to `false`: `appa({ log: false })`. | ||
## Documentation | ||
- [Getting started](docs/getting-started.md) | ||
- [Related modules](docs/related-modules.md) | ||
- [API](docs/api.md) | ||
@@ -56,0 +84,0 @@ - [Tests](tests/) |
@@ -105,1 +105,20 @@ var test = require('tape') | ||
}) | ||
test('handle internal errors', function (t) { | ||
t.plan(4) | ||
var app = createApp({ log: false }) | ||
app.on('/error', function (req, res, ctx) { | ||
throw new Error('oops') | ||
}) | ||
var server = createServer(app).listen(3131, function () { | ||
request({ url: 'http://127.0.0.1:3131/error', json: true }, function (err, res, body) { | ||
t.notOk(err) | ||
t.equal(res.statusCode, 500) | ||
t.equal(body.statusCode, 500) | ||
t.equal(body.message, 'Internal server error') | ||
server.close() | ||
}) | ||
}) | ||
}) |
@@ -18,3 +18,3 @@ # Tests | ||
### Just run the linter | ||
### Only run the linter | ||
@@ -28,3 +28,3 @@ ```sh | ||
```sh | ||
npm run test:no-lint | ||
npm run test:node | ||
``` |
Deprecated
MaintenanceThe maintainer of the package marked it as deprecated. This could indicate that a single version should not be used, or that the package is no longer maintained and any new vulnerabilities will not be fixed.
Found 1 instance in 1 package
61025
352
109
1