Search code examples
socket.iofeathersjs

Where do I handle feathersjs/socketio-client connection error


I'm using feathers with socketio in backend. The client is listening and everything works well.

I want to handle the 'server not responding' error and I don't find where can I do that?

The error thrown by the server:

"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" }"

It is correct, the 'promise' is not handled! Where do I handle?

I tried adding catch on each line just to see what works but without success:

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

Solution

  • The timeout error is coming from a particular request so you have to handle it wherever you are making the request.

    restApi.service('/newsfeed').find()

    If you're using async/await, you can wrap your service call in a try/catch or my personal favorite is just putting a catch on the call;

    // 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
    });
    

    If you're using promises, just use a catch:

    restApi.service('/newsfeed').find().then(response => {
      // handle response
    }).catch(e => {
      // handle e
    });