Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: GH-1438, error reponse customization documentation incorrect #1439

Merged
merged 2 commits into from Aug 11, 2017
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 17 additions & 4 deletions docs/api/server.md
Expand Up @@ -134,16 +134,29 @@ server.on('InternalServer', function(req, res, err, callback) {
```

Inside the error event listener, it is also possible to change the payload
if so desired. To do so, simply set your custom response on the `body` property
of the error. For example, it is common to send a custom 500 response:
if so desired. To do so, simply implement a custom `toString()` or `toJSON()`.
Depending on the content-type and formatter being used for the response, one
of the two serializers will be used. For example, given the folllwing example:

```js
server.on('InternalServer', function(req, res, err, callback) {
err.body = 'Sorry, an error occurred!';
server.on('restifyError', function(req, res, err, callback) {
err.toJSON = function customToJSON() {
return {
name: err.name,
message: err.message
};
};
err.toString = function customToString() {
return 'i just want a string';
};
return callback();
});
```

A request with an `accept: application/json` will trigger the `toJSON()`
serializer, while a request with `accept: text/plain` will trigger the
`toString()` serializer.

Note that the signature is identical for all error events emitted. The listener
is invoked with the following signature:

Expand Down