Skip to content

Commit 57823e0

Browse files
authored
impl(common): helper functions to create Status (#10270)
These will make it easier to return errors, with less syntactic noise. A future PR will make it easier to create error infos used in the library.
1 parent a471594 commit 57823e0

File tree

6 files changed

+212
-0
lines changed

6 files changed

+212
-0
lines changed

google/cloud/google_cloud_cpp_common.bzl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ google_cloud_cpp_common_hdrs = [
5757
"internal/invoke_result.h",
5858
"internal/ios_flags_saver.h",
5959
"internal/log_impl.h",
60+
"internal/make_status.h",
6061
"internal/non_constructible.h",
6162
"internal/pagination_range.h",
6263
"internal/parse_rfc3339.h",
@@ -105,6 +106,7 @@ google_cloud_cpp_common_srcs = [
105106
"internal/future_impl.cc",
106107
"internal/getenv.cc",
107108
"internal/log_impl.cc",
109+
"internal/make_status.cc",
108110
"internal/parse_rfc3339.cc",
109111
"internal/populate_common_options.cc",
110112
"internal/random.cc",

google/cloud/google_cloud_cpp_common.cmake

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,8 @@ add_library(
8383
internal/ios_flags_saver.h
8484
internal/log_impl.cc
8585
internal/log_impl.h
86+
internal/make_status.cc
87+
internal/make_status.h
8688
internal/non_constructible.h
8789
internal/pagination_range.h
8890
internal/parse_rfc3339.cc
@@ -301,6 +303,7 @@ if (BUILD_TESTING)
301303
internal/group_options_test.cc
302304
internal/invoke_result_test.cc
303305
internal/log_impl_test.cc
306+
internal/make_status_test.cc
304307
internal/pagination_range_test.cc
305308
internal/parse_rfc3339_test.cc
306309
internal/populate_common_options_test.cc

google/cloud/google_cloud_cpp_common_unit_tests.bzl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ google_cloud_cpp_common_unit_tests = [
3838
"internal/group_options_test.cc",
3939
"internal/invoke_result_test.cc",
4040
"internal/log_impl_test.cc",
41+
"internal/make_status_test.cc",
4142
"internal/pagination_range_test.cc",
4243
"internal/parse_rfc3339_test.cc",
4344
"internal/populate_common_options_test.cc",
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
// Copyright 2022 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#include "google/cloud/internal/make_status.h"
16+
17+
namespace google {
18+
namespace cloud {
19+
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN
20+
namespace internal {
21+
22+
Status CancelledError(std::string msg, ErrorInfo info) {
23+
return Status(StatusCode::kCancelled, std::move(msg), std::move(info));
24+
}
25+
26+
Status UnknownError(std::string msg, ErrorInfo info) {
27+
return Status(StatusCode::kUnknown, std::move(msg), std::move(info));
28+
}
29+
30+
Status InvalidArgumentError(std::string msg, ErrorInfo info) {
31+
return Status(StatusCode::kInvalidArgument, std::move(msg), std::move(info));
32+
}
33+
34+
Status DeadlineExceededError(std::string msg, ErrorInfo info) {
35+
return Status(StatusCode::kDeadlineExceeded, std::move(msg), std::move(info));
36+
}
37+
38+
Status NotFoundError(std::string msg, ErrorInfo info) {
39+
return Status(StatusCode::kNotFound, std::move(msg), std::move(info));
40+
}
41+
42+
Status AlreadyExistsError(std::string msg, ErrorInfo info) {
43+
return Status(StatusCode::kAlreadyExists, std::move(msg), std::move(info));
44+
}
45+
46+
Status PermissionDeniedError(std::string msg, ErrorInfo info) {
47+
return Status(StatusCode::kPermissionDenied, std::move(msg), std::move(info));
48+
}
49+
50+
Status UnauthenticatedError(std::string msg, ErrorInfo info) {
51+
return Status(StatusCode::kUnauthenticated, std::move(msg), std::move(info));
52+
}
53+
54+
Status ResourceExhaustedError(std::string msg, ErrorInfo info) {
55+
return Status(StatusCode::kResourceExhausted, std::move(msg),
56+
std::move(info));
57+
}
58+
59+
Status FailedPreconditionError(std::string msg, ErrorInfo info) {
60+
return Status(StatusCode::kFailedPrecondition, std::move(msg),
61+
std::move(info));
62+
}
63+
64+
Status AbortedError(std::string msg, ErrorInfo info) {
65+
return Status(StatusCode::kAborted, std::move(msg), std::move(info));
66+
}
67+
68+
Status OutOfRangeError(std::string msg, ErrorInfo info) {
69+
return Status(StatusCode::kOutOfRange, std::move(msg), std::move(info));
70+
}
71+
72+
Status UnimplementedError(std::string msg, ErrorInfo info) {
73+
return Status(StatusCode::kUnimplemented, std::move(msg), std::move(info));
74+
}
75+
76+
Status InternalError(std::string msg, ErrorInfo info) {
77+
return Status(StatusCode::kInternal, std::move(msg), std::move(info));
78+
}
79+
80+
Status UnavailableError(std::string msg, ErrorInfo info) {
81+
return Status(StatusCode::kUnavailable, std::move(msg), std::move(info));
82+
}
83+
84+
Status DataLossError(std::string msg, ErrorInfo info) {
85+
return Status(StatusCode::kDataLoss, std::move(msg), std::move(info));
86+
}
87+
88+
} // namespace internal
89+
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END
90+
} // namespace cloud
91+
} // namespace google
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// Copyright 2022 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#ifndef GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_INTERNAL_MAKE_STATUS_H
16+
#define GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_INTERNAL_MAKE_STATUS_H
17+
18+
#include "google/cloud/status.h"
19+
#include "google/cloud/version.h"
20+
#include <string>
21+
22+
namespace google {
23+
namespace cloud {
24+
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN
25+
namespace internal {
26+
27+
Status CancelledError(std::string msg, ErrorInfo info = {});
28+
Status UnknownError(std::string msg, ErrorInfo info = {});
29+
Status InvalidArgumentError(std::string msg, ErrorInfo info = {});
30+
Status DeadlineExceededError(std::string msg, ErrorInfo info = {});
31+
Status NotFoundError(std::string msg, ErrorInfo info = {});
32+
Status AlreadyExistsError(std::string msg, ErrorInfo info = {});
33+
Status PermissionDeniedError(std::string msg, ErrorInfo info = {});
34+
Status UnauthenticatedError(std::string msg, ErrorInfo info = {});
35+
Status ResourceExhaustedError(std::string msg, ErrorInfo info = {});
36+
Status FailedPreconditionError(std::string msg, ErrorInfo info = {});
37+
Status AbortedError(std::string msg, ErrorInfo info = {});
38+
Status OutOfRangeError(std::string msg, ErrorInfo info = {});
39+
Status UnimplementedError(std::string msg, ErrorInfo info = {});
40+
Status InternalError(std::string msg, ErrorInfo info = {});
41+
Status UnavailableError(std::string msg, ErrorInfo info = {});
42+
Status DataLossError(std::string msg, ErrorInfo info = {});
43+
44+
} // namespace internal
45+
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END
46+
} // namespace cloud
47+
} // namespace google
48+
49+
#endif // GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_INTERNAL_MAKE_STATUS_H
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
// Copyright 2022 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#include "google/cloud/internal/make_status.h"
16+
#include "google/cloud/testing_util/status_matchers.h"
17+
#include <gmock/gmock.h>
18+
19+
namespace google {
20+
namespace cloud {
21+
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN
22+
namespace internal {
23+
24+
using ::google::cloud::testing_util::StatusIs;
25+
26+
TEST(MakeStatus, Basic) {
27+
auto error_info = ErrorInfo("REASON", "domain", {{"key", "value"}});
28+
29+
struct TestCase {
30+
StatusCode code;
31+
Status status;
32+
} cases[] = {
33+
{StatusCode::kCancelled, CancelledError("test", error_info)},
34+
{StatusCode::kUnknown, UnknownError("test", error_info)},
35+
{StatusCode::kInvalidArgument, InvalidArgumentError("test", error_info)},
36+
{StatusCode::kDeadlineExceeded,
37+
DeadlineExceededError("test", error_info)},
38+
{StatusCode::kNotFound, NotFoundError("test", error_info)},
39+
{StatusCode::kAlreadyExists, AlreadyExistsError("test", error_info)},
40+
{StatusCode::kPermissionDenied,
41+
PermissionDeniedError("test", error_info)},
42+
{StatusCode::kUnauthenticated, UnauthenticatedError("test", error_info)},
43+
{StatusCode::kResourceExhausted,
44+
ResourceExhaustedError("test", error_info)},
45+
{StatusCode::kFailedPrecondition,
46+
FailedPreconditionError("test", error_info)},
47+
{StatusCode::kAborted, AbortedError("test", error_info)},
48+
{StatusCode::kOutOfRange, OutOfRangeError("test", error_info)},
49+
{StatusCode::kUnimplemented, UnimplementedError("test", error_info)},
50+
{StatusCode::kInternal, InternalError("test", error_info)},
51+
{StatusCode::kUnavailable, UnavailableError("test", error_info)},
52+
{StatusCode::kDataLoss, DataLossError("test", error_info)},
53+
};
54+
55+
for (auto const& test : cases) {
56+
SCOPED_TRACE("Testing for " + StatusCodeToString(test.code));
57+
EXPECT_THAT(test.status, StatusIs(test.code));
58+
EXPECT_EQ(test.status.message(), "test");
59+
EXPECT_EQ(test.status.error_info(), error_info);
60+
}
61+
}
62+
63+
} // namespace internal
64+
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END
65+
} // namespace cloud
66+
} // namespace google

0 commit comments

Comments
 (0)