Skip to content

Commit ee28994

Browse files
committed
v.1.0.0
0 parents  commit ee28994

File tree

9 files changed

+205
-0
lines changed

9 files changed

+205
-0
lines changed

.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
logs
2+
*.log
3+
coverage
4+
node_modules
5+
.idea
6+
.vscode

.npmignore

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# IntelliJ project files
2+
.idea
3+
*.iml
4+
out
5+
gen
6+
7+
# Unrelevant files and folders
8+
benchmark
9+
coverage
10+
test
11+
.travis.yml
12+
.gitignore
13+
*.log
14+
.vscode
15+
.codeclimate.yml

.travis.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
language: node_js
2+
sudo: false
3+
node_js:
4+
- "4"
5+
- "6"
6+
- "7"
7+
install:
8+
- npm install

LICENSE

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2017 Ruben Bridgewater
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.
22+

README.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
[![Build Status](https://travis-ci.org/NodeRedis/redis-errors.png?branch=master)](https://travis-ci.org/NodeRedis/redis-errors)
2+
[![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg)](http://standardjs.com/)
3+
4+
# redis-errors
5+
6+
All error classes used in [node_redis](https://github.com/NodeRedis/node_redis) are in here. They can be required as needed.
7+
8+
## Install
9+
10+
Install with [NPM](https://npmjs.org/):
11+
12+
npm install redis-errors
13+
14+
## Usage
15+
16+
```js
17+
const { RedisError, ReplyError } = require('redis-errors');
18+
19+
// Using async await
20+
try {
21+
return client.set('foo') // Missing value
22+
} catch (err) {
23+
if (err instanceof ReplyError) {
24+
console.log(err)
25+
}
26+
throw err
27+
}
28+
```
29+
30+
### Error classes
31+
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`.
37+
38+
## License
39+
40+
[MIT](./LICENSE)

index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
'use strict'
2+
3+
module.exports = require('./lib/errors')

lib/errors.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
'use strict'
2+
3+
const assert = require('assert')
4+
5+
class RedisError extends Error {
6+
get name () {
7+
return this.constructor.name
8+
}
9+
}
10+
11+
class ParserError extends RedisError {
12+
constructor (message, buffer, offset) {
13+
assert(buffer)
14+
assert.strictEqual(typeof offset, 'number')
15+
16+
const tmp = Error.stackTraceLimit
17+
Error.stackTraceLimit = 2
18+
super(message)
19+
Error.stackTraceLimit = tmp
20+
this.offset = offset
21+
this.buffer = buffer
22+
}
23+
24+
get name () {
25+
return this.constructor.name
26+
}
27+
}
28+
29+
class ReplyError extends RedisError {
30+
constructor (message) {
31+
const tmp = Error.stackTraceLimit
32+
Error.stackTraceLimit = 2
33+
super(message)
34+
Error.stackTraceLimit = tmp
35+
}
36+
get name () {
37+
return this.constructor.name
38+
}
39+
}
40+
41+
module.exports = {
42+
RedisError,
43+
ParserError,
44+
ReplyError
45+
}

package.json

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
{
2+
"name": "redis-errors",
3+
"version": "1.0.0",
4+
"description": "Error classes used in node_redis",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "npm run coverage",
8+
"lint": "standard --fix",
9+
"posttest": "npm run lint && npm run coverage:check",
10+
"coverage": "node ./node_modules/istanbul/lib/cli.js cover --preserve-comments ./node_modules/mocha/bin/_mocha -- -R spec",
11+
"coverage:check": "node ./node_modules/istanbul/lib/cli.js check-coverage --branch 100 --statement 100"
12+
},
13+
"repository": {
14+
"type": "git",
15+
"url": "git+https://github.com/NodeRedis/node-redis-parser.git"
16+
},
17+
"keywords": [
18+
"redis",
19+
"javascript",
20+
"node",
21+
"error"
22+
],
23+
"engines": {
24+
"node": ">=4"
25+
},
26+
"devDependencies": {
27+
"buffer-from": "^0.1.1",
28+
"istanbul": "^0.4.0",
29+
"mocha": "^3.1.2",
30+
"standard": "^10.0.0"
31+
},
32+
"author": "Ruben Bridgewater",
33+
"license": "MIT",
34+
"bugs": {
35+
"url": "https://github.com/NodeRedis/redis-errors/issues"
36+
},
37+
"homepage": "https://github.com/NodeRedis/redis-errors#readme",
38+
"directories": {
39+
"test": "test",
40+
"lib": "lib"
41+
}
42+
}

test/errors.spec.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
'use strict'
2+
3+
/* eslint-env mocha */
4+
5+
const assert = require('assert')
6+
const bufferFrom = require('buffer-from')
7+
const errors = require('../')
8+
const ReplyError = errors.ReplyError
9+
const ParserError = errors.ParserError
10+
const RedisError = errors.RedisError
11+
12+
describe('errors', function () {
13+
it('errors should have a stack trace with error message', function () {
14+
const err1 = new RedisError('test')
15+
const err2 = new ReplyError('test')
16+
const err3 = new ParserError('test', bufferFrom(''), 0)
17+
assert(err1.stack)
18+
assert(err2.stack)
19+
assert(err3.stack)
20+
assert(/RedisError: test/.test(err1.stack))
21+
assert(/ReplyError: test/.test(err2.stack))
22+
assert(/ParserError: test/.test(err3.stack))
23+
})
24+
})

0 commit comments

Comments
 (0)