Skip to content

Commit 6c86956

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

File tree

30 files changed

+89
-175
lines changed

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

0 commit comments

Comments
 (0)