node-ldapjs/lib/errors/index.js

152 lines
3.7 KiB
JavaScript
Raw Normal View History

2011-08-04 20:32:01 +00:00
// Copyright 2011 Mark Cavage, Inc. All rights reserved.
var util = require('util');
2015-11-03 04:31:02 +00:00
var assert = require('assert-plus');
2011-08-04 20:32:01 +00:00
var LDAPResult = require('../messages').LDAPResult;
///--- Globals
2014-07-01 23:36:47 +00:00
var CODES = require('./codes');
2011-08-04 20:32:01 +00:00
var ERRORS = [];
///--- Error Base class
2015-11-03 04:31:02 +00:00
function LDAPError(message, dn, caller) {
2011-08-04 20:32:01 +00:00
if (Error.captureStackTrace)
Error.captureStackTrace(this, caller || LDAPError);
2015-11-03 04:31:02 +00:00
this.lde_message = message;
this.lde_dn = dn;
2011-08-04 20:32:01 +00:00
}
util.inherits(LDAPError, Error);
2015-11-03 04:31:02 +00:00
Object.defineProperties(LDAPError.prototype, {
name: {
get: function getName() { return 'LDAPError'; },
configurable: false
},
code: {
get: function getCode() { return CODES.LDAP_OTHER; },
configurable: false
},
message: {
get: function getMessage() {
return this.lde_message || this.name;
},
configurable: false
},
dn: {
get: function getDN() {
return (this.lde_dn ? this.lde_dn.toString() : '');
},
configurable: false
}
});
2011-08-04 20:32:01 +00:00
///--- Exported API
module.exports = {};
2011-09-26 23:45:49 +00:00
module.exports.LDAPError = LDAPError;
2011-08-04 20:32:01 +00:00
2015-11-03 04:31:02 +00:00
// Some whacky games here to make sure all the codes are exported
Object.keys(CODES).forEach(function (code) {
2011-08-04 20:32:01 +00:00
module.exports[code] = CODES[code];
if (code === 'LDAP_SUCCESS')
return;
var err = '';
var msg = '';
var pieces = code.split('_').slice(1);
for (var i = 0; i < pieces.length; i++) {
var lc = pieces[i].toLowerCase();
var key = lc.charAt(0).toUpperCase() + lc.slice(1);
err += key;
msg += key + ((i + 1) < pieces.length ? ' ' : '');
}
if (!/\w+Error$/.test(err))
err += 'Error';
// At this point LDAP_OPERATIONS_ERROR is now OperationsError in $err
// and 'Operations Error' in $msg
module.exports[err] = function (message, dn, caller) {
2015-11-03 04:31:02 +00:00
LDAPError.call(this, message, dn, caller || module.exports[err]);
};
2011-08-04 20:32:01 +00:00
module.exports[err].constructor = module.exports[err];
util.inherits(module.exports[err], LDAPError);
2015-11-03 04:31:02 +00:00
Object.defineProperties(module.exports[err].prototype, {
name: {
get: function getName() { return err; },
configurable: false
},
code: {
get: function getCode() { return CODES[code]; },
configurable: false
}
});
2011-08-04 20:32:01 +00:00
ERRORS[CODES[code]] = {
err: err,
message: msg
};
});
module.exports.getError = function (res) {
2015-11-03 04:31:02 +00:00
assert.ok(res instanceof LDAPResult, 'res (LDAPResult) required');
2011-08-04 20:32:01 +00:00
var errObj = ERRORS[res.status];
var E = module.exports[errObj.err];
return new E(res.errorMessage || errObj.message,
res.matchedDN || null,
module.exports.getError);
};
module.exports.getMessage = function (code) {
2015-11-03 04:31:02 +00:00
assert.number(code, 'code (number) required');
2011-08-04 20:32:01 +00:00
var errObj = ERRORS[code];
2011-08-04 20:32:01 +00:00
return (errObj && errObj.message ? errObj.message : '');
};
///--- Custom application errors
function ConnectionError(message) {
2015-11-03 04:31:02 +00:00
LDAPError.call(this, message, null, ConnectionError);
}
util.inherits(ConnectionError, LDAPError);
module.exports.ConnectionError = ConnectionError;
2015-11-03 04:31:02 +00:00
Object.defineProperties(ConnectionError.prototype, {
name: {
get: function () { return 'ConnectionError'; },
configurable: false
}
});
function AbandonedError(message) {
2015-11-03 04:31:02 +00:00
LDAPError.call(this, message, null, AbandonedError);
}
util.inherits(AbandonedError, LDAPError);
module.exports.AbandonedError = AbandonedError;
2015-11-03 04:31:02 +00:00
Object.defineProperties(AbandonedError.prototype, {
name: {
get: function () { return 'AbandonedError'; },
configurable: false
}
});
function TimeoutError(message) {
2015-11-03 04:31:02 +00:00
LDAPError.call(this, message, null, TimeoutError);
}
util.inherits(TimeoutError, LDAPError);
module.exports.TimeoutError = TimeoutError;
2015-11-03 04:31:02 +00:00
Object.defineProperties(TimeoutError.prototype, {
name: {
get: function () { return 'TimeoutError'; },
configurable: false
}
});