forked from datastax/cpp-driver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherror_response.cpp
215 lines (184 loc) · 6.83 KB
/
error_response.cpp
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
/*
Copyright (c) 2014-2016 DataStax
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#include "error_response.hpp"
#include "external_types.hpp"
#include "logger.hpp"
#include "serialization.hpp"
#include <iomanip>
#include <sstream>
extern "C" {
void cass_error_result_free(const CassErrorResult* error_result) {
error_result->dec_ref();
}
CassError cass_error_result_code(const CassErrorResult* error_result) {
return static_cast<CassError>(
CASS_ERROR(CASS_ERROR_SOURCE_SERVER, error_result->code()));
}
CassConsistency cass_error_result_consistency(const CassErrorResult* error_result) {
return error_result->consistency();
}
cass_int32_t cass_error_result_actual(const CassErrorResult* error_result) {
return error_result->received();
}
cass_int32_t cass_error_result_required(const CassErrorResult* error_result) {
return error_result->required();
}
cass_int32_t cass_error_result_num_failures(const CassErrorResult* error_result) {
return error_result->num_failures();
}
cass_bool_t cass_error_result_data_present(const CassErrorResult* error_result) {
return static_cast<cass_bool_t>(error_result->data_present());
}
CassWriteType cass_error_result_write_type(const CassErrorResult* error_result) {
return error_result->write_type();
}
CassError cass_error_result_keyspace(const CassErrorResult* error_result,
const char** keyspace,
size_t* keyspace_length) {
if (error_result->code() != CASS_ERROR_SERVER_ALREADY_EXISTS ||
error_result->code() != CASS_ERROR_SERVER_FUNCTION_FAILURE) {
return CASS_ERROR_LIB_INVALID_ERROR_RESULT_TYPE;
}
*keyspace = error_result->keyspace().data();
*keyspace_length = error_result->keyspace().size();
return CASS_OK;
}
CassError cass_error_result_table(const CassErrorResult* error_result,
const char** table,
size_t* table_length) {
if (error_result->code() != CASS_ERROR_SERVER_ALREADY_EXISTS) {
return CASS_ERROR_LIB_INVALID_ERROR_RESULT_TYPE;
}
*table = error_result->table().data();
*table_length = error_result->table().size();
return CASS_OK;
}
CassError cass_error_result_function(const CassErrorResult* error_result,
const char** function,
size_t* function_length) {
if (error_result->code() != CASS_ERROR_SERVER_FUNCTION_FAILURE) {
return CASS_ERROR_LIB_INVALID_ERROR_RESULT_TYPE;
}
*function = error_result->function().data();
*function_length = error_result->function().size();
return CASS_OK;
}
size_t cass_error_num_arg_types(const CassErrorResult* error_result) {
return error_result->arg_types().size();
}
CassError cass_error_result_arg_type(const CassErrorResult* error_result,
size_t index,
const char** arg_type,
size_t* arg_type_length) {
if (error_result->code() != CASS_ERROR_SERVER_FUNCTION_FAILURE) {
return CASS_ERROR_LIB_INVALID_ERROR_RESULT_TYPE;
}
if (index > error_result->arg_types().size()) {
return CASS_ERROR_LIB_INDEX_OUT_OF_BOUNDS;
}
cass::StringRef arg_type_ref = error_result->arg_types()[index];
*arg_type = arg_type_ref.data();
*arg_type_length = arg_type_ref.size();
return CASS_OK;
}
} // extern "C"
namespace cass {
std::string ErrorResponse::error_message() const {
std::ostringstream ss;
ss << "'" << message().to_string() << "'"
<< " (0x" << std::hex << std::uppercase << std::setw(8) << std::setfill('0')
<< CASS_ERROR(CASS_ERROR_SOURCE_SERVER, code()) << ")";
return ss.str();
}
bool ErrorResponse::decode(int version, char* buffer, size_t size) {
char* pos = decode_int32(buffer, code_);
pos = decode_string(pos, &message_);
switch (code_) {
case CQL_ERROR_UNAVAILABLE:
pos = decode_uint16(pos, cl_);
pos = decode_int32(pos, required_);
decode_int32(pos, received_);
break;
case CQL_ERROR_READ_TIMEOUT:
pos = decode_uint16(pos, cl_);
pos = decode_int32(pos, received_);
pos = decode_int32(pos, required_);
decode_byte(pos, data_present_);
break;
case CQL_ERROR_WRITE_TIMEOUT:
pos = decode_uint16(pos, cl_);
pos = decode_int32(pos, received_);
pos = decode_int32(pos, required_);
decode_write_type(pos);
break;
case CQL_ERROR_READ_FAILURE:
pos = decode_uint16(pos, cl_);
pos = decode_int32(pos, received_);
pos = decode_int32(pos, required_);
pos = decode_int32(pos, num_failures_);
decode_byte(pos, data_present_);
break;
case CQL_ERROR_FUNCTION_FAILURE:
pos = decode_string(pos, &keyspace_);
pos = decode_string(pos, &function_);
decode_stringlist(pos, arg_types_);
break;
case CQL_ERROR_WRITE_FAILURE:
pos = decode_uint16(pos, cl_);
pos = decode_int32(pos, received_);
pos = decode_int32(pos, required_);
pos = decode_int32(pos, num_failures_);
decode_write_type(pos);
break;
case CQL_ERROR_UNPREPARED:
decode_string(pos, &prepared_id_);
break;
case CQL_ERROR_ALREADY_EXISTS:
pos = decode_string(pos, &keyspace_);
pos = decode_string(pos, &table_);
break;
}
return true;
}
void ErrorResponse::decode_write_type(char* pos) {
StringRef write_type;
decode_string(pos, &write_type);
if (write_type == "SIMPLE") {
write_type_ = CASS_WRITE_TYPE_SIMPLE;
} else if(write_type == "BATCH") {
write_type_ = CASS_WRITE_TYPE_BATCH;
} else if(write_type == "UNLOGGED_BATCH") {
write_type_ = CASS_WRITE_TYPE_UNLOGGED_BATCH;
} else if(write_type == "COUNTER") {
write_type_ = CASS_WRITE_TYPE_COUNTER;
} else if(write_type == "BATCH_LOG") {
write_type_ = CASS_WRITE_TYPE_BATCH_LOG;
}
}
bool check_error_or_invalid_response(const std::string& prefix, uint8_t expected_opcode,
Response* response) {
if (response->opcode() == expected_opcode) {
return false;
}
std::ostringstream ss;
if (response->opcode() == CQL_OPCODE_ERROR) {
ss << prefix << ": Error response "
<< static_cast<ErrorResponse*>(response)->error_message();
} else {
ss << prefix << ": Unexpected opcode "
<< opcode_to_string(response->opcode());
}
LOG_ERROR("%s", ss.str().c_str());
return true;
}
} // namespace cass