Skip to content

Commit 898ac2a

Browse files
authored
Merge pull request #164 from itrofimow/exceptions_hierarchy
introduce exceptions hierarchy
2 parents b2e5da9 + 293981d commit 898ac2a

24 files changed

+147
-102
lines changed

clickhouse/base/compressed.cpp

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include "compressed.h"
22
#include "wire_format.h"
33
#include "output.h"
4+
#include "../exceptions.h"
45

56
#include <cityhash/city.h>
67
#include <lz4/lz4.h>
@@ -30,7 +31,7 @@ CompressedInput::~CompressedInput() {
3031
#else
3132
if (!std::uncaught_exceptions()) {
3233
#endif
33-
throw std::runtime_error("some data was not read");
34+
throw LZ4Error("some data was not read");
3435
}
3536
}
3637
}
@@ -59,7 +60,7 @@ bool CompressedInput::Decompress() {
5960
}
6061

6162
if (method != COMPRESSION_METHOD) {
62-
throw std::runtime_error("unsupported compression method " + std::to_string(int(method)));
63+
throw LZ4Error("unsupported compression method " + std::to_string(int(method)));
6364
} else {
6465
if (!WireFormat::ReadFixed(*input_, &compressed)) {
6566
return false;
@@ -69,7 +70,7 @@ bool CompressedInput::Decompress() {
6970
}
7071

7172
if (compressed > DBMS_MAX_COMPRESSED_SIZE) {
72-
throw std::runtime_error("compressed data too big");
73+
throw LZ4Error("compressed data too big");
7374
}
7475

7576
Buffer tmp(compressed);
@@ -87,14 +88,14 @@ bool CompressedInput::Decompress() {
8788
return false;
8889
} else {
8990
if (hash != CityHash128((const char*)tmp.data(), compressed)) {
90-
throw std::runtime_error("data was corrupted");
91+
throw LZ4Error("data was corrupted");
9192
}
9293
}
9394

9495
data_ = Buffer(original);
9596

9697
if (LZ4_decompress_safe((const char*)tmp.data() + HEADER_SIZE, (char*)data_.data(), compressed - HEADER_SIZE, original) < 0) {
97-
throw std::runtime_error("can't decompress data");
98+
throw LZ4Error("can't decompress data");
9899
} else {
99100
mem_.Reset(data_.data(), original);
100101
}
@@ -143,7 +144,7 @@ void CompressedOutput::Compress(const void * data, size_t len) {
143144
len,
144145
static_cast<int>(compressed_buffer_.size() - HEADER_SIZE));
145146
if (compressed_size <= 0)
146-
throw std::runtime_error("Failed to compress chunk of " + std::to_string(len) + " bytes, "
147+
throw LZ4Error("Failed to compress chunk of " + std::to_string(len) + " bytes, "
147148
"LZ4 error: " + std::to_string(compressed_size));
148149

149150
{
@@ -165,7 +166,7 @@ void CompressedOutput::Compress(const void * data, size_t len) {
165166
void CompressedOutput::PreallocateCompressBuffer(size_t input_size) {
166167
const auto estimated_compressed_buffer_size = LZ4_compressBound(static_cast<int>(input_size));
167168
if (estimated_compressed_buffer_size <= 0)
168-
throw std::runtime_error("Failed to estimate compressed buffer size, LZ4 error: " + std::to_string(estimated_compressed_buffer_size));
169+
throw LZ4Error("Failed to estimate compressed buffer size, LZ4 error: " + std::to_string(estimated_compressed_buffer_size));
169170

170171
compressed_buffer_.resize(estimated_compressed_buffer_size + HEADER_SIZE + EXTRA_COMPRESS_BUFFER_SIZE);
171172
}

clickhouse/base/sslsocket.cpp

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include "sslsocket.h"
22
#include "../client.h"
3+
#include "../exceptions.h"
34

45
#include <stdexcept>
56

@@ -45,7 +46,7 @@ void throwSSLError(SSL * ssl, int error, const char * /*location*/, const char *
4546
// << "\n\t last err: " << ERR_peek_last_error()
4647
// << std::endl;
4748

48-
throw std::runtime_error(prefix + std::to_string(error) + " : " + reason_str);
49+
throw clickhouse::OpenSSLError(prefix + std::to_string(error) + " : " + reason_str);
4950
}
5051

5152
void configureSSL(const clickhouse::SSLParams::ConfigurationType & configuration, SSL * ssl, SSL_CTX * context = nullptr) {
@@ -75,13 +76,13 @@ void configureSSL(const clickhouse::SSLParams::ConfigurationType & configuration
7576
else if (err == 0)
7677
throwSSLError(ssl, SSL_ERROR_NONE, nullptr, nullptr, "Failed to configure OpenSSL with command '" + kv.first + "' ");
7778
else if (err == 1 && value_present)
78-
throw std::runtime_error("Failed to configure OpenSSL: command '" + kv.first + "' needs no value");
79+
throw clickhouse::OpenSSLError("Failed to configure OpenSSL: command '" + kv.first + "' needs no value");
7980
else if (err == -2)
80-
throw std::runtime_error("Failed to cofigure OpenSSL: unknown command '" + kv.first + "'");
81+
throw clickhouse::OpenSSLError("Failed to cofigure OpenSSL: unknown command '" + kv.first + "'");
8182
else if (err == -3)
82-
throw std::runtime_error("Failed to cofigure OpenSSL: command '" + kv.first + "' requires a value");
83+
throw clickhouse::OpenSSLError("Failed to cofigure OpenSSL: command '" + kv.first + "' requires a value");
8384
else
84-
throw std::runtime_error("Failed to cofigure OpenSSL: command '" + kv.first + "' unknown error: " + std::to_string(err));
85+
throw clickhouse::OpenSSLError("Failed to cofigure OpenSSL: command '" + kv.first + "' unknown error: " + std::to_string(err));
8586
}
8687
}
8788

@@ -104,7 +105,7 @@ SSL_CTX * prepareSSLContext(const clickhouse::SSLParams & context_params) {
104105
std::unique_ptr<SSL_CTX, decltype(&SSL_CTX_free)> ctx(SSL_CTX_new(method), &SSL_CTX_free);
105106

106107
if (!ctx)
107-
throw std::runtime_error("Failed to initialize SSL context");
108+
throw clickhouse::OpenSSLError("Failed to initialize SSL context");
108109

109110
#define HANDLE_SSL_CTX_ERROR(statement) do { \
110111
if (const auto ret_code = (statement); !ret_code) \
@@ -204,7 +205,7 @@ SSLSocket::SSLSocket(const NetworkAddress& addr, const SSLParams & ssl_params,
204205
{
205206
auto ssl = ssl_.get();
206207
if (!ssl)
207-
throw std::runtime_error("Failed to create SSL instance");
208+
throw clickhouse::OpenSSLError("Failed to create SSL instance");
208209

209210
std::unique_ptr<ASN1_OCTET_STRING, decltype(&ASN1_OCTET_STRING_free)> ip_addr(a2i_IPADDRESS(addr.Host().c_str()), &ASN1_OCTET_STRING_free);
210211

@@ -228,7 +229,7 @@ SSLSocket::SSLSocket(const NetworkAddress& addr, const SSLParams & ssl_params,
228229

229230
if (const auto verify_result = SSL_get_verify_result(ssl); !ssl_params.skip_verification && verify_result != X509_V_OK) {
230231
auto error_message = X509_verify_cert_error_string(verify_result);
231-
throw std::runtime_error("Failed to verify SSL connection, X509_v error: "
232+
throw clickhouse::OpenSSLError("Failed to verify SSL connection, X509_v error: "
232233
+ std::to_string(verify_result)
233234
+ " " + error_message
234235
+ "\nServer certificate: " + getCertificateInfo(SSL_get_peer_certificate(ssl)));

clickhouse/base/wire_format.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
#include "input.h"
44
#include "output.h"
55

6+
#include "../exceptions.h"
7+
68
#include <stdexcept>
79

810
namespace {
@@ -38,7 +40,7 @@ void WireFormat::WriteAll(OutputStream& output, const void* buf, size_t len) {
3840
}
3941

4042
if (len) {
41-
throw std::runtime_error("Failed to write " + std::to_string(original_len)
43+
throw Error("Failed to write " + std::to_string(original_len)
4244
+ " bytes, only written " + std::to_string(original_len - len));
4345
}
4446
}

clickhouse/block.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#include "block.h"
22

3+
#include "exceptions.h"
4+
35
#include <stdexcept>
46

57
namespace clickhouse {
@@ -54,7 +56,7 @@ void Block::AppendColumn(const std::string& name, const ColumnRef& col) {
5456
if (columns_.empty()) {
5557
rows_ = col->Size();
5658
} else if (col->Size() != rows_) {
57-
throw std::runtime_error("all columns in block must have same count of rows. Name: ["+name+"], rows: ["+std::to_string(rows_)+"], columns: [" + std::to_string(col->Size())+"]");
59+
throw ValidationError("all columns in block must have same count of rows. Name: ["+name+"], rows: ["+std::to_string(rows_)+"], columns: [" + std::to_string(col->Size())+"]");
5860
}
5961

6062
columns_.push_back(ColumnItem{name, col});
@@ -86,7 +88,7 @@ size_t Block::RefreshRowCount()
8688
if (idx == 0UL)
8789
rows = col->Size();
8890
else if (rows != col->Size())
89-
throw std::runtime_error("all columns in block must have same count of rows. Name: ["+name+"], rows: ["+std::to_string(rows)+"], columns: [" + std::to_string(col->Size())+"]");
91+
throw ValidationError("all columns in block must have same count of rows. Name: ["+name+"], rows: ["+std::to_string(rows)+"], columns: [" + std::to_string(col->Size())+"]");
9092
}
9193

9294
rows_ = rows;

clickhouse/client.cpp

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ ClientOptions& ClientOptions::SetSSLOptions(ClientOptions::SSLOptions options)
8787
return *this;
8888
#else
8989
(void)options;
90-
throw std::runtime_error("Library was built with no SSL support");
90+
throw OpenSSLError("Library was built with no SSL support");
9191
#endif
9292
}
9393

@@ -152,7 +152,7 @@ class Client::Impl {
152152
private:
153153
/// In case of network errors tries to reconnect to server and
154154
/// call fuc several times.
155-
void RetryGuard(std::function<void()> fuc);
155+
void RetryGuard(std::function<void()> func);
156156

157157
private:
158158
class EnsureNull {
@@ -187,10 +187,6 @@ class Client::Impl {
187187
std::unique_ptr<OutputStream> output_;
188188
std::unique_ptr<SocketBase> socket_;
189189

190-
#if defined(WITH_OPENSSL)
191-
std::unique_ptr<SSLContext> ssl_context_;
192-
#endif
193-
194190
ServerInfo server_info_;
195191
};
196192

@@ -282,7 +278,7 @@ void Client::Impl::Insert(const std::string& table_name, const std::string& quer
282278
bool ret = ReceivePacket(&server_packet);
283279

284280
if (!ret) {
285-
throw std::runtime_error("fail to receive data packet");
281+
throw ProtocolError("fail to receive data packet");
286282
}
287283
if (server_packet == ServerCodes::Data) {
288284
break;
@@ -306,7 +302,7 @@ void Client::Impl::Insert(const std::string& table_name, const std::string& quer
306302

307303
if (eos_packet != ServerCodes::EndOfStream && eos_packet != ServerCodes::Exception
308304
&& eos_packet != ServerCodes::Log && options_.rethrow_exceptions) {
309-
throw std::runtime_error(std::string{"unexpected packet from server while receiving end of query, expected (expected Exception, EndOfStream or Log, got: "}
305+
throw ProtocolError(std::string{"unexpected packet from server while receiving end of query, expected (expected Exception, EndOfStream or Log, got: "}
310306
+ (eos_packet ? std::to_string(eos_packet) : "nothing") + ")");
311307
}
312308
}
@@ -319,15 +315,15 @@ void Client::Impl::Ping() {
319315
const bool ret = ReceivePacket(&server_packet);
320316

321317
if (!ret || server_packet != ServerCodes::Pong) {
322-
throw std::runtime_error("fail to ping server");
318+
throw ProtocolError("fail to ping server");
323319
}
324320
}
325321

326322
void Client::Impl::ResetConnection() {
327323
InitializeStreams(socket_factory_->connect(options_));
328324

329325
if (!Handshake()) {
330-
throw std::runtime_error("fail to connect to " + options_.host);
326+
throw ProtocolError("fail to connect to " + options_.host);
331327
}
332328
}
333329

@@ -358,7 +354,7 @@ bool Client::Impl::ReceivePacket(uint64_t* server_packet) {
358354
switch (packet_type) {
359355
case ServerCodes::Data: {
360356
if (!ReceiveData()) {
361-
throw std::runtime_error("can't read data packet from input stream");
357+
throw ProtocolError("can't read data packet from input stream");
362358
}
363359
return true;
364360
}
@@ -431,7 +427,7 @@ bool Client::Impl::ReceivePacket(uint64_t* server_packet) {
431427
}
432428

433429
default:
434-
throw std::runtime_error("unimplemented " + std::to_string((int)packet_type));
430+
throw UnimplementedError("unimplemented " + std::to_string((int)packet_type));
435431
break;
436432
}
437433

@@ -489,12 +485,12 @@ bool Client::Impl::ReadBlock(InputStream& input, Block* block) {
489485

490486
if (ColumnRef col = CreateColumnByType(type, create_column_settings)) {
491487
if (num_rows && !col->Load(&input, num_rows)) {
492-
throw std::runtime_error("can't load column '" + name + "' of type " + type);
488+
throw ProtocolError("can't load column '" + name + "' of type " + type);
493489
}
494490

495491
block->AppendColumn(name, col);
496492
} else {
497-
throw std::runtime_error(std::string("unsupported column type: ") + type);
493+
throw UnimplementedError(std::string("unsupported column type: ") + type);
498494
}
499495
}
500496

@@ -573,7 +569,7 @@ bool Client::Impl::ReceiveException(bool rethrow) {
573569
}
574570

575571
if (rethrow || options_.rethrow_exceptions) {
576-
throw ServerException(std::move(e));
572+
throw ServerError(std::move(e));
577573
}
578574

579575
return exception_received;
@@ -668,8 +664,8 @@ void Client::Impl::SendData(const Block& block) {
668664
if (compression_ == CompressionState::Enable) {
669665
assert(options_.compression_method == CompressionMethod::LZ4);
670666

671-
std::unique_ptr<OutputStream> compressed_ouput = std::make_unique<CompressedOutput>(output_.get(), options_.max_compression_chunk_size);
672-
BufferedOutput buffered(std::move(compressed_ouput), options_.max_compression_chunk_size);
667+
std::unique_ptr<OutputStream> compressed_output = std::make_unique<CompressedOutput>(output_.get(), options_.max_compression_chunk_size);
668+
BufferedOutput buffered(std::move(compressed_output), options_.max_compression_chunk_size);
673669

674670
WriteBlock(block, buffered);
675671
} else {
@@ -794,19 +790,19 @@ void Client::Execute(const Query& query) {
794790
}
795791

796792
void Client::Select(const std::string& query, SelectCallback cb) {
797-
Execute(Query(query).OnData(cb));
793+
Execute(Query(query).OnData(std::move(cb)));
798794
}
799795

800796
void Client::Select(const std::string& query, const std::string& query_id, SelectCallback cb) {
801-
Execute(Query(query, query_id).OnData(cb));
797+
Execute(Query(query, query_id).OnData(std::move(cb)));
802798
}
803799

804800
void Client::SelectCancelable(const std::string& query, SelectCancelableCallback cb) {
805-
Execute(Query(query).OnDataCancelable(cb));
801+
Execute(Query(query).OnDataCancelable(std::move(cb)));
806802
}
807803

808804
void Client::SelectCancelable(const std::string& query, const std::string& query_id, SelectCancelableCallback cb) {
809-
Execute(Query(query, query_id).OnDataCancelable(cb));
805+
Execute(Query(query, query_id).OnDataCancelable(std::move(cb)));
810806
}
811807

812808
void Client::Select(const Query& query) {

clickhouse/columns/array.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ ColumnArray::ColumnArray(ColumnRef data)
1313

1414
void ColumnArray::AppendAsColumn(ColumnRef array) {
1515
if (!data_->Type()->IsEqual(array->Type())) {
16-
throw std::runtime_error(
16+
throw ValidationError(
1717
"can't append column of type " + array->Type()->GetName() + " "
1818
"to column type " + data_->Type()->GetName());
1919
}

clickhouse/columns/column.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
#include "../types/types.h"
44
#include "../columns/itemview.h"
5+
#include "../exceptions.h"
56

67
#include <memory>
78
#include <stdexcept>
@@ -61,7 +62,7 @@ class Column : public std::enable_shared_from_this<Column> {
6162
/// Get a view on raw item data if it is supported by column, will throw an exception if index is out of range.
6263
/// Please note that view is invalidated once column items are added or deleted, column is loaded from strean or destroyed.
6364
virtual ItemView GetItem(size_t) const {
64-
throw std::runtime_error("GetItem() is not supported for column of " + type_->GetName());
65+
throw UnimplementedError("GetItem() is not supported for column of " + type_->GetName());
6566
}
6667

6768
friend void swap(Column& left, Column& right) {

clickhouse/columns/date.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ ItemView ColumnDateTime64::GetItem(size_t index) const {
184184
void ColumnDateTime64::Swap(Column& other) {
185185
auto& col = dynamic_cast<ColumnDateTime64&>(other);
186186
if (col.GetPrecision() != GetPrecision()) {
187-
throw std::runtime_error("Can't swap DateTime64 columns when precisions are not the same: "
187+
throw ValidationError("Can't swap DateTime64 columns when precisions are not the same: "
188188
+ std::to_string(GetPrecision()) + "(this) != " + std::to_string(col.GetPrecision()) + "(that)");
189189
}
190190

clickhouse/columns/decimal.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -156,21 +156,21 @@ void ColumnDecimal::Append(const std::string& value) {
156156
} else if (*c >= '0' && *c <= '9') {
157157
if (mulOverflow(int_value, 10, &int_value) ||
158158
addOverflow(int_value, *c - '0', &int_value)) {
159-
throw std::runtime_error("value is too big for 128-bit integer");
159+
throw AssertionError("value is too big for 128-bit integer");
160160
}
161161
} else {
162-
throw std::runtime_error(std::string("unexpected symbol '") + (*c) + "' in decimal value");
162+
throw ValidationError(std::string("unexpected symbol '") + (*c) + "' in decimal value");
163163
}
164164
++c;
165165
}
166166

167167
if (c != end) {
168-
throw std::runtime_error("unexpected symbol '-' in decimal value");
168+
throw ValidationError("unexpected symbol '-' in decimal value");
169169
}
170170

171171
while (zeros) {
172172
if (mulOverflow(int_value, 10, &int_value)) {
173-
throw std::runtime_error("value is too big for 128-bit integer");
173+
throw AssertionError("value is too big for 128-bit integer");
174174
}
175175
--zeros;
176176
}
@@ -187,7 +187,7 @@ Int128 ColumnDecimal::At(size_t i) const {
187187
case Type::Int128:
188188
return data_->As<ColumnInt128>()->At(i);
189189
default:
190-
throw std::runtime_error("Invalid data_ column type in ColumnDecimal");
190+
throw ValidationError("Invalid data_ column type in ColumnDecimal");
191191
}
192192
}
193193

0 commit comments

Comments
 (0)