Reputation: 7397
I'm trying to define my own error format to be returned in the response. I have written my own middleware:
function catchAndLogErrors(app) {
return function (err, req, res, next) {
// stuff happening...
throw new Error(err.message, {name: err.name, code: err.code, className: err.className, info: err.info, data: err.data});
};
}
and then in /src/middleware/index.js
I've commented out the handler
and put my own middleware:
const handler = require('feathers-errors/handler');
const catchAndLogErrors = require('my-middleware');
...
app.use(catchAndLogErrors(app));
// app.use(handler());
but I get the error returned as HTML and it's actually only the message and the stacktrace, but none of the other properties.
Any ideas?
Upvotes: 0
Views: 835
Reputation: 44215
Since you commented out the Feathers error handler you will have to implement your own Express error handler where you format the error and send the response (instead of just throwing it) similar to this (if you want to send JSON):
function catchAndLogErrors(app) {
return function (err, req, res, next) {
res.json(err);
};
}
The real Feathers way for modifying errors of service method calls are error hooks. They allow you to modify hook.error
to the response you need, e.g. with an application wide hook that applies to all service calls:
app.hooks({
error(hook) {
const err = hook.error;
//change `hook.error` to the modified error
hook.error = new Error(err.message, {
name: err.name,
code: err.code,
className: err.className,
info: err.info,
data: err.dat
});
}
});
Upvotes: 1