Skip to content
This repository was archived by the owner on May 14, 2024. It is now read-only.

Commit dcce6eb

Browse files
committed
Clean up getters/setters in errors
1 parent df43cab commit dcce6eb

File tree

2 files changed

+100
-46
lines changed

2 files changed

+100
-46
lines changed

lib/errors/index.js

Lines changed: 61 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright 2011 Mark Cavage, Inc. All rights reserved.
22

33
var util = require('util');
4+
var assert = require('assert-plus');
45

56
var LDAPResult = require('../messages').LDAPResult;
67

@@ -11,36 +12,46 @@ var CODES = require('./codes');
1112
var ERRORS = [];
1213

1314

14-
1515
///--- Error Base class
1616

17-
function LDAPError(errorName, errorCode, msg, dn, caller) {
17+
function LDAPError(message, dn, caller) {
1818
if (Error.captureStackTrace)
1919
Error.captureStackTrace(this, caller || LDAPError);
2020

21-
this.__defineGetter__('dn', function () {
22-
return (dn ? (dn.toString() || '') : '');
23-
});
24-
this.__defineGetter__('code', function () {
25-
return errorCode;
26-
});
27-
this.__defineGetter__('name', function () {
28-
return errorName;
29-
});
30-
this.__defineGetter__('message', function () {
31-
return msg || errorName;
32-
});
21+
this.lde_message = message;
22+
this.lde_dn = dn;
3323
}
3424
util.inherits(LDAPError, Error);
35-
25+
Object.defineProperties(LDAPError.prototype, {
26+
name: {
27+
get: function getName() { return 'LDAPError'; },
28+
configurable: false
29+
},
30+
code: {
31+
get: function getCode() { return CODES.LDAP_OTHER; },
32+
configurable: false
33+
},
34+
message: {
35+
get: function getMessage() {
36+
return this.lde_message || this.name;
37+
},
38+
configurable: false
39+
},
40+
dn: {
41+
get: function getDN() {
42+
return (this.lde_dn ? this.lde_dn.toString() : '');
43+
},
44+
configurable: false
45+
}
46+
});
3647

3748

3849
///--- Exported API
39-
// Some whacky games here to make sure all the codes are exported
4050

4151
module.exports = {};
4252
module.exports.LDAPError = LDAPError;
4353

54+
// Some whacky games here to make sure all the codes are exported
4455
Object.keys(CODES).forEach(function (code) {
4556
module.exports[code] = CODES[code];
4657
if (code === 'LDAP_SUCCESS')
@@ -62,26 +73,29 @@ Object.keys(CODES).forEach(function (code) {
6273
// At this point LDAP_OPERATIONS_ERROR is now OperationsError in $err
6374
// and 'Operations Error' in $msg
6475
module.exports[err] = function (message, dn, caller) {
65-
LDAPError.call(this,
66-
err,
67-
CODES[code],
68-
message || msg,
69-
dn || null,
70-
caller || module.exports[err]);
76+
LDAPError.call(this, message, dn, caller || module.exports[err]);
7177
};
7278
module.exports[err].constructor = module.exports[err];
7379
util.inherits(module.exports[err], LDAPError);
80+
Object.defineProperties(module.exports[err].prototype, {
81+
name: {
82+
get: function getName() { return err; },
83+
configurable: false
84+
},
85+
code: {
86+
get: function getCode() { return CODES[code]; },
87+
configurable: false
88+
}
89+
});
7490

7591
ERRORS[CODES[code]] = {
7692
err: err,
7793
message: msg
7894
};
7995
});
8096

81-
8297
module.exports.getError = function (res) {
83-
if (!(res instanceof LDAPResult))
84-
throw new TypeError('res (LDAPResult) required');
98+
assert.ok(res instanceof LDAPResult, 'res (LDAPResult) required');
8599

86100
var errObj = ERRORS[res.status];
87101
var E = module.exports[errObj.err];
@@ -90,10 +104,8 @@ module.exports.getError = function (res) {
90104
module.exports.getError);
91105
};
92106

93-
94107
module.exports.getMessage = function (code) {
95-
if (typeof (code) !== 'number')
96-
throw new TypeError('code (number) required');
108+
assert.number(code, 'code (number) required');
97109

98110
var errObj = ERRORS[code];
99111
return (errObj && errObj.message ? errObj.message : '');
@@ -103,34 +115,37 @@ module.exports.getMessage = function (code) {
103115
///--- Custom application errors
104116

105117
function ConnectionError(message) {
106-
LDAPError.call(this,
107-
'ConnectionError',
108-
CODES.LDAP_OTHER,
109-
message,
110-
null,
111-
ConnectionError);
118+
LDAPError.call(this, message, null, ConnectionError);
112119
}
113120
util.inherits(ConnectionError, LDAPError);
114121
module.exports.ConnectionError = ConnectionError;
122+
Object.defineProperties(ConnectionError.prototype, {
123+
name: {
124+
get: function () { return 'ConnectionError'; },
125+
configurable: false
126+
}
127+
});
115128

116129
function AbandonedError(message) {
117-
LDAPError.call(this,
118-
'AbandonedError',
119-
CODES.LDAP_OTHER,
120-
message,
121-
null,
122-
AbandonedError);
130+
LDAPError.call(this, message, null, AbandonedError);
123131
}
124132
util.inherits(AbandonedError, LDAPError);
125133
module.exports.AbandonedError = AbandonedError;
134+
Object.defineProperties(AbandonedError.prototype, {
135+
name: {
136+
get: function () { return 'AbandonedError'; },
137+
configurable: false
138+
}
139+
});
126140

127141
function TimeoutError(message) {
128-
LDAPError.call(this,
129-
'TimeoutError',
130-
CODES.LDAP_OTHER,
131-
message,
132-
null,
133-
TimeoutError);
142+
LDAPError.call(this, message, null, TimeoutError);
134143
}
135144
util.inherits(TimeoutError, LDAPError);
136145
module.exports.TimeoutError = TimeoutError;
146+
Object.defineProperties(TimeoutError.prototype, {
147+
name: {
148+
get: function () { return 'TimeoutError'; },
149+
configurable: false
150+
}
151+
});

test/errors.test.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Copyright 2015 Joyent, Inc.
2+
3+
var test = require('tape').test;
4+
5+
var ldap = require('../lib/index');
6+
7+
8+
///--- Tests
9+
10+
test('basic error', function (t) {
11+
var msg = 'mymsg';
12+
var err = new ldap.LDAPError(msg, null, null);
13+
t.ok(err);
14+
t.equal(err.name, 'LDAPError');
15+
t.equal(err.code, ldap.LDAP_OTHER);
16+
t.equal(err.dn, '');
17+
t.equal(err.message, msg);
18+
t.end();
19+
});
20+
21+
test('"custom" errors', function (t) {
22+
var errors = [
23+
{ name: 'ConnectionError', func: ldap.ConnectionError },
24+
{ name: 'AbandonedError', func: ldap.AbandonedError },
25+
{ name: 'TimeoutError', func: ldap.TimeoutError }
26+
];
27+
28+
errors.forEach(function (entry) {
29+
var msg = entry.name + 'msg';
30+
var err = new entry.func(msg);
31+
t.ok(err);
32+
t.equal(err.name, entry.name);
33+
t.equal(err.code, ldap.LDAP_OTHER);
34+
t.equal(err.dn, '');
35+
t.equal(err.message, msg);
36+
});
37+
38+
t.end();
39+
});

0 commit comments

Comments
 (0)