Skip to content

Commit 6c22949

Browse files
committed
feat: add more error classes
1 parent 69004c7 commit 6c22949

File tree

2 files changed

+45
-9
lines changed

2 files changed

+45
-9
lines changed

README.md

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,26 +14,48 @@ Install with [NPM](https://npmjs.org/):
1414
## Usage
1515

1616
```js
17-
const { RedisError, ReplyError } = require('redis-errors');
17+
const { ReplyError, InterruptError } = require('redis-errors');
1818

1919
// Using async await
2020
try {
21-
return client.set('foo') // Missing value
21+
await client.set('foo') // Missing value
2222
} catch (err) {
23+
if (err instanceof InterruptError) {
24+
console.error('Command might have been processed')
25+
}
2326
if (err instanceof ReplyError) {
24-
console.log(err)
27+
// ...
2528
}
2629
throw err
2730
}
31+
32+
// Using callbacks
33+
client.set('foo', (err, res) => {
34+
if (err) {
35+
if (err instanceof InterruptError) {
36+
// ...
37+
}
38+
}
39+
})
2840
```
2941

3042
### Error classes
3143

32-
* `RedisError` sub class of Error
33-
* `ReplyError` sub class of RedisError
34-
* `ParserError` sub class of RedisError
35-
36-
All Redis errors will be returned as `ReplyErrors` while a parser error is returned as `ParserError`.
44+
* `RedisError` subclass of Error
45+
* `ReplyError` subclass of RedisError
46+
* `ParserError` subclass of RedisError
47+
* `AbortError` subclass of RedisError
48+
* `InterruptError` subclass of AbortError
49+
50+
* All errors returned by NodeRedis (client) are `RedisError`s.
51+
* All errors returned by Redis itself (server) will be a `ReplyError`.
52+
* Parsing errors are returned as `ParserError`. *Note:* Please report these!
53+
* If a command was not executed but rejected, it'll return a `AbortError`.
54+
* All executed commands that could not fulfill (e.g. network drop while
55+
executing) return a `InterruptError`.
56+
*Note:* Interrupt errors can happen for multiple reasons that are out of the
57+
scope of NodeRedis itself. There is nothing that can be done on library side
58+
to prevent those.
3759

3860
## License
3961

lib/errors.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,22 @@ class ReplyError extends RedisError {
3838
}
3939
}
4040

41+
class AbortError extends RedisError {
42+
get name () {
43+
return this.constructor.name
44+
}
45+
}
46+
47+
class InterruptError extends AbortError {
48+
get name () {
49+
return this.constructor.name
50+
}
51+
}
52+
4153
module.exports = {
4254
RedisError,
4355
ParserError,
44-
ReplyError
56+
ReplyError,
57+
AbortError,
58+
InterruptError
4559
}

0 commit comments

Comments
 (0)