All files / confluent-kafka-javascript/lib/kafkajs _error.js

30.35% Statements 17/56
35.29% Branches 6/17
10% Functions 2/20
30.9% Lines 17/55

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 2991x                                       1x         1x         1x         1x         1x         1x               1x   1x       1x         1x   165x   1x     1x                                                                                                                                                                                                                                                                                                                                                                                                   1x                     1x   1x                                        
const LibrdKafkaError = require('../error');
 
/**
 * KafkaJSError represents an error when using the promisified interface.
 * @memberof KafkaJS
 */
class KafkaJSError extends Error {
    /**
     * This constructor is meant to be used by the library. Please see the members for more information on
     * what can be accessed from an error object.
     *
     * @param {Error | string} error an Error or a string describing the error.
     * @param {object} properties a set of optional error properties.
     * @param {boolean} [properties.retriable=false] whether the error is retriable. Applies only to the transactional producer
     * @param {boolean} [properties.fatal=false] whether the error is fatal. Applies only to the transactional producer.
     * @param {boolean} [properties.abortable=false] whether the error is abortable. Applies only to the transactional producer.
     * @param {string} [properties.stack] the stack trace of the error.
     * @param {number} [properties.code=LibrdKafkaError.codes.ERR_UNKNOWN] the error code.
     */
    constructor(e, { retriable = false, fatal = false, abortable = false, stack = null, code = LibrdKafkaError.codes.ERR_UNKNOWN } = {}) {
        super(e, {});
        /**
         * Name of the error.
         * @type {string}
         */
        this.name = 'KafkaJSError';
        /**
         * Message detailing the error.
         * @type {string}
         */
        this.message = e.message || e;
        /**
         * Whether the error is retriable (for transactional producer).
         * @type {boolean}
         */
        this.retriable = retriable;
        /**
         * Whether the error is fatal (for transactional producer).
         * @type {boolean}
         */
        this.fatal = fatal;
        /**
         * Whether the error is abortable (for transactional producer).
         * @type {boolean}
         */
        this.abortable = abortable;
        /**
         * The error code from Librdkafka.
         *
         * This field should be checked (as opposed to the type of the error) to determine what sort of an error this is.
         * @see {@link RdKafka.LibrdKafkaError.codes} For a list of error codes that can be returned.
         * @type {number}
         */
        this.code = code;
 
        if (stack) {
            /**
             * The stack trace of the error.
             */
            this.stack = stack;
        } else E{
            Error.captureStackTrace(this, this.constructor);
        }
 
        const errTypes = Object
            .keys(LibrdKafkaError.codes)
            .filter(k => LibrdKafkaError.codes[k] === this.code);
 
        Iif (errTypes.length !== 1) {
            this.type = LibrdKafkaError.codes.ERR_UNKNOWN;
        } else {
            this.type = errTypes[0];
        }
    }
}
 
/**
 * KafkaJSProtocolError represents an error that is caused when a Kafka Protocol RPC has an embedded error.
 * @extends KafkaJS.KafkaJSError
 * @memberof KafkaJS
 */
class KafkaJSProtocolError extends KafkaJSError {
    constructor() {
        super(...arguments);
        this.name = 'KafkaJSProtocolError';
    }
}
 
/**
 * KafkaJSOffsetOutOfRange represents the error raised when fetching from an offset out of range.
 * @extends KafkaJS.KafkaJSProtocolError
 * @memberof KafkaJS
 */
class KafkaJSOffsetOutOfRange extends KafkaJSProtocolError {
    constructor() {
        super(...arguments);
        this.name = 'KafkaJSOffsetOutOfRange';
    }
}
 
/**
 * KafkaJSConnectionError represents the error raised when a connection to a broker cannot be established or is broken unexpectedly.
 * @extends KafkaJS.KafkaJSError
 * @memberof KafkaJS
 */
class KafkaJSConnectionError extends KafkaJSError {
    constructor() {
        super(...arguments);
        this.name = 'KafkaJSConnectionError';
    }
}
 
/**
 * KafkaJSRequestTimeoutError represents the error raised on a timeout for one request.
 * @extends KafkaJS.KafkaJSError
 * @memberof KafkaJS
 */
class KafkaJSRequestTimeoutError extends KafkaJSError {
    constructor() {
        super(...arguments);
        this.name = 'KafkaJSRequestTimeoutError';
    }
}
 
/**
 * KafkaJSPartialMessageError represents the error raised when a response does not contain all expected information.
 * @extends KafkaJS.KafkaJSError
 * @memberof KafkaJS
 */
class KafkaJSPartialMessageError extends KafkaJSError {
    constructor() {
        super(...arguments);
        this.name = 'KafkaJSPartialMessageError';
    }
}
 
/**
 * KafkaJSSASLAuthenticationError represents an error raised when authentication fails.
 * @extends KafkaJS.KafkaJSError
 * @memberof KafkaJS
 */
class KafkaJSSASLAuthenticationError extends KafkaJSError {
    constructor() {
        super(...arguments);
        this.name = 'KafkaJSSASLAuthenticationError';
    }
}
 
/**
 * KafkaJSGroupCoordinatorNotFound represents an error raised when the group coordinator is not found.
 * @extends KafkaJS.KafkaJSError
 * @memberof KafkaJS
 */
class KafkaJSGroupCoordinatorNotFound extends KafkaJSError {
    constructor() {
        super(...arguments);
        this.name = 'KafkaJSGroupCoordinatorNotFound';
    }
}
 
/**
 * KafkaJSNotImplemented represents an error raised when a feature is not implemented for this particular client.
 * @extends KafkaJS.KafkaJSError
 * @memberof KafkaJS
 */
class KafkaJSNotImplemented extends KafkaJSError {
    constructor() {
        super(...arguments);
        this.name = 'KafkaJSNotImplemented';
    }
}
 
/**
 * KafkaJSTimeout represents an error raised when a timeout for an operation occurs (including retries).
 * @extends KafkaJS.KafkaJSError
 * @memberof KafkaJS
 */
class KafkaJSTimeout extends KafkaJSError {
    constructor() {
        super(...arguments);
        this.name = 'KafkaJSTimeout';
    }
}
 
/**
 * KafkaJSCreateTopicError represents an error raised by the createTopics method for one topic.
 * @extends KafkaJS.KafkaJSError
 * @memberof KafkaJS
 */
class KafkaJSCreateTopicError extends KafkaJSProtocolError {
    constructor(e, topicName, properties) {
      super(e, properties);
      this.topic = topicName;
      this.name = 'KafkaJSCreateTopicError';
    }
}
 
/**
 * KafkaJSDeleteGroupsError represents an error raised by the deleteGroups method.
 * @extends KafkaJS.KafkaJSError
 * @memberof KafkaJS
 */
class KafkaJSDeleteGroupsError extends KafkaJSError {
    constructor(e, groups) {
      super(e);
      this.groups = groups || [];
      this.name = 'KafkaJSDeleteGroupsError';
    }
}
 
/**
 * KafkaJSDeleteTopicRecordsError represents an error raised by the deleteTopicRecords method.
 * @extends KafkaJS.KafkaJSError
 * @memberof KafkaJS
 */
class KafkaJSDeleteTopicRecordsError extends KafkaJSError {
    constructor({ partitions }) {
      /*
       * This error is retriable if all the errors were retriable
       */
      const retriable = partitions
        .filter(({ error }) => error !== null)
        .every(({ error }) => error.retriable === true);
 
      super('Error while deleting records', { retriable });
      this.name = 'KafkaJSDeleteTopicRecordsError';
      this.partitions = partitions;
    }
}
 
/**
 * KafkaJSAggregateError represents an error raised when multiple errors occur at once.
 * @extends Error
 * @memberof KafkaJS
 */
class KafkaJSAggregateError extends Error {
    constructor(message, errors) {
        super(message);
        /**
         * A list of errors that are part of the aggregate error.
         * @type {Array<KafkaJS.KafkaJSError>}
         */
        this.errors = errors;
        this.name = 'KafkaJSAggregateError';
    }
}
 
/**
 * KafkaJSNoBrokerAvailableError represents an error raised when no broker is available for the operation.
 * @extends KafkaJS.KafkaJSError
 * @memberof KafkaJS
 */
class KafkaJSNoBrokerAvailableError extends KafkaJSError {
    constructor() {
        super(...arguments);
        this.name = 'KafkaJSNoBrokerAvailableError';
    }
}
 
/**
 * @function isRebalancing
 * @param {KafkaJS.KafkaJSError} e
 * @returns {boolean} Returns whether the error is a rebalancing error.
 * @memberof KafkaJS
 */
const isRebalancing = e =>
    e.type === 'REBALANCE_IN_PROGRESS' ||
    e.type === 'NOT_COORDINATOR_FOR_GROUP' ||
    e.type === 'ILLEGAL_GENERATION';
 
/**
 * @function isKafkaJSError
 * @param {any} e
 * @returns {boolean} Returns whether the error is a KafkaJSError.
 * @memberof KafkaJS
 */
const isKafkaJSError = e => e instanceof KafkaJSError;
 
module.exports = {
    KafkaJSError,
    KafkaJSPartialMessageError,
    KafkaJSProtocolError,
    KafkaJSConnectionError,
    KafkaJSRequestTimeoutError,
    KafkaJSSASLAuthenticationError,
    KafkaJSOffsetOutOfRange,
    KafkaJSGroupCoordinatorNotFound,
    KafkaJSNotImplemented,
    KafkaJSTimeout,
    KafkaJSCreateTopicError,
    KafkaJSDeleteGroupsError,
    KafkaJSDeleteTopicRecordsError,
    KafkaJSAggregateError,
    KafkaJSNoBrokerAvailableError,
    isRebalancing,
    isKafkaJSError,
    ErrorCodes: LibrdKafkaError.codes,
};