Skip to content

Commit 6c86956

Browse files
committed
Added ThrowOnError helper for TStatus
1 parent 5bf7224 commit 6c86956

30 files changed

+89
-175
lines changed

examples/basic_example/basic_example.cpp

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,25 +8,7 @@
88

99
using namespace NYdb;
1010
using namespace NYdb::NQuery;
11-
12-
class TYdbErrorException : public yexception {
13-
public:
14-
TYdbErrorException(const TStatus& status)
15-
: Status(status) {}
16-
17-
TStatus Status;
18-
};
19-
20-
static void ThrowOnError(const TStatus& status) {
21-
if (!status.IsSuccess()) {
22-
throw TYdbErrorException(status) << status;
23-
}
24-
}
25-
26-
static void PrintStatus(const TStatus& status) {
27-
std::cerr << "Status: " << ToString(status.GetStatus()) << std::endl;
28-
std::cerr << status.GetIssues().ToString();
29-
}
11+
using namespace NYdb::NStatusHelpers;
3012

3113
template <class T>
3214
std::string OptionalToString(const std::optional<T>& opt) {
@@ -502,8 +484,7 @@ bool Run(const TDriver& driver) {
502484
DropTables(client);
503485
}
504486
catch (const TYdbErrorException& e) {
505-
std::cerr << "Execution failed due to fatal error:" << std::endl;
506-
PrintStatus(e.Status);
487+
std::cerr << "Execution failed due to fatal error: " << e.what() << std::endl;
507488
return false;
508489
}
509490

examples/pagination/pagination.cpp

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,28 +7,10 @@
77

88
using namespace NYdb;
99
using namespace NYdb::NTable;
10+
using namespace NYdb::NStatusHelpers;
1011

1112
const uint32_t MaxPages = 10;
1213

13-
class TYdbErrorException : public yexception {
14-
public:
15-
TYdbErrorException(const TStatus& status)
16-
: Status(status) {}
17-
18-
TStatus Status;
19-
};
20-
21-
static void ThrowOnError(const TStatus& status) {
22-
if (!status.IsSuccess()) {
23-
throw TYdbErrorException(status) << status;
24-
}
25-
}
26-
27-
static void PrintStatus(const TStatus& status) {
28-
std::cerr << "Status: " << ToString(status.GetStatus()) << std::endl;
29-
std::cerr << status.GetIssues().ToString();
30-
}
31-
3214
static std::string JoinPath(const std::string& basePath, const std::string& path) {
3315
if (basePath.empty()) {
3416
return path;
@@ -184,8 +166,7 @@ bool Run(const TDriver& driver, const std::string& path) {
184166
}
185167
}
186168
catch (const TYdbErrorException& e) {
187-
std::cerr << "Execution failed due to fatal error:" << std::endl;
188-
PrintStatus(e.Status);
169+
std::cerr << "Execution failed due to fatal error: " << e.what() << std::endl;
189170
return false;
190171
}
191172

examples/secondary_index/main.cpp

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

33
using namespace NLastGetopt;
44
using namespace NYdb;
5+
using namespace NYdb::NStatusHelpers;
56

67
////////////////////////////////////////////////////////////////////////////////
78

@@ -61,7 +62,7 @@ int main(int argc, char** argv) {
6162
return RunDeleteSeries(driver, prefix, argc, argv);
6263
}
6364
} catch (const TYdbErrorException& e) {
64-
std::cerr << "Execution failed: " << e << std::endl;
65+
std::cerr << "Execution failed: " << e.what() << std::endl;
6566
return 1;
6667
}
6768

examples/secondary_index/secondary_index.h

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -41,33 +41,6 @@ std::string JoinPath(const std::string& prefix, const std::string& path);
4141

4242
////////////////////////////////////////////////////////////////////////////////
4343

44-
class TYdbErrorException : public yexception {
45-
public:
46-
TYdbErrorException(NYdb::TStatus status)
47-
: Status(std::move(status))
48-
{ }
49-
50-
friend std::ostream& operator<<(std::ostream& out, const TYdbErrorException& e) {
51-
out << "Status: " << ToString(e.Status.GetStatus());
52-
if (e.Status.GetIssues()) {
53-
out << std::endl;
54-
out << e.Status.GetIssues().ToString();
55-
}
56-
return out;
57-
}
58-
59-
private:
60-
NYdb::TStatus Status;
61-
};
62-
63-
inline void ThrowOnError(NYdb::TStatus status) {
64-
if (!status.IsSuccess()) {
65-
throw TYdbErrorException(status) << status;
66-
}
67-
}
68-
69-
////////////////////////////////////////////////////////////////////////////////
70-
7144
int RunCreateTables(NYdb::TDriver& driver, const std::string& prefix, int argc, char** argv);
7245
int RunDropTables(NYdb::TDriver& driver, const std::string& prefix, int argc, char** argv);
7346
int RunUpdateViews(NYdb::TDriver& driver, const std::string& prefix, int argc, char** argv);

examples/secondary_index/secondary_index_create.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using namespace NLastGetopt;
44
using namespace NYdb;
55
using namespace NYdb::NTable;
6+
using namespace NYdb::NStatusHelpers;
67

78
////////////////////////////////////////////////////////////////////////////////
89

examples/secondary_index/secondary_index_delete.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using namespace NLastGetopt;
44
using namespace NYdb;
55
using namespace NYdb::NTable;
6+
using namespace NYdb::NStatusHelpers;
67

78
////////////////////////////////////////////////////////////////////////////////
89

examples/secondary_index/secondary_index_drop.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using namespace NLastGetopt;
44
using namespace NYdb;
55
using namespace NYdb::NTable;
6+
using namespace NYdb::NStatusHelpers;
67

78
////////////////////////////////////////////////////////////////////////////////
89

examples/secondary_index/secondary_index_generate.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using namespace NLastGetopt;
99
using namespace NYdb;
1010
using namespace NYdb::NTable;
11+
using namespace NYdb::NStatusHelpers;
1112

1213
namespace {
1314

examples/secondary_index/secondary_index_list.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using namespace NLastGetopt;
66
using namespace NYdb;
77
using namespace NYdb::NTable;
8+
using namespace NYdb::NStatusHelpers;
89

910
////////////////////////////////////////////////////////////////////////////////
1011

examples/secondary_index/secondary_index_update.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using namespace NLastGetopt;
44
using namespace NYdb;
55
using namespace NYdb::NTable;
6+
using namespace NYdb::NStatusHelpers;
67

78
////////////////////////////////////////////////////////////////////////////////
89

examples/secondary_index_builtin/main.cpp

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

33
using namespace NLastGetopt;
44
using namespace NYdb;
5+
using namespace NYdb::NStatusHelpers;
56

67
int main(int argc, char** argv) {
78

@@ -56,7 +57,7 @@ int main(int argc, char** argv) {
5657
}
5758

5859
} catch (const TYdbErrorException& e) {
59-
std::cerr << "Execution failed: " << e << std::endl;
60+
std::cerr << "Execution failed: " << e.what() << std::endl;
6061
return 1;
6162
}
6263
}

examples/secondary_index_builtin/secondary_index.h

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -52,30 +52,6 @@ struct TSeries {
5252
, UploadedUserId(uploadedUserId) {}
5353
};
5454

55-
class TYdbErrorException: public yexception {
56-
public:
57-
TYdbErrorException(NYdb::TStatus status)
58-
: Status(std::move(status))
59-
{ }
60-
61-
friend std::ostream& operator<<(std::ostream& out, const TYdbErrorException& e) {
62-
out << "Status:" << ToString(e.Status.GetStatus());
63-
if (e.Status.GetIssues()) {
64-
out << std::endl;
65-
out << e.Status.GetIssues().ToString();
66-
}
67-
return out;
68-
}
69-
private:
70-
NYdb::TStatus Status;
71-
};
72-
73-
inline void ThrowOnError(NYdb::TStatus status) {
74-
if (!status.IsSuccess()){
75-
throw TYdbErrorException(status) << status;
76-
}
77-
}
78-
7955
std::string GetCommandsList();
8056
TCommand Parse(const char *stringCmnd);
8157
std::string JoinPath(const std::string& prefix, const std::string& path);

examples/secondary_index_builtin/secondary_index_create.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
using namespace NYdb;
44
using namespace NYdb::NTable;
5+
using namespace NYdb::NStatusHelpers;
56
using namespace NLastGetopt;
67

78
static void CreateSeriesTable(TTableClient& client, const std::string& path) {

examples/secondary_index_builtin/secondary_index_drop.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include "secondary_index.h"
22

33
using namespace NYdb::NTable;
4+
using namespace NYdb::NStatusHelpers;
45
using namespace NYdb;
56

67
static void DropTable(TTableClient& client, const std::string& path) {

examples/secondary_index_builtin/secondary_index_fill.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
using namespace NYdb;
66
using namespace NYdb::NTable;
7+
using namespace NYdb::NStatusHelpers;
78

89
std::vector<TSeries> GetSeries() {
910
std::vector<TSeries> series = {

examples/secondary_index_builtin/secondary_index_select.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
using namespace NYdb;
44
using namespace NYdb::NTable;
5+
using namespace NYdb::NStatusHelpers;
56
using namespace NLastGetopt;
67

78
TStatus SelectSeriesWithViews(TSession session, const std::string& path, std::vector<TSeries>& selectResult, uint64_t minViews) {

examples/secondary_index_builtin/secondary_index_select_join.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
using namespace NYdb;
44
using namespace NYdb::NTable;
5+
using namespace NYdb::NStatusHelpers;
56
using namespace NLastGetopt;
67

78
TStatus SelectSeriesWithUserName(TSession session, const std::string& path,

examples/topic_writer/transaction/main.cpp

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,6 @@
11
#include <ydb-cpp-sdk/client/topic/client.h>
22
#include <ydb-cpp-sdk/client/table/table.h>
33

4-
void ThrowOnError(const NYdb::TStatus& status)
5-
{
6-
if (status.IsSuccess()) {
7-
return;
8-
}
9-
10-
ythrow yexception() << status;
11-
}
12-
134
int main()
145
{
156
const std::string ENDPOINT = "HOST:PORT";

examples/ttl/main.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
using namespace NLastGetopt;
66
using namespace NYdb;
7+
using namespace NYdb::NStatusHelpers;
78

89
void StopHandler(int) {
910
exit(1);

examples/ttl/ttl.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using namespace NExample;
77
using namespace NYdb;
88
using namespace NYdb::NTable;
9+
using namespace NYdb::NStatusHelpers;
910

1011
constexpr uint32_t DOC_TABLE_PARTITION_COUNT = 4;
1112
constexpr uint32_t EXPIRATION_QUEUE_COUNT = 4;
@@ -334,8 +335,7 @@ bool Run(const TDriver& driver, const std::string& path) {
334335
ReadDocument(client, path, "https://ya.ru/");
335336
}
336337
catch (const TYdbErrorException& e) {
337-
std::cerr << "Execution failed due to fatal error:" << std::endl;
338-
PrintStatus(e.Status);
338+
std::cerr << "Execution failed due to fatal error: " << e.what() << std::endl;
339339
return false;
340340
}
341341

examples/ttl/util.h

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -12,25 +12,6 @@ namespace NExample {
1212
using namespace NYdb;
1313
using namespace NYdb::NTable;
1414

15-
class TYdbErrorException : public yexception {
16-
public:
17-
TYdbErrorException(const TStatus& status)
18-
: Status(status) {}
19-
20-
TStatus Status;
21-
};
22-
23-
inline void ThrowOnError(const TStatus& status) {
24-
if (!status.IsSuccess()) {
25-
throw TYdbErrorException(status) << status;
26-
}
27-
}
28-
29-
inline void PrintStatus(const TStatus& status) {
30-
std::cerr << "Status: " << ToString(status.GetStatus()) << std::endl;
31-
std::cerr << status.GetIssues().ToString();
32-
}
33-
3415
inline std::string JoinPath(const std::string& basePath, const std::string& path) {
3516
if (basePath.empty()) {
3617
return path;

include/ydb-cpp-sdk/client/table/table.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1098,8 +1098,8 @@ using TAsyncScanQueryPartIterator = NThreading::TFuture<TScanQueryPartIterator>;
10981098

10991099
struct TCreateSessionSettings : public TOperationRequestSettings<TCreateSessionSettings> {};
11001100

1101-
using TBackoffSettings = NYdb::NRetry::TBackoffSettings;
1102-
using TRetryOperationSettings = NYdb::NRetry::TRetryOperationSettings;
1101+
using TBackoffSettings = NYdb::V3::NRetry::TBackoffSettings;
1102+
using TRetryOperationSettings = NYdb::V3::NRetry::TRetryOperationSettings;
11031103

11041104
struct TSessionPoolSettings {
11051105
using TSelf = TSessionPoolSettings;

include/ydb-cpp-sdk/client/types/status/status.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#pragma once
22

3+
#include <ydb-cpp-sdk/client/types/exceptions/exceptions.h>
34
#include <ydb-cpp-sdk/client/types/fatal_error_handlers/handlers.h>
45
#include <ydb-cpp-sdk/client/types/ydb.h>
56

@@ -45,4 +46,27 @@ class TStreamPartStatus : public TStatus {
4546
bool EOS() const;
4647
};
4748

49+
namespace NStatusHelpers {
50+
51+
class TYdbErrorException : public TYdbException {
52+
public:
53+
TYdbErrorException(TStatus status)
54+
: Status_(std::move(status))
55+
{
56+
*this << status;
57+
}
58+
59+
friend IOutputStream& operator<<(IOutputStream& out, const TYdbErrorException& e) {
60+
return out << e.Status_;
61+
}
62+
63+
private:
64+
TStatus Status_;
65+
};
66+
67+
void ThrowOnError(TStatus status, std::function<void(TStatus)> onSuccess = [](TStatus) {});
68+
void ThrowOnErrorOrPrintIssues(TStatus status);
69+
70+
}
71+
4872
} // namespace NYdb

0 commit comments

Comments
 (0)