Buffet: utility function tweaks
Various tweakes to helper functions and classes, split off from
a larger CL that required them.
1. Added Error::AddToPrintf to help add formatted error messages.
2. Added Error::GetFirstError to get the innermost error occurred.
3. Added string_utils::ToString and swept code using std::to_string
in order to ensure we format doubles correctly (using %g instead
of %f format specifier) and bool values (using "true"/"false"
instead of 1/0).
4. Fixed C-style cast in http_utils.h and using static_cast now.
5. Fixed a few linter warnings. Also since the linter was updated
there is no reason to have some NOLINT since many C++11 features
are now recognized properly by cpplint.
BUG=None
TEST=All unit tests pass.
Change-Id: I208ffaa3f0ec0a5ff78bf9e8151e784ec8cd77e2
Reviewed-on: https://chromium-review.googlesource.com/202962
Tested-by: Alex Vakulenko <avakulenko@chromium.org>
Reviewed-by: Christopher Wiley <wiley@chromium.org>
Commit-Queue: Alex Vakulenko <avakulenko@chromium.org>
diff --git a/buffet/error.cc b/buffet/error.cc
index bccb9b8..db5543d 100644
--- a/buffet/error.cc
+++ b/buffet/error.cc
@@ -5,6 +5,7 @@
#include "buffet/error.h"
#include <base/logging.h>
+#include <base/strings/stringprintf.h>
using buffet::Error;
using buffet::ErrorPtr;
@@ -36,6 +37,15 @@
}
}
+void Error::AddToPrintf(ErrorPtr* error, const std::string& domain,
+ const std::string& code, const char* format, ...) {
+ va_list ap;
+ va_start(ap, format);
+ std::string message = base::StringPrintV(format, ap);
+ va_end(ap);
+ AddTo(error, domain, code, message);
+}
+
bool Error::HasDomain(const std::string& domain) const {
const Error* err = this;
while (err) {
@@ -56,6 +66,13 @@
return false;
}
+const Error* Error::GetFirstError() const {
+ const Error* err = this;
+ while (err->GetInnerError())
+ err = err->GetInnerError();
+ return err;
+}
+
Error::Error(const std::string& domain, const std::string& code,
const std::string& message, ErrorPtr inner_error) :
domain_(domain), code_(code), message_(message),