2019-08-27 21:11:49 +00:00
|
|
|
'use strict'
|
2011-08-04 20:32:01 +00:00
|
|
|
|
2020-10-31 21:07:32 +00:00
|
|
|
const util = require('util')
|
|
|
|
const assert = require('assert-plus')
|
2011-08-04 20:32:01 +00:00
|
|
|
|
2020-10-31 21:07:32 +00:00
|
|
|
const LDAPResult = require('../messages').LDAPResult
|
2011-08-04 20:32:01 +00:00
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
/// --- Globals
|
2011-08-04 20:32:01 +00:00
|
|
|
|
2020-10-31 21:07:32 +00:00
|
|
|
const CODES = require('./codes')
|
|
|
|
const ERRORS = []
|
2011-08-04 20:32:01 +00:00
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
/// --- Error Base class
|
2011-08-04 20:32:01 +00:00
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
function LDAPError (message, dn, caller) {
|
|
|
|
if (Error.captureStackTrace) { Error.captureStackTrace(this, caller || LDAPError) }
|
2011-08-04 20:32:01 +00:00
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
this.lde_message = message
|
|
|
|
this.lde_dn = dn
|
2011-08-04 20:32:01 +00:00
|
|
|
}
|
2019-08-27 21:11:49 +00:00
|
|
|
util.inherits(LDAPError, Error)
|
2015-11-03 04:31:02 +00:00
|
|
|
Object.defineProperties(LDAPError.prototype, {
|
|
|
|
name: {
|
2019-08-27 21:11:49 +00:00
|
|
|
get: function getName () { return 'LDAPError' },
|
2015-11-03 04:31:02 +00:00
|
|
|
configurable: false
|
|
|
|
},
|
|
|
|
code: {
|
2019-08-27 21:11:49 +00:00
|
|
|
get: function getCode () { return CODES.LDAP_OTHER },
|
2015-11-03 04:31:02 +00:00
|
|
|
configurable: false
|
|
|
|
},
|
|
|
|
message: {
|
2019-08-27 21:11:49 +00:00
|
|
|
get: function getMessage () {
|
|
|
|
return this.lde_message || this.name
|
2015-11-03 04:31:02 +00:00
|
|
|
},
|
2021-02-19 04:45:14 +00:00
|
|
|
set: function setMessage (message) {
|
|
|
|
this.lde_message = message
|
|
|
|
},
|
2015-11-03 04:31:02 +00:00
|
|
|
configurable: false
|
|
|
|
},
|
|
|
|
dn: {
|
2019-08-27 21:11:49 +00:00
|
|
|
get: function getDN () {
|
|
|
|
return (this.lde_dn ? this.lde_dn.toString() : '')
|
2015-11-03 04:31:02 +00:00
|
|
|
},
|
|
|
|
configurable: false
|
|
|
|
}
|
2019-08-27 21:11:49 +00:00
|
|
|
})
|
2011-08-04 20:32:01 +00:00
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
/// --- Exported API
|
2011-08-04 20:32:01 +00:00
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
module.exports = {}
|
|
|
|
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
|
2012-02-18 08:15:52 +00:00
|
|
|
Object.keys(CODES).forEach(function (code) {
|
2019-08-27 21:11:49 +00:00
|
|
|
module.exports[code] = CODES[code]
|
|
|
|
if (code === 'LDAP_SUCCESS') { return }
|
2011-08-04 20:32:01 +00:00
|
|
|
|
2020-10-31 21:07:32 +00:00
|
|
|
let err = ''
|
|
|
|
let msg = ''
|
|
|
|
const pieces = code.split('_').slice(1)
|
|
|
|
for (let i = 0; i < pieces.length; i++) {
|
|
|
|
const lc = pieces[i].toLowerCase()
|
|
|
|
const key = lc.charAt(0).toUpperCase() + lc.slice(1)
|
2019-08-27 21:11:49 +00:00
|
|
|
err += key
|
|
|
|
msg += key + ((i + 1) < pieces.length ? ' ' : '')
|
2011-08-04 20:32:01 +00:00
|
|
|
}
|
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
if (!/\w+Error$/.test(err)) { err += 'Error' }
|
2011-08-04 20:32:01 +00:00
|
|
|
|
|
|
|
// At this point LDAP_OPERATIONS_ERROR is now OperationsError in $err
|
|
|
|
// and 'Operations Error' in $msg
|
2012-02-18 08:15:52 +00:00
|
|
|
module.exports[err] = function (message, dn, caller) {
|
2019-08-27 21:11:49 +00:00
|
|
|
LDAPError.call(this, message, dn, caller || module.exports[err])
|
|
|
|
}
|
|
|
|
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: {
|
2019-08-27 21:11:49 +00:00
|
|
|
get: function getName () { return err },
|
2015-11-03 04:31:02 +00:00
|
|
|
configurable: false
|
|
|
|
},
|
|
|
|
code: {
|
2019-08-27 21:11:49 +00:00
|
|
|
get: function getCode () { return CODES[code] },
|
2015-11-03 04:31:02 +00:00
|
|
|
configurable: false
|
|
|
|
}
|
2019-08-27 21:11:49 +00:00
|
|
|
})
|
2011-08-04 20:32:01 +00:00
|
|
|
|
|
|
|
ERRORS[CODES[code]] = {
|
|
|
|
err: err,
|
|
|
|
message: msg
|
2019-08-27 21:11:49 +00:00
|
|
|
}
|
|
|
|
})
|
2011-08-04 20:32:01 +00:00
|
|
|
|
2012-02-18 08:15:52 +00:00
|
|
|
module.exports.getError = function (res) {
|
2019-08-27 21:11:49 +00:00
|
|
|
assert.ok(res instanceof LDAPResult, 'res (LDAPResult) required')
|
2011-08-04 20:32:01 +00:00
|
|
|
|
2020-10-31 21:07:32 +00:00
|
|
|
const errObj = ERRORS[res.status]
|
|
|
|
const E = module.exports[errObj.err]
|
2011-08-04 20:32:01 +00:00
|
|
|
return new E(res.errorMessage || errObj.message,
|
2019-08-27 21:11:49 +00:00
|
|
|
res.matchedDN || null,
|
|
|
|
module.exports.getError)
|
|
|
|
}
|
2011-08-04 20:32:01 +00:00
|
|
|
|
2012-02-18 08:15:52 +00:00
|
|
|
module.exports.getMessage = function (code) {
|
2019-08-27 21:11:49 +00:00
|
|
|
assert.number(code, 'code (number) required')
|
2012-04-27 03:23:43 +00:00
|
|
|
|
2020-10-31 21:07:32 +00:00
|
|
|
const errObj = ERRORS[code]
|
2019-08-27 21:11:49 +00:00
|
|
|
return (errObj && errObj.message ? errObj.message : '')
|
|
|
|
}
|
2012-04-27 03:23:43 +00:00
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
/// --- Custom application errors
|
2012-04-27 03:23:43 +00:00
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
function ConnectionError (message) {
|
|
|
|
LDAPError.call(this, message, null, ConnectionError)
|
2012-04-27 03:23:43 +00:00
|
|
|
}
|
2019-08-27 21:11:49 +00:00
|
|
|
util.inherits(ConnectionError, LDAPError)
|
|
|
|
module.exports.ConnectionError = ConnectionError
|
2015-11-03 04:31:02 +00:00
|
|
|
Object.defineProperties(ConnectionError.prototype, {
|
|
|
|
name: {
|
2019-08-27 21:11:49 +00:00
|
|
|
get: function () { return 'ConnectionError' },
|
2015-11-03 04:31:02 +00:00
|
|
|
configurable: false
|
|
|
|
}
|
2019-08-27 21:11:49 +00:00
|
|
|
})
|
2014-07-09 18:40:17 +00:00
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
function AbandonedError (message) {
|
|
|
|
LDAPError.call(this, message, null, AbandonedError)
|
2014-07-09 18:40:17 +00:00
|
|
|
}
|
2019-08-27 21:11:49 +00:00
|
|
|
util.inherits(AbandonedError, LDAPError)
|
|
|
|
module.exports.AbandonedError = AbandonedError
|
2015-11-03 04:31:02 +00:00
|
|
|
Object.defineProperties(AbandonedError.prototype, {
|
|
|
|
name: {
|
2019-08-27 21:11:49 +00:00
|
|
|
get: function () { return 'AbandonedError' },
|
2015-11-03 04:31:02 +00:00
|
|
|
configurable: false
|
|
|
|
}
|
2019-08-27 21:11:49 +00:00
|
|
|
})
|
2014-07-09 18:40:17 +00:00
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
function TimeoutError (message) {
|
|
|
|
LDAPError.call(this, message, null, TimeoutError)
|
2014-07-09 18:40:17 +00:00
|
|
|
}
|
2019-08-27 21:11:49 +00:00
|
|
|
util.inherits(TimeoutError, LDAPError)
|
|
|
|
module.exports.TimeoutError = TimeoutError
|
2015-11-03 04:31:02 +00:00
|
|
|
Object.defineProperties(TimeoutError.prototype, {
|
|
|
|
name: {
|
2019-08-27 21:11:49 +00:00
|
|
|
get: function () { return 'TimeoutError' },
|
2015-11-03 04:31:02 +00:00
|
|
|
configurable: false
|
|
|
|
}
|
2019-08-27 21:11:49 +00:00
|
|
|
})
|