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
diff --git a/init/service_test.cpp b/init/service_test.cpp
index 62e46f4..98d876f 100644
--- a/init/service_test.cpp
+++ b/init/service_test.cpp
@@ -132,29 +132,29 @@
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());
}