@feathersjs/errors
Advanced tools
Comparing version
# Change Log | ||
## [v3.0.0](https://github.com/feathersjs/errors/tree/v3.0.0) (2017-11-01) | ||
[Full Changelog](https://github.com/feathersjs/errors/compare/v3.0.0-pre.1...v3.0.0) | ||
**Merged pull requests:** | ||
- Update to Buzzard infrastructure [\#93](https://github.com/feathersjs/errors/pull/93) ([daffl](https://github.com/daffl)) | ||
## [v3.0.0-pre.1](https://github.com/feathersjs/errors/tree/v3.0.0-pre.1) (2017-10-21) | ||
@@ -4,0 +11,0 @@ [Full Changelog](https://github.com/feathersjs/errors/compare/v2.9.2...v3.0.0-pre.1) |
@@ -7,3 +7,3 @@ const path = require('path'); | ||
}; | ||
const defaultError = path.resolve(defaults.public, 'default.html'); | ||
const defaultHtmlError = path.resolve(defaults.public, 'default.html'); | ||
@@ -17,6 +17,10 @@ module.exports = function (options = {}) { | ||
404: path.resolve(options.public, '404.html'), | ||
default: defaultError | ||
default: defaultHtmlError | ||
}; | ||
} | ||
if (typeof options.json === 'undefined') { | ||
options.json = {}; | ||
} | ||
return function (error, req, res, next) { | ||
@@ -37,37 +41,46 @@ if (error.type !== 'FeathersError') { | ||
// If the developer passed a custom function | ||
// If the developer passed a custom function for ALL html errors | ||
if (typeof options.html === 'function') { | ||
formatter['text/html'] = options.html; | ||
} else { | ||
formatter['text/html'] = function () { | ||
let file = options.html[error.code]; | ||
if (!file) { | ||
file = options.html.default || defaultError; | ||
} | ||
res.set('Content-Type', 'text/html'); | ||
res.sendFile(file); | ||
}; | ||
let file = options.html[error.code]; | ||
if (!file) { | ||
file = options.html.default || defaultHtmlError; | ||
} | ||
// If the developer passed a custom function for individual html errors | ||
if (typeof file === 'function') { | ||
formatter['text/html'] = file; | ||
} else { | ||
formatter['text/html'] = function () { | ||
res.set('Content-Type', 'text/html'); | ||
res.sendFile(file); | ||
}; | ||
} | ||
} | ||
// If the developer passed a custom function | ||
// If the developer passed a custom function for ALL json errors | ||
if (typeof options.json === 'function') { | ||
formatter['application/json'] = options.json; | ||
} else { | ||
// Don't show stack trace if it is a 404 error | ||
if (error.code === 404) { | ||
error.stack = null; | ||
} | ||
let handler = options.json[error.code] || options.json.default; | ||
// If the developer passed a custom function for individual json errors | ||
if (typeof handler === 'function') { | ||
formatter['application/json'] = handler; | ||
} else { | ||
// Don't show stack trace if it is a 404 error | ||
if (error.code === 404) { | ||
error.stack = null; | ||
} | ||
formatter['application/json'] = function () { | ||
let output = Object.assign({}, error.toJSON()); | ||
formatter['application/json'] = function () { | ||
let output = Object.assign({}, error.toJSON()); | ||
if (process.env.NODE_ENV === 'production') { | ||
delete output.stack; | ||
} | ||
if (process.env.NODE_ENV === 'production') { | ||
delete output.stack; | ||
} | ||
res.set('Content-Type', 'application/json'); | ||
res.json(output); | ||
}; | ||
res.set('Content-Type', 'application/json'); | ||
res.json(output); | ||
}; | ||
} | ||
} | ||
@@ -74,0 +87,0 @@ |
{ | ||
"name": "@feathersjs/errors", | ||
"description": "Common error types for feathers apps", | ||
"version": "3.0.0", | ||
"version": "3.1.0", | ||
"homepage": "https://github.com/feathersjs/errors", | ||
@@ -6,0 +6,0 @@ "main": "lib/index", |
@@ -45,4 +45,6 @@ # @feathersjs/errors | ||
#### Usage: | ||
### Usage: | ||
#### Error objects: | ||
```js | ||
@@ -70,2 +72,44 @@ import errors from '@feathersjs/errors'; | ||
#### Error handler: | ||
This plugin will handle all error responses for you automatically. In the case of HTML errors, basic error pages are provided without needing to do anything. In the case of JSON, the error is normalized and serialized as JSON so that your client code will receive a predictable error object every time! | ||
However, you can customize how errors are sent to the user on a per error code basis. This allows you to easily customize the user experience for different types of errors. If a particular configuration is omitted, it falls back to the default behavior described above. | ||
```js | ||
import feathers from 'feathers'; | ||
import errorHandler from 'feathers-errors/handler'; | ||
const app = feathers(); | ||
// full app configuration omitted for brevity | ||
// configure the error handler last | ||
app.configure(errorHandler({ | ||
html: { | ||
// strings should point to html files | ||
404: 'path/to/custom-404.html', | ||
// functions are treated as middleware | ||
406: (err, req, res, next) => { | ||
// handle the error yourself | ||
res.send(...); | ||
}, | ||
// Optionally configure your own default behavior. | ||
default: 'path/to/generic/error-page.html' | ||
}, | ||
json: { | ||
404: (err, req, res, next) => { | ||
// make sure to strip off the stack trace in production | ||
if (process.env.NODE_ENV === 'production') { | ||
delete err.stack; | ||
} | ||
res.json({ message: 'Not found' }); | ||
}, | ||
default: (err, req, res, next) => { | ||
// handle all other errors | ||
res.json({ message: 'Oh no! Something went wrong' }); | ||
} | ||
} | ||
}); | ||
``` | ||
## License | ||
@@ -72,0 +116,0 @@ |
68091
3.69%288
5.11%118
59.46%