我在后端使用带有socketio的羽毛。客户端正在监听,一切正常。
我想处理“服务器无响应”错误,但是我找不到在哪里可以做到这一点?
服务器抛出的错误:
"Unhandled promise rejection
Object { type: "FeathersError", name: "Timeout", message: "Timeout of 5000ms exceeded calling find on newsfeed", code: 408, className: "timeout", data: {…}, errors: {}, stack: "FeathersError@webpack-internal:///./node_modules/@feathersjs/errors/lib/index.js:58:19\nTimeout@webpack-internal:///./node_modules/@feathersjs/errors/lib/index.js:135:3\nsend/</timeoutId<@webpack-internal:///./node_modules/@feathersjs/transport-commons/lib/client.js:66:9\n" }"
这是正确的,没有处理“承诺”!我在哪里处理?
我试图在每一行上添加catch只是为了查看什么有效,但没有成功:
import feathers from '@feathersjs/feathers'
import socketio from '@feathersjs/socketio-client'
import io from 'socket.io-client'
const socket = io('http://localhost:3030/', {transports: ['websocket']});
socket.on("connect_failed", er=>console.error('Error connecting to server: ', er));
const restApi = feathers()
.configure(socketio(socket));
try {
restApi.service('/newsfeed');
restApi.on("connect_failed", er=>console.error('Error connecting to server: ', er));
}
catch (er) {
console.error('Error connecting to server: ', er)
}
export default restApi
答案 0 :(得分:0)
超时错误来自特定请求,因此无论您在哪里发出请求,都必须处理该错误。
restApi.service('/newsfeed').find()
如果您使用的是异步/等待,则可以将服务呼叫包装在try / catch中,或者我个人最喜欢的只是在呼叫中加入一个陷阱;
// try/catch
try {
const response= await restApi.service('/newsfeed').find();
catch(e) {
// handle e
}
//just use catch
const response= await restApi.service('/newsfeed').find().catch(e => {
// handle e
});
如果您使用的是Promise,只需使用catch:
restApi.service('/newsfeed').find().then(response => {
// handle response
}).catch(e => {
// handle e
});