Better documentation for catching errors
puchm opened this issue · comments
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
- adding a section to the Readme documenting this
- one of the following possibilities regarding
state
not being in the type defs- Adding
state
to type defs - Adding a separate function to the type defs, e.g.
onError
(if you don't want to expose theEventEmitter
like that) - Somehow redirecting the events that go to
state
so that they can be caught withemitter.on
.
- Adding
What do you think would be best?
Can you send a PR to add state to the typedefs?
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?
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.