mcollina / mqemitter-redis

Redis-powered MQEmitter

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Better documentation for catching errors

puchm opened this issue · comments

commented

I tried to figure out how to catch a connection error which was already mentioned in several other issues. Apparently, the way to do it is:

const emitter = redis();
emitter.state.once('error', () => console.log(error))

This has a problem though: state is not in the type definitions - I just found out about this by looking at the test file. It also took way too long to find out about this so it should be documented in a better way.

I would propose

  1. adding a section to the Readme documenting this
  2. one of the following possibilities regarding state not being in the type defs
    1. Adding state to type defs
    2. Adding a separate function to the type defs, e.g. onError (if you don't want to expose the EventEmitter like that)
    3. Somehow redirecting the events that go to state so that they can be caught with emitter.on.

What do you think would be best?

Can you send a PR to add state to the typedefs?

commented

I tried doing that but it seems like it is completely impossible to catch the error.

This is a repo that shows it: https://github.com/puchm/mqemitter-redis-issue
I have patched the typedefs using patch-package for now.

Run npm start. The repo contains the main.ts file. It tries to connect to Redis (which won't work) and then starts a promise which should resolve after 10 seconds. After a few seconds, the Redis connection error gets thrown. The error listener gets executed properly but the execution stops after that. All other asynchronous code is also stopped, as the promise never resolves.

This can crash a server without the dev being able to catch it so we should fix this as soon as possible.

What could be the cause of this?

commented

This seems to be related to redis/ioredis#944.

Another possible solution is shown in redis/ioredis#68 and here: https://stackoverflow.com/a/61133867/9144769

I tried finding a solution but I couldn't get any of these to work. Could it be the case that there are calls to the Redis server when initializing the instance, other than the connection? I think there may be calls to redis before the connection is established.