No description
11a3aeeae3
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 |
||
---|---|---|
adb | ||
adf | ||
base | ||
bootstat | ||
cpio | ||
debuggerd | ||
demangle | ||
fastboot | ||
fingerprintd | ||
fs_mgr | ||
gatekeeperd | ||
healthd | ||
include | ||
init | ||
libappfuse | ||
libbacktrace | ||
libbinderwrapper | ||
libcrypto_utils | ||
libcutils | ||
libdiskconfig | ||
libion | ||
libkeyutils | ||
liblog | ||
libmemtrack | ||
libmemunreachable | ||
libmetricslogger | ||
libnativebridge | ||
libnativeloader | ||
libnetutils | ||
libpackagelistparser | ||
libpixelflinger | ||
libprocessgroup | ||
libprocinfo | ||
libsparse | ||
libsuspend | ||
libsync | ||
libsystem | ||
libsysutils | ||
libunwindstack | ||
libusbhost | ||
libutils | ||
libziparchive | ||
lmkd | ||
logcat | ||
logd | ||
logwrapper | ||
mkbootimg | ||
reboot | ||
rootdir | ||
run-as | ||
sdcard | ||
shell_and_utilities | ||
toolbox | ||
trusty | ||
.clang-format | ||
.clang-format-2 | ||
.clang-format-4 | ||
.gitignore | ||
Android.bp | ||
Android.mk | ||
CleanSpec.mk | ||
MODULE_LICENSE_APACHE2 | ||
NOTICE | ||
platform_tools_tool_version.mk | ||
PREUPLOAD.cfg |