Skip to content

Commit 1edb951

Browse files
Merge pull request #2349 from redis/DOC-5881-python-error-checks
DOC-5880 DOC-5881 DOC-5882 DOC-5883 DOC-5885 DOC-5886 Error checking pages
2 parents 28c01b2 + 5a67374 commit 1edb951

File tree

13 files changed

+1159
-19
lines changed

13 files changed

+1159
-19
lines changed

assets/css/index.css

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,12 @@ section.prose {
8888
@apply bg-slate-900 rounded-lg;
8989
}
9090

91+
.prose pre.mermaid {
92+
background-color: white !important;
93+
border-radius: 0.5rem;
94+
padding: 1rem;
95+
}
96+
9197
.prose pre > code {
9298
@apply bg-none font-monogeist;
9399
}
Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
---
2+
title: Error handling
3+
description: Learn how to handle errors when using NRedisStack.
4+
linkTitle: Error handling
5+
weight: 60
6+
---
7+
8+
NRedisStack uses **exceptions** to signal errors. Code examples in the documentation often omit error handling for brevity, but it is essential in production code. This page explains how NRedisStack's error handling works and how to apply common error handling patterns.
9+
10+
For an overview of error types and handling strategies, see [Error handling]({{< relref "/develop/clients/error-handling" >}}).
11+
See also [Production usage]({{< relref "/develop/clients/dotnet/produsage" >}})
12+
for more information on connection management, timeouts, and other aspects of
13+
app reliability.
14+
15+
## Exception types
16+
17+
NRedisStack throws exceptions to signal errors. Common exception types include:
18+
19+
| Exception | When it occurs | Recoverable | Recommended action |
20+
|---|---|---|---|
21+
| `RedisConnectionException` | Connection refused or lost || Retry with backoff or fall back |
22+
| `RedisTimeoutException` | Operation exceeded timeout || Retry with backoff |
23+
| `RedisCommandException` | Invalid command or arguments || Fix the command or arguments |
24+
| `RedisServerException` | Invalid operation on server || Fix the operation or data |
25+
26+
See [Categories of errors]({{< relref "/develop/clients/error-handling#categories-of-errors" >}})
27+
for a more detailed discussion of these errors and their causes.
28+
29+
## Applying error handling patterns
30+
31+
The [Error handling]({{< relref "/develop/clients/error-handling" >}}) overview
32+
describes four main patterns. The sections below show how to implement them in
33+
NRedisStack:
34+
35+
### Pattern 1: Fail fast
36+
37+
Catch specific exceptions that represent unrecoverable errors and re-throw them (see
38+
[Pattern 1: Fail fast]({{< relref "/develop/clients/error-handling#pattern-1-fail-fast" >}})
39+
for a full description):
40+
41+
```csharp
42+
using NRedisStack;
43+
using StackExchange.Redis;
44+
45+
var muxer = ConnectionMultiplexer.Connect("localhost:6379");
46+
var db = muxer.GetDatabase();
47+
48+
try {
49+
var result = db.StringGet("key");
50+
} catch (RedisCommandException) {
51+
// This indicates a bug in the code, such as using
52+
// StringGet on a list key.
53+
throw;
54+
}
55+
```
56+
57+
### Pattern 2: Graceful degradation
58+
59+
Catch specific errors and fall back to an alternative, where possible (see
60+
[Pattern 2: Graceful degradation]({{< relref "/develop/clients/error-handling#pattern-2-graceful-degradation" >}})
61+
for a full description):
62+
63+
```csharp
64+
try {
65+
var cachedValue = db.StringGet(key);
66+
return cachedValue.ToString();
67+
} catch (RedisConnectionException) {
68+
logger.LogWarning("Cache unavailable, using database");
69+
70+
// Fallback to database
71+
return database.Get(key);
72+
}
73+
```
74+
75+
### Pattern 3: Retry with backoff
76+
77+
Retry on temporary errors such as timeouts (see
78+
[Pattern 3: Retry with backoff]({{< relref "/develop/clients/error-handling#pattern-3-retry-with-backoff" >}})
79+
for a full description):
80+
81+
```csharp
82+
const int maxRetries = 3;
83+
int retryDelay = 100;
84+
85+
for (int attempt = 0; attempt < maxRetries; attempt++) {
86+
try {
87+
return db.StringGet(key).ToString();
88+
} catch (RedisTimeoutException) {
89+
if (attempt < maxRetries - 1) {
90+
Thread.Sleep(retryDelay);
91+
retryDelay *= 2; // Exponential backoff
92+
} else {
93+
throw;
94+
}
95+
}
96+
}
97+
```
98+
99+
See also [Timeouts]({{< relref "/develop/clients/dotnet/produsage#timeouts" >}})
100+
for more information on configuring timeouts in NRedisStack.
101+
102+
### Pattern 4: Log and continue
103+
104+
Log non-critical errors and continue (see
105+
[Pattern 4: Log and continue]({{< relref "/develop/clients/error-handling#pattern-4-log-and-continue" >}})
106+
for a full description):
107+
108+
```csharp
109+
try {
110+
db.StringSet(key, value, TimeSpan.FromSeconds(3600));
111+
} catch (RedisConnectionException) {
112+
logger.LogWarning($"Failed to cache {key}, continuing without cache");
113+
// Application continues normally
114+
}
115+
```
116+
117+
## Async error handling
118+
119+
Error handling works the usual way with `async`/`await`, as shown in the
120+
example below:
121+
122+
```csharp
123+
using NRedisStack;
124+
using StackExchange.Redis;
125+
126+
var muxer = ConnectionMultiplexer.Connect("localhost:6379");
127+
var db = muxer.GetDatabase();
128+
129+
async Task<string> GetWithFallbackAsync(string key) {
130+
try {
131+
var result = await db.StringGetAsync(key);
132+
if (result.HasValue) {
133+
return result.ToString();
134+
}
135+
} catch (RedisConnectionException) {
136+
logger.LogWarning("Cache unavailable");
137+
return await database.GetAsync(key);
138+
}
139+
140+
return await database.GetAsync(key);
141+
}
142+
```
143+
144+
## See also
145+
146+
- [Error handling]({{< relref "/develop/clients/error-handling" >}})
147+
- [Production usage]({{< relref "/develop/clients/dotnet/produsage" >}})

content/develop/clients/dotnet/produsage.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,9 @@ the most common Redis exceptions:
112112
[stream entry]({{< relref "/develop/data-types/streams#entry-ids" >}})
113113
using an invalid ID).
114114

115+
See [Error handling]({{< relref "/develop/clients/dotnet/error-handling" >}})
116+
for more information on handling exceptions.
117+
115118
### Retries
116119

117120
During the initial `ConnectionMultiplexer.Connect()` call, `NRedisStack` will

0 commit comments

Comments
 (0)