Ohm-Management - Projektarbeit B-ME
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

index.js 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. // Copyright 2011 Mark Cavage, Inc. All rights reserved.
  2. var util = require('util');
  3. var assert = require('assert-plus');
  4. var LDAPResult = require('../messages').LDAPResult;
  5. ///--- Globals
  6. var CODES = require('./codes');
  7. var ERRORS = [];
  8. ///--- Error Base class
  9. function LDAPError(message, dn, caller) {
  10. if (Error.captureStackTrace)
  11. Error.captureStackTrace(this, caller || LDAPError);
  12. this.lde_message = message;
  13. this.lde_dn = dn;
  14. }
  15. util.inherits(LDAPError, Error);
  16. Object.defineProperties(LDAPError.prototype, {
  17. name: {
  18. get: function getName() { return 'LDAPError'; },
  19. configurable: false
  20. },
  21. code: {
  22. get: function getCode() { return CODES.LDAP_OTHER; },
  23. configurable: false
  24. },
  25. message: {
  26. get: function getMessage() {
  27. return this.lde_message || this.name;
  28. },
  29. configurable: false
  30. },
  31. dn: {
  32. get: function getDN() {
  33. return (this.lde_dn ? this.lde_dn.toString() : '');
  34. },
  35. configurable: false
  36. }
  37. });
  38. ///--- Exported API
  39. module.exports = {};
  40. module.exports.LDAPError = LDAPError;
  41. // Some whacky games here to make sure all the codes are exported
  42. Object.keys(CODES).forEach(function (code) {
  43. module.exports[code] = CODES[code];
  44. if (code === 'LDAP_SUCCESS')
  45. return;
  46. var err = '';
  47. var msg = '';
  48. var pieces = code.split('_').slice(1);
  49. for (var i = 0; i < pieces.length; i++) {
  50. var lc = pieces[i].toLowerCase();
  51. var key = lc.charAt(0).toUpperCase() + lc.slice(1);
  52. err += key;
  53. msg += key + ((i + 1) < pieces.length ? ' ' : '');
  54. }
  55. if (!/\w+Error$/.test(err))
  56. err += 'Error';
  57. // At this point LDAP_OPERATIONS_ERROR is now OperationsError in $err
  58. // and 'Operations Error' in $msg
  59. module.exports[err] = function (message, dn, caller) {
  60. LDAPError.call(this, message, dn, caller || module.exports[err]);
  61. };
  62. module.exports[err].constructor = module.exports[err];
  63. util.inherits(module.exports[err], LDAPError);
  64. Object.defineProperties(module.exports[err].prototype, {
  65. name: {
  66. get: function getName() { return err; },
  67. configurable: false
  68. },
  69. code: {
  70. get: function getCode() { return CODES[code]; },
  71. configurable: false
  72. }
  73. });
  74. ERRORS[CODES[code]] = {
  75. err: err,
  76. message: msg
  77. };
  78. });
  79. module.exports.getError = function (res) {
  80. assert.ok(res instanceof LDAPResult, 'res (LDAPResult) required');
  81. var errObj = ERRORS[res.status];
  82. var E = module.exports[errObj.err];
  83. return new E(res.errorMessage || errObj.message,
  84. res.matchedDN || null,
  85. module.exports.getError);
  86. };
  87. module.exports.getMessage = function (code) {
  88. assert.number(code, 'code (number) required');
  89. var errObj = ERRORS[code];
  90. return (errObj && errObj.message ? errObj.message : '');
  91. };
  92. ///--- Custom application errors
  93. function ConnectionError(message) {
  94. LDAPError.call(this, message, null, ConnectionError);
  95. }
  96. util.inherits(ConnectionError, LDAPError);
  97. module.exports.ConnectionError = ConnectionError;
  98. Object.defineProperties(ConnectionError.prototype, {
  99. name: {
  100. get: function () { return 'ConnectionError'; },
  101. configurable: false
  102. }
  103. });
  104. function AbandonedError(message) {
  105. LDAPError.call(this, message, null, AbandonedError);
  106. }
  107. util.inherits(AbandonedError, LDAPError);
  108. module.exports.AbandonedError = AbandonedError;
  109. Object.defineProperties(AbandonedError.prototype, {
  110. name: {
  111. get: function () { return 'AbandonedError'; },
  112. configurable: false
  113. }
  114. });
  115. function TimeoutError(message) {
  116. LDAPError.call(this, message, null, TimeoutError);
  117. }
  118. util.inherits(TimeoutError, LDAPError);
  119. module.exports.TimeoutError = TimeoutError;
  120. Object.defineProperties(TimeoutError.prototype, {
  121. name: {
  122. get: function () { return 'TimeoutError'; },
  123. configurable: false
  124. }
  125. });