gpt4 book ai didi

javascript - 如何实现req/res拦截器

转载 作者:行者123 更新时间:2023-12-03 05:54:53 26 4
gpt4 key购买 nike

我尝试使用feathers-client 实现请求/响应拦截器。

目的是向请求添加全局元数据并剥离响应正文。另外我想使用响应拦截器来实现全局错误处理程序。

我查看了钩子(Hook),但如果发生任何错误,after* 钩子(Hook)似乎不会被执行。

feathersclient()
...
.configure(function() {
const app = this;
app.mixins.push(function(service) {
service.before(function(hook) {
console.log('SENT', service.path, hook);
return hook;
});
service.after(function(hook) {
// Never fired if req produces an error
console.log('RECEIVE', service.path, hook);
return hook;
});
});
})

最佳答案

你是对的,不幸的是没有一个很好的方法来 Hook 发生的错误。不过,feathers-hooks v1.6.0 将支持 onError 处理程序。在那之前,您可以使用自己的错误处理程序创建一个服务混合,如下所示:

feathersclient()
...
.configure(function() {
const app = this;
app.mixins.push(function(service) {
const mixin = {};

app.methods.forEach(method => {
if(typeof service[method] === 'function') {
mixin[method] = function(... args) {
return this._super(... args).catch(error => {
// do some error handling here
throw error;
});
}
}
});

service.mixin(mixin);
});
})

关于javascript - 如何实现req/res拦截器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39976173/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com