UNPKG

6.02 kBJavaScriptView Raw
1/*
2 * Copyright DataStax, Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16'use strict';
17const util = require('util');
18/**
19 * Contains the error classes exposed by the driver.
20 * @module errors
21 */
22
23/**
24 * Base Error
25 * @private
26 */
27function DriverError (message) {
28 Error.call(this, message);
29 Error.captureStackTrace(this, this.constructor);
30 this.name = this.constructor.name;
31 this.info = 'Cassandra Driver Error';
32 // Explicitly set the message property as the Error.call() doesn't set the property on v8
33 this.message = message;
34}
35
36util.inherits(DriverError, Error);
37
38/**
39 * Represents an error when a query cannot be performed because no host is available or could be reached by the driver.
40 * @param {Object} innerErrors An object map containing the error per host tried
41 * @param {String} [message]
42 * @constructor
43 */
44function NoHostAvailableError(innerErrors, message) {
45 DriverError.call(this, message);
46 this.innerErrors = innerErrors;
47 this.info = 'Represents an error when a query cannot be performed because no host is available or could be reached by the driver.';
48 if (!message) {
49 this.message = 'All host(s) tried for query failed.';
50 if (innerErrors) {
51 const hostList = Object.keys(innerErrors);
52 if (hostList.length > 0) {
53 const host = hostList[0];
54 this.message += util.format(' First host tried, %s: %s. See innerErrors.', host, innerErrors[host]);
55 }
56 }
57 }
58}
59
60util.inherits(NoHostAvailableError, DriverError);
61
62/**
63 * Represents an error message from the server
64 * @param {Number} code Cassandra exception code
65 * @param {String} message
66 * @constructor
67 */
68function ResponseError(code, message) {
69 DriverError.call(this, message);
70 /**
71 * The error code as defined in [responseErrorCodes]{@link module:types~responseErrorCodes}.
72 * @type {Number}
73 */
74 this.code = code;
75 this.info = 'Represents an error message from the server';
76}
77
78util.inherits(ResponseError, DriverError);
79
80/**
81 * Represents a bug inside the driver or in a Cassandra host.
82 * @param {String} message
83 * @constructor
84 */
85function DriverInternalError(message) {
86 DriverError.call(this, message);
87 this.info = 'Represents a bug inside the driver or in a Cassandra host.';
88}
89
90util.inherits(DriverInternalError, DriverError);
91
92/**
93 * Represents an error when trying to authenticate with auth-enabled host
94 * @param {String} message
95 * @constructor
96 */
97function AuthenticationError(message) {
98 DriverError.call(this, message);
99 this.info = 'Represents an authentication error from the driver or from a Cassandra node.';
100}
101
102util.inherits(AuthenticationError, DriverError);
103
104/**
105 * Represents an error that is raised when one of the arguments provided to a method is not valid
106 * @param {String} message
107 * @constructor
108 */
109function ArgumentError(message) {
110 DriverError.call(this, message);
111 this.info = 'Represents an error that is raised when one of the arguments provided to a method is not valid.';
112}
113
114util.inherits(ArgumentError, DriverError);
115
116/**
117 * Represents a client-side error that is raised when the client didn't hear back from the server within
118 * {@link ClientOptions.socketOptions.readTimeout}.
119 * @param {String} message The error message.
120 * @param {String} [host] Address of the server host that caused the operation to time out.
121 * @constructor
122 */
123function OperationTimedOutError(message, host) {
124 DriverError.call(this, message, this.constructor);
125 this.info = 'Represents a client-side error that is raised when the client did not hear back from the server ' +
126 'within socketOptions.readTimeout';
127
128 /**
129 * When defined, it gets the address of the host that caused the operation to time out.
130 * @type {String|undefined}
131 */
132 this.host = host;
133}
134
135util.inherits(OperationTimedOutError, DriverError);
136
137/**
138 * Represents an error that is raised when a feature is not supported in the driver or in the current Cassandra version.
139 * @param message
140 * @constructor
141 */
142function NotSupportedError(message) {
143 DriverError.call(this, message, this.constructor);
144 this.info = 'Represents a feature that is not supported in the driver or in the Cassandra version.';
145}
146
147util.inherits(NotSupportedError, DriverError);
148
149/**
150 * Represents a client-side error indicating that all connections to a certain host have reached
151 * the maximum amount of in-flight requests supported.
152 * @param {String} address
153 * @param {Number} maxRequestsPerConnection
154 * @param {Number} connectionLength
155 * @constructor
156 */
157function BusyConnectionError(address, maxRequestsPerConnection, connectionLength) {
158 const message = util.format('All connections to host %s are busy, %d requests are in-flight on %s',
159 address, maxRequestsPerConnection, connectionLength === 1 ? 'a single connection': 'each connection');
160 DriverError.call(this, message, this.constructor);
161 this.info = 'Represents a client-side error indicating that all connections to a certain host have reached ' +
162 'the maximum amount of in-flight requests supported (pooling.maxRequestsPerConnection)';
163}
164
165util.inherits(BusyConnectionError, DriverError);
166
167exports.ArgumentError = ArgumentError;
168exports.AuthenticationError = AuthenticationError;
169exports.BusyConnectionError = BusyConnectionError;
170exports.DriverError = DriverError;
171exports.OperationTimedOutError = OperationTimedOutError;
172exports.DriverInternalError = DriverInternalError;
173exports.NoHostAvailableError = NoHostAvailableError;
174exports.NotSupportedError = NotSupportedError;
175exports.ResponseError = ResponseError;
\No newline at end of file