Skip to content

Commit afdd9b0

Browse files
DOC-5882 improve node-redis error handling page
1 parent fd9e48a commit afdd9b0

File tree

3 files changed

+33
-22
lines changed

3 files changed

+33
-22
lines changed

content/develop/clients/nodejs/error-handling.md

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
title: Error handling
33
description: Learn how to handle errors when using node-redis.
44
linkTitle: Error handling
5-
weight: 50
5+
weight: 6
66
---
77

88
node-redis uses
@@ -63,6 +63,27 @@ client.get(key)
6363
});
6464
```
6565

66+
## Error events
67+
68+
Node-Redis provides [multiple events to handle various scenarios](https://github.com/redis/node-redis?tab=readme-ov-file#events), among which the most critical is the `error` event.
69+
70+
This event is triggered whenever an error occurs within the client.
71+
72+
It is crucial to listen for error events.
73+
74+
If a client does not register at least one error listener and an error occurs, the system will throw that error, potentially causing the Node.js process to exit unexpectedly.
75+
See [the EventEmitter docs](https://nodejs.org/api/events.html#events_error_events) for more details.
76+
77+
```typescript
78+
const client = createClient({
79+
// ... client options
80+
});
81+
// Always ensure there's a listener for errors in the client to prevent process crashes due to unhandled errors
82+
client.on('error', error => {
83+
console.error(`Redis client error:`, error);
84+
});
85+
```
86+
6687
## Applying error handling patterns
6788

6889
The [Error handling]({{< relref "/develop/clients/error-handling" >}})
@@ -134,6 +155,11 @@ async function getWithRetry(key, { attempts = 3, baseDelayMs = 100 } = {}) {
134155
}
135156
```
136157

158+
Note that you can also configure node-redis to reconnect to the
159+
server automatically when the connection is lost. See
160+
[Reconnect after disconnection]({{< relref "/develop/clients/nodejs/connect#reconnect-after-disconnection" >}})
161+
for more information.
162+
137163
### Pattern 4: Log and continue
138164

139165
Log non-critical errors and continue (see
@@ -156,4 +182,3 @@ try {
156182

157183
- [Error handling]({{< relref "/develop/clients/error-handling" >}})
158184
- [Production usage]({{< relref "/develop/clients/nodejs/produsage" >}})
159-
- [Connection pooling]({{< relref "/develop/clients/pools-and-muxing" >}})

content/develop/clients/nodejs/migration.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ categories:
1212
description: Discover the differences between `ioredis` and `node-redis`.
1313
linkTitle: Migrate from ioredis
1414
title: Migrate from ioredis
15-
weight: 6
15+
weight: 10
1616
---
1717

1818
Redis previously recommended the [`ioredis`](https://github.com/redis/ioredis)

content/develop/clients/nodejs/produsage.md

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ categories:
1212
description: Get your Node.js app ready for production
1313
linkTitle: Production usage
1414
title: Production usage
15-
weight: 5
15+
weight: 8
1616
---
1717

1818
This guide offers recommendations to get the best reliability and
@@ -38,24 +38,10 @@ progress in implementing the recommendations.
3838

3939
Node-Redis provides [multiple events to handle various scenarios](https://github.com/redis/node-redis?tab=readme-ov-file#events), among which the most critical is the `error` event.
4040

41-
This event is triggered whenever an error occurs within the client.
42-
43-
It is crucial to listen for error events.
44-
45-
If a client does not register at least one error listener and an error occurs, the system will throw that error, potentially causing the Node.js process to exit unexpectedly.
46-
See [the EventEmitter docs](https://nodejs.org/api/events.html#events_error_events) for more details.
47-
48-
```typescript
49-
const client = createClient({
50-
// ... client options
51-
});
52-
// Always ensure there's a listener for errors in the client to prevent process crashes due to unhandled errors
53-
client.on('error', error => {
54-
console.error(`Redis client error:`, error);
55-
});
56-
```
57-
58-
See also [Error handling]({{< relref "/develop/clients/nodejs/error-handling" >}}) for a more detailed discussion of error handling approaches in `node-redis`.
41+
This event is triggered whenever an error occurs within the client, and
42+
it is very important to set a handler to listen for it.
43+
See [Error events]({{< relref "/develop/clients/nodejs/error-handling#error-events" >}})
44+
for more information and an example of setting an error handler.
5945

6046
### Handling reconnections
6147

0 commit comments

Comments
 (0)