Skip to content

Commit 028876c

Browse files
authored
cleanup: stop throwing/catching standard exceptions in quickstarts (googleapis#10053)
Instead throw/catch `google::cloud::Status` values, so that all the error information can be reported.
1 parent cda279b commit 028876c

192 files changed

Lines changed: 564 additions & 761 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

generator/internal/scaffold_generator.cc

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -796,7 +796,6 @@ void GenerateQuickstartSkeleton(
796796
#include "google/cloud/$library$/ EDIT HERE .h"
797797
#include "google/cloud/project.h"
798798
#include <iostream>
799-
#include <stdexcept>
800799
801800
int main(int argc, char* argv[]) try {
802801
if (argc != 2) {
@@ -810,13 +809,13 @@ int main(int argc, char* argv[]) try {
810809
811810
auto const project = google::cloud::Project(argv[1]);
812811
for (auto r : client.List/*EDIT HERE*/(project.FullName())) {
813-
if (!r) throw std::runtime_error(r.status().message());
812+
if (!r) throw std::move(r).status();
814813
std::cout << r->DebugString() << "\n";
815814
}
816815
817816
return 0;
818-
} catch (std::exception const& ex) {
819-
std::cerr << "Standard exception raised: " << ex.what() << "\n";
817+
} catch (google::cloud::Status const& status) {
818+
std::cerr << "google::cloud::Status thrown: " << status << "\n";
820819
return 1;
821820
}
822821
)""";

google/cloud/accessapproval/README.md

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ this library.
3535
#include "google/cloud/accessapproval/access_approval_client.h"
3636
#include "google/cloud/project.h"
3737
#include <iostream>
38-
#include <stdexcept>
3938

4039
int main(int argc, char* argv[]) try {
4140
if (argc != 2) {
@@ -48,13 +47,13 @@ int main(int argc, char* argv[]) try {
4847
accessapproval::MakeAccessApprovalConnection());
4948
auto const project = google::cloud::Project(argv[1]);
5049
for (auto a : client.ListApprovalRequests(project.FullName())) {
51-
if (!a) throw std::runtime_error(a.status().message());
50+
if (!a) throw std::move(a).status();
5251
std::cout << a->DebugString() << "\n";
5352
}
5453

5554
return 0;
56-
} catch (std::exception const& ex) {
57-
std::cerr << "Standard exception raised: " << ex.what() << "\n";
55+
} catch (google::cloud::Status const& status) {
56+
std::cerr << "google::cloud::Status thrown: " << status << "\n";
5857
return 1;
5958
}
6059
```

google/cloud/accessapproval/quickstart/quickstart.cc

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
#include "google/cloud/accessapproval/access_approval_client.h"
1616
#include "google/cloud/project.h"
1717
#include <iostream>
18-
#include <stdexcept>
1918

2019
int main(int argc, char* argv[]) try {
2120
if (argc != 2) {
@@ -28,12 +27,12 @@ int main(int argc, char* argv[]) try {
2827
accessapproval::MakeAccessApprovalConnection());
2928
auto const project = google::cloud::Project(argv[1]);
3029
for (auto a : client.ListApprovalRequests(project.FullName())) {
31-
if (!a) throw std::runtime_error(a.status().message());
30+
if (!a) throw std::move(a).status();
3231
std::cout << a->DebugString() << "\n";
3332
}
3433

3534
return 0;
36-
} catch (std::exception const& ex) {
37-
std::cerr << "Standard exception raised: " << ex.what() << "\n";
35+
} catch (google::cloud::Status const& status) {
36+
std::cerr << "google::cloud::Status thrown: " << status << "\n";
3837
return 1;
3938
}

google/cloud/accesscontextmanager/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ if (BUILD_TESTING)
123123
set_tests_properties(
124124
accesscontextmanager_quickstart
125125
PROPERTIES LABELS "integration-test;quickstart" PASS_REGULAR_EXPRESSION
126-
"Permanent error in ListAccessLevels:")
126+
"Permanent error in.*ListAccessLevels:")
127127
endif ()
128128

129129
# Get the destination directories based on the GNU recommendations.

google/cloud/accesscontextmanager/README.md

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ this library.
3434
```cc
3535
#include "google/cloud/accesscontextmanager/access_context_manager_client.h"
3636
#include <iostream>
37-
#include <stdexcept>
3837

3938
int main(int argc, char* argv[]) try {
4039
if (argc != 2) {
@@ -48,13 +47,13 @@ int main(int argc, char* argv[]) try {
4847

4948
auto const parent = std::string("accessPolicies/") + argv[1];
5049
for (auto r : client.ListAccessLevels(parent)) {
51-
if (!r) throw std::runtime_error(r.status().message());
50+
if (!r) throw std::move(r).status();
5251
std::cout << r->DebugString() << "\n";
5352
}
5453

5554
return 0;
56-
} catch (std::exception const& ex) {
57-
std::cerr << "Standard exception raised: " << ex.what() << "\n";
55+
} catch (google::cloud::Status const& status) {
56+
std::cerr << "google::cloud::Status thrown: " << status << "\n";
5857
return 1;
5958
}
6059
```

google/cloud/accesscontextmanager/quickstart/quickstart.cc

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414

1515
#include "google/cloud/accesscontextmanager/access_context_manager_client.h"
1616
#include <iostream>
17-
#include <stdexcept>
1817

1918
int main(int argc, char* argv[]) try {
2019
if (argc != 2) {
@@ -28,12 +27,12 @@ int main(int argc, char* argv[]) try {
2827

2928
auto const parent = std::string("accessPolicies/") + argv[1];
3029
for (auto r : client.ListAccessLevels(parent)) {
31-
if (!r) throw std::runtime_error(r.status().message());
30+
if (!r) throw std::move(r).status();
3231
std::cout << r->DebugString() << "\n";
3332
}
3433

3534
return 0;
36-
} catch (std::exception const& ex) {
37-
std::cerr << "Standard exception raised: " << ex.what() << "\n";
35+
} catch (google::cloud::Status const& status) {
36+
std::cerr << "google::cloud::Status thrown: " << status << "\n";
3837
return 1;
3938
}

google/cloud/apigateway/README.md

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ this library.
3434
```cc
3535
#include "google/cloud/apigateway/api_gateway_client.h"
3636
#include <iostream>
37-
#include <stdexcept>
3837

3938
int main(int argc, char* argv[]) try {
4039
if (argc != 3) {
@@ -49,13 +48,13 @@ int main(int argc, char* argv[]) try {
4948
auto const parent =
5049
std::string("projects/") + argv[1] + "/locations/" + argv[2];
5150
for (auto r : client.ListGateways(parent)) {
52-
if (!r) throw std::runtime_error(r.status().message());
51+
if (!r) throw std::move(r).status();
5352
std::cout << r->DebugString() << "\n";
5453
}
5554

5655
return 0;
57-
} catch (std::exception const& ex) {
58-
std::cerr << "Standard exception raised: " << ex.what() << "\n";
56+
} catch (google::cloud::Status const& status) {
57+
std::cerr << "google::cloud::Status thrown: " << status << "\n";
5958
return 1;
6059
}
6160
```

google/cloud/apigateway/quickstart/quickstart.cc

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414

1515
#include "google/cloud/apigateway/api_gateway_client.h"
1616
#include <iostream>
17-
#include <stdexcept>
1817

1918
int main(int argc, char* argv[]) try {
2019
if (argc != 3) {
@@ -29,12 +28,12 @@ int main(int argc, char* argv[]) try {
2928
auto const parent =
3029
std::string("projects/") + argv[1] + "/locations/" + argv[2];
3130
for (auto r : client.ListGateways(parent)) {
32-
if (!r) throw std::runtime_error(r.status().message());
31+
if (!r) throw std::move(r).status();
3332
std::cout << r->DebugString() << "\n";
3433
}
3534

3635
return 0;
37-
} catch (std::exception const& ex) {
38-
std::cerr << "Standard exception raised: " << ex.what() << "\n";
36+
} catch (google::cloud::Status const& status) {
37+
std::cerr << "google::cloud::Status thrown: " << status << "\n";
3938
return 1;
4039
}

google/cloud/apigeeconnect/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ if (BUILD_TESTING)
121121
set_tests_properties(
122122
apigeeconnect_quickstart
123123
PROPERTIES LABELS "integration-test;quickstart" PASS_REGULAR_EXPRESSION
124-
"Permanent error in ListConnections:")
124+
"Permanent error in.*ListConnections:")
125125
endif ()
126126

127127
# Get the destination directories based on the GNU recommendations.

google/cloud/apigeeconnect/README.md

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ this library.
4141
```cc
4242
#include "google/cloud/apigeeconnect/connection_client.h"
4343
#include <iostream>
44-
#include <stdexcept>
4544

4645
int main(int argc, char* argv[]) try {
4746
if (argc != 3) {
@@ -56,13 +55,13 @@ int main(int argc, char* argv[]) try {
5655
auto const parent =
5756
std::string{"projects/"} + argv[1] + "/endpoints/" + argv[2];
5857
for (auto r : client.ListConnections(parent)) {
59-
if (!r) throw std::runtime_error(r.status().message());
58+
if (!r) throw std::move(r).status();
6059
std::cout << r->DebugString() << "\n";
6160
}
6261

6362
return 0;
64-
} catch (std::exception const& ex) {
65-
std::cerr << "Standard exception raised: " << ex.what() << "\n";
63+
} catch (google::cloud::Status const& status) {
64+
std::cerr << "google::cloud::Status thrown: " << status << "\n";
6665
return 1;
6766
}
6867
```

0 commit comments

Comments
 (0)