buffet: Added advanced error reporting Created chromeos::Error class that encapsulates rich error information from various system domains. Swept GCD device registration, HTTP transport and utilities to always return additional error information when the caller requests it. This includes internal errors, general HTTP errors as well as parsing and returning specific GCD and OAuth2 server error responses. Also fixed a number of existing linter warnings. BUG=chromium:366709 TEST=All existing and new unit tests pass. Change-Id: Ic01622a8efa3dc365ec106e595b09536818b9b23 Reviewed-on: https://chromium-review.googlesource.com/198772 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 new file mode 100644 index 0000000..b04bbb6 --- /dev/null +++ b/buffet/error.cc
@@ -0,0 +1,63 @@ +// Copyright 2014 The Chromium OS Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "buffet/error.h" + +#include <base/logging.h> + +using chromeos::Error; +using chromeos::ErrorPtr; + +ErrorPtr Error::Create(const std::string& domain, + const std::string& code, + const std::string& message) { + return Create(domain, code, message, ErrorPtr()); +} + +ErrorPtr Error::Create(const std::string& domain, + const std::string& code, + const std::string& message, + ErrorPtr inner_error) { + LOG(ERROR) << "Error::Create: Domain=" << domain + << ", Code=" << code << ", Message=" << message; + return ErrorPtr(new Error(domain, code, message, std::move(inner_error))); +} + +void Error::AddTo(ErrorPtr* error, const std::string& domain, + const std::string& code, const std::string& message) { + if (error) { + *error = Create(domain, code, message, std::move(*error)); + } else { + // Create already logs the error, but if |error| is nullptr, + // we still want to log the error... + LOG(ERROR) << "Error::Create: Domain=" << domain + << ", Code=" << code << ", Message=" << message; + } +} + +bool Error::HasDomain(const std::string& domain) const { + const Error* err = this; + while (err) { + if (err->GetDomain() == domain) + return true; + err = err->GetInnerError(); + } + return false; +} + +bool Error::HasError(const std::string& domain, const std::string& code) const { + const Error* err = this; + while (err) { + if (err->GetDomain() == domain && err->GetCode() == code) + return true; + err = err->GetInnerError(); + } + return false; +} + +Error::Error(const std::string& domain, const std::string& code, + const std::string& message, ErrorPtr inner_error) : + domain_(domain), code_(code), message_(message), + inner_error_(std::move(inner_error)) { +}