lib/error.js
function LibrdKafkaError(e) {
  if (!(this instanceof LibrdKafkaError)) {
    return new LibrdKafkaError(e);
  }

  if (typeof e === 'number') {
    this.message = librdkafka.err2str(e);
    this.code = e;
    this.errno = e;
    if (e >= LibrdKafkaError.codes.ERR__END) {
      this.origin = 'local';
    } else {
      this.origin = 'kafka';
    }
    Error.captureStackTrace(this, this.constructor);
  } else if (!util.isError(e)) {
    // This is the better way
    this.message = e.message;
    this.code = e.code;
    this.errno = e.code;
    if (e.code >= LibrdKafkaError.codes.ERR__END) {
      this.origin = 'local';
    } else {
      this.origin = 'kafka';
    }
    Error.captureStackTrace(this, this.constructor);
  } else {
    var message = e.message;
    var parsedMessage = message.split(': ');

    var origin, msg;

    if (parsedMessage.length > 1) {
      origin = parsedMessage[0].toLowerCase();
      msg = parsedMessage[1].toLowerCase();
    } else {
      origin = 'unknown';
      msg = message.toLowerCase();
    }

    // special cases
    if (msg === 'consumer is disconnected' || msg === 'producer is disconnected') {
      this.origin = 'local';
      this.code = LibrdKafkaError.codes.ERR__STATE;
      this.errno = this.code;
      this.message = msg;
    } else {
      this.origin = origin;
      this.message = msg;
      this.code = typeof e.code === 'number' ? e.code : -1;
      this.errno = this.code;
      this.stack = e.stack;
    }

  }

}
function LibrdKafkaError(e) {
  if (!(this instanceof LibrdKafkaError)) {
    return new LibrdKafkaError(e);
  }

  if (typeof e === 'number') {
    this.message = librdkafka.err2str(e);
    this.code = e;
    this.errno = e;
    if (e >= LibrdKafkaError.codes.ERR__END) {
      this.origin = 'local';
    } else {
      this.origin = 'kafka';
    }
    Error.captureStackTrace(this, this.constructor);
  } else if (!util.isError(e)) {
    // This is the better way
    this.message = e.message;
    this.code = e.code;
    this.errno = e.code;
    if (e.code >= LibrdKafkaError.codes.ERR__END) {
      this.origin = 'local';
    } else {
      this.origin = 'kafka';
    }
    Error.captureStackTrace(this, this.constructor);
  } else {
    var message = e.message;
    var parsedMessage = message.split(': ');

    var origin, msg;

    if (parsedMessage.length > 1) {
      origin = parsedMessage[0].toLowerCase();
      msg = parsedMessage[1].toLowerCase();
    } else {
      origin = 'unknown';
      msg = message.toLowerCase();
    }

    // special cases
    if (msg === 'consumer is disconnected' || msg === 'producer is disconnected') {
      this.origin = 'local';
      this.code = LibrdKafkaError.codes.ERR__STATE;
      this.errno = this.code;
      this.message = msg;
    } else {
      this.origin = origin;
      this.message = msg;
      this.code = typeof e.code === 'number' ? e.code : -1;
      this.errno = this.code;
      this.stack = e.stack;
    }

  }

}
function LibrdKafkaError ( e ) { if ( ! ( this instanceof LibrdKafkaError ) ) { return new LibrdKafkaError ( e ) ; } if ( typeof e === 'number' ) { this . message = librdkafka . err2str ( e ) ; this . code = e ; this . errno = e ; if ( e >= LibrdKafkaError . codes . ERR__END ) { this . origin = 'local' ; } else { this . origin = 'kafka' ; } Error . captureStackTrace ( this , this . constructor ) ; } else if ( ! util . isError ( e ) ) { // This is the better way this . message = e . message ; this . code = e . code ; this . errno = e . code ; if ( e . code >= LibrdKafkaError . codes . ERR__END ) { this . origin = 'local' ; } else { this . origin = 'kafka' ; } Error . captureStackTrace ( this , this . constructor ) ; } else { var message = e . message ; var parsedMessage = message . split ( ': ' ) ; var origin , msg ; if ( parsedMessage . length > 1 ) { origin = parsedMessage [ 0 ] . toLowerCase ( ) ; msg = parsedMessage [ 1 ] . toLowerCase ( ) ; } else { origin = 'unknown' ; msg = message . toLowerCase ( ) ; } // special cases if ( msg === 'consumer is disconnected' || msg === 'producer is disconnected' ) { this . origin = 'local' ; this . code = LibrdKafkaError . codes . ERR__STATE ; this . errno = this . code ; this . message = msg ; } else { this . origin = origin ; this . message = msg ; this . code = typeof e . code === 'number' ? e . code : - 1 ; this . errno = this . code ; this . stack = e . stack ; } } }
Representation of a librdkafka error

This can be created by giving either another error
to piggy-back on. In this situation it tries to parse
the error string to figure out the intent. However, more usually,
it is constructed by an error object created by a C++ Baton.

@param {object|error} e - An object or error to wrap
@property {string} message - The error message
@property {number} code - The error code.
@property {string} origin - The origin, whether it is local or remote
@constructor
Representation of a librdkafka error