forked from aerospike/aerospike-client-nodejs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherror.js
More file actions
138 lines (119 loc) · 4.99 KB
/
error.js
File metadata and controls
138 lines (119 loc) · 4.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
// *****************************************************************************
// Copyright 2013-2019 Aerospike, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License")
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// *****************************************************************************
'use strict'
/* global expect, describe, it */
const AerospikeError = require('../lib/error')
const status = require('../lib/status')
require('./test_helper.js')
describe('AerospikeError #noserver', function () {
describe('constructor', function () {
it('creates a new AerospikeError instance', function () {
expect(new AerospikeError()).to.be.instanceof(AerospikeError)
})
it('inherits from the Error class', function () {
expect(new AerospikeError()).to.be.instanceof(Error)
})
it('initializes the error with default values', function () {
const subject = new AerospikeError()
expect(subject).to.have.property('message', '')
expect(subject).to.have.property('code', status.ERR_CLIENT)
expect(subject).to.have.property('command', null)
expect(subject).to.have.property('func', null)
expect(subject).to.have.property('file', null)
expect(subject).to.have.property('line', null)
expect(subject).to.have.property('inDoubt', false)
})
it('sets an error message', function () {
const subject = new AerospikeError('Dooh!')
expect(subject).to.have.property('message', 'Dooh!')
})
it('keeps a reference to the command', function () {
const cmd = {}
const subject = new AerospikeError('Dooh!', cmd)
expect(subject).to.have.property('command', cmd)
})
it('captures a stacktrace', function () {
const subject = new AerospikeError('Dooh!')
expect(subject).to.have.property('stack')
.that.is.a('string')
.that.includes('AerospikeError: Dooh!')
})
it('copies the stacktrace of the command', function () {
const cmd = { name: 'AerospikeError', message: 'Dooh!' }
Error.captureStackTrace(cmd)
const subject = new AerospikeError('Dooh!', cmd)
expect(subject).to.have.property('stack')
.that.is.a('string')
.that.equals(cmd.stack)
})
})
describe('.fromASError', function () {
it('copies the info from a AerospikeClient error instance', function () {
const error = {
code: -11,
message: 'Dooh!',
func: 'connect',
file: 'lib/client.js',
line: 101,
inDoubt: true
}
const subject = AerospikeError.fromASError(error)
expect(subject).to.have.property('code', -11)
expect(subject).to.have.property('message', 'Dooh!')
expect(subject).to.have.property('func', 'connect')
expect(subject).to.have.property('file', 'lib/client.js')
expect(subject).to.have.property('line', 101)
expect(subject).to.have.property('inDoubt', true)
})
it('replaces error codes with descriptive messages', function () {
const error = {
code: status.ERR_RECORD_NOT_FOUND,
message: '127.0.0.1:3000 AEROSPIKE_ERR_RECORD_NOT_FOUND'
}
const subject = AerospikeError.fromASError(error)
expect(subject.message).to.equal('127.0.0.1:3000 Record does not exist in database. May be returned by read, or write with policy Aerospike.policy.exists.UPDATE')
})
it('returns an AerospikeError instance unmodified', function () {
const error = new AerospikeError('Dooh!')
expect(AerospikeError.fromASError(error)).to.equal(error)
})
it('returns null if the status code is OK', function () {
const error = { code: status.OK }
expect(AerospikeError.fromASError(error)).to.be.null()
})
it('returns null if no error is passed', function () {
expect(AerospikeError.fromASError(null)).to.be.null()
})
})
describe('#isServerError()', function () {
it('returns true if the error code indicates a server error', function () {
const error = { code: status.ERR_RECORD_NOT_FOUND }
const subject = AerospikeError.fromASError(error)
expect(subject.isServerError()).to.be.true()
})
it('returns false if the error code indicates a client error', function () {
const error = { code: status.ERR_PARAM }
const subject = AerospikeError.fromASError(error)
expect(subject.isServerError()).to.be.false()
})
})
describe('#toString()', function () {
it('sets an informative error message', function () {
const subject = new AerospikeError('Dooh!')
expect(subject.toString()).to.eql('AerospikeError: Dooh!')
})
})
})