比方说,如果我在调用服务时抛出GeneralError,如何使error对象像我想要的那样:
{
"status": "Failed",
"code": "500_1",
"detail": "Something is wrong with your API"
} 我已经尝试在错误的钩子上添加这个
hook => {
hook.error = {
"status": "Failed",
"code": "500_1",
"detail": "Something is wrong with your API"
}
return hook
}但是仍然不能,并且仍然返回羽毛的默认错误对象:
{
"name": "GeneralError",
"message": "Error",
"code": 500,
"className": "general-error",
"data": {},
"errors": {}
}发布于 2020-10-06 22:46:29
您可以创建自己的custom error。
示例:
const { FeathersError } = require('@feathersjs/errors');
class UnsupportedMediaType extends FeathersError {
constructor(message, data) {
super(message, 'unsupported-media-type', 415, 'UnsupportedMediaType', data);
}
}
const error = new UnsupportedMediaType('Not supported');
console.log(error.toJSON());发布于 2019-11-03 02:29:00
所有的羽化错误都可以与自定义消息一起提供,该消息覆盖有效负载中的默认消息:
throw new GeneralError('The server is sleeping. Come back later.');您还可以传递额外的数据和/或错误。所有这些都有文档记录:https://docs.feathersjs.com/api/errors.html
发布于 2020-12-15 19:44:55
根据上面的@daff注释,这是定制返回的错误对象的方式。这里包括扩展内置错误和自定义错误
custom-errors.js
const { FeathersError } = require('@feathersjs/errors');
class CustomError extends FeathersError {
constructor(message, name, code) {
super(message, name, code);
}
toJSON() {
return {
status: "Failed",
code: this.code,
detail: this.message,
}
}
}
class BadRequest extends CustomError {
constructor(message) {
super(message, 'bad-request', 400);
}
}
class NotAuthenticated extends CustomError {
constructor(message) {
super(message, 'not-authenticated', 401);
}
}
class MyCustomError extends CustomError {
constructor(message) {
super(message, 'my-custom-error', 500_1);
}
}抛出错误,如
throw new MyCustomError('Something is wrong with your API');输出(以Postman为单位)
{
"status": "Failed",
"code": 500_1
"detail": "Something is wrong with your API",
code
}https://stackoverflow.com/questions/58668554
复制相似问题