diff options
| author | Tom Cherry <tomcherry@google.com> | 2017-08-03 12:54:07 -0700 |
|---|---|---|
| committer | Tom Cherry <tomcherry@google.com> | 2017-08-14 14:07:30 -0700 |
| commit | 11a3aeeae3dc887b889d4086d4d26d95c324c08d (patch) | |
| tree | b83a59f592c18759494dc64274606eafdceae3b4 /init/service_test.cpp | |
| parent | d467db9b3d000e91717daccae0a13ba45cedb6ed (diff) | |
| download | system_core-11a3aeeae3dc887b889d4086d4d26d95c324c08d.tar.gz system_core-11a3aeeae3dc887b889d4086d4d26d95c324c08d.tar.bz2 system_core-11a3aeeae3dc887b889d4086d4d26d95c324c08d.zip | |
init: introduce Result<T> for return values and error handling
init tries to propagate error information up to build context before
logging errors. This is a good thing, however too often init has the
overly verbose paradigm for error handling, below:
bool CalculateResult(const T& input, U* output, std::string* err)
bool CalculateAndUseResult(const T& input, std::string* err) {
U output;
std::string calculate_result_err;
if (!CalculateResult(input, &output, &calculate_result_err)) {
*err = "CalculateResult " + input + " failed: " +
calculate_result_err;
return false;
}
UseResult(output);
return true;
}
Even more common are functions that return only true/false but also
require passing a std::string* err in order to see the error message.
This change introduces a Result<T> that is use to either hold a
successful return value of type T or to hold an error message as a
std::string. If the functional only returns success or a failure with
an error message, Result<Success> may be used. The classes Error and
ErrnoError are used to indicate a failed Result<T>.
A successful Result<T> is constructed implicitly from any type that
can be implicitly converted to T or from the constructor arguments for
T. This allows you to return a type T directly from a function that
returns Result<T>.
Error and ErrnoError are used to construct a Result<T> has
failed. Each of these classes take an ostream as an input and are
implicitly cast to a Result<T> containing that failure. ErrnoError()
additionally appends ": " + strerror(errno) to the end of the failure
string to aid in interacting with C APIs.
The end result is that the above code snippet is turned into the much
clearer example below:
Result<U> CalculateResult(const T& input);
Result<Success> CalculateAndUseResult(const T& input) {
auto output = CalculateResult(input);
if (!output) {
return Error() << "CalculateResult " << input << " failed: "
<< output.error();
}
UseResult(*output);
return Success();
}
This change also makes this conversion for some of the util.cpp
functions that used the old paradigm.
Test: boot bullhead, init unit tests
Merged-In: I1e7d3a8820a79362245041251057fbeed2f7979b
Change-Id: I1e7d3a8820a79362245041251057fbeed2f7979b
Diffstat (limited to 'init/service_test.cpp')
| -rw-r--r-- | init/service_test.cpp | 28 |
1 files changed, 14 insertions, 14 deletions
diff --git a/init/service_test.cpp b/init/service_test.cpp index 62e46f4cd..98d876f51 100644 --- a/init/service_test.cpp +++ b/init/service_test.cpp @@ -132,29 +132,29 @@ static void Test_make_temporary_oneshot_service(bool dash_dash, bool seclabel, b ASSERT_EQ("", svc->seclabel()); } if (uid) { - uid_t decoded_uid; - std::string err; - ASSERT_TRUE(DecodeUid("log", &decoded_uid, &err)); - ASSERT_EQ(decoded_uid, svc->uid()); + auto decoded_uid = DecodeUid("log"); + ASSERT_TRUE(decoded_uid); + ASSERT_EQ(*decoded_uid, svc->uid()); } else { ASSERT_EQ(0U, svc->uid()); } if (gid) { - uid_t decoded_uid; - std::string err; - ASSERT_TRUE(DecodeUid("shell", &decoded_uid, &err)); - ASSERT_EQ(decoded_uid, svc->gid()); + auto decoded_uid = DecodeUid("shell"); + ASSERT_TRUE(decoded_uid); + ASSERT_EQ(*decoded_uid, svc->gid()); } else { ASSERT_EQ(0U, svc->gid()); } if (supplementary_gids) { ASSERT_EQ(2U, svc->supp_gids().size()); - uid_t decoded_uid; - std::string err; - ASSERT_TRUE(DecodeUid("system", &decoded_uid, &err)); - ASSERT_EQ(decoded_uid, svc->supp_gids()[0]); - ASSERT_TRUE(DecodeUid("adb", &decoded_uid, &err)); - ASSERT_EQ(decoded_uid, svc->supp_gids()[1]); + + auto decoded_uid = DecodeUid("system"); + ASSERT_TRUE(decoded_uid); + ASSERT_EQ(*decoded_uid, svc->supp_gids()[0]); + + decoded_uid = DecodeUid("adb"); + ASSERT_TRUE(decoded_uid); + ASSERT_EQ(*decoded_uid, svc->supp_gids()[1]); } else { ASSERT_EQ(0U, svc->supp_gids().size()); } |
