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_unittest.cc b/buffet/error_unittest.cc
new file mode 100644
index 0000000..3963315
--- /dev/null
+++ b/buffet/error_unittest.cc
@@ -0,0 +1,56 @@
+// 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 <gtest/gtest.h>
+#include <base/files/file_path.h>
+
+#include "buffet/error.h"
+
+using chromeos::Error;
+
+namespace {
+
+chromeos::ErrorPtr GenerateNetworkError() {
+ return Error::Create("network", "not_found", "Resource not found");
+}
+
+chromeos::ErrorPtr GenerateHttpError() {
+ auto inner = GenerateNetworkError();
+ return Error::Create("HTTP", "404", "Not found", std::move(inner));
+}
+
+} // namespace
+
+TEST(Error, Single) {
+ auto err = GenerateNetworkError();
+ EXPECT_EQ("network", err->GetDomain());
+ EXPECT_EQ("not_found", err->GetCode());
+ EXPECT_EQ("Resource not found", err->GetMessage());
+ EXPECT_EQ(nullptr, err->GetInnerError());
+ EXPECT_TRUE(err->HasDomain("network"));
+ EXPECT_FALSE(err->HasDomain("HTTP"));
+ EXPECT_FALSE(err->HasDomain("foo"));
+ EXPECT_TRUE(err->HasError("network", "not_found"));
+ EXPECT_FALSE(err->HasError("network", "404"));
+ EXPECT_FALSE(err->HasError("HTTP", "404"));
+ EXPECT_FALSE(err->HasError("HTTP", "not_found"));
+ EXPECT_FALSE(err->HasError("foo", "bar"));
+}
+
+TEST(Error, Nested) {
+ auto err = GenerateHttpError();
+ EXPECT_EQ("HTTP", err->GetDomain());
+ EXPECT_EQ("404", err->GetCode());
+ EXPECT_EQ("Not found", err->GetMessage());
+ EXPECT_NE(nullptr, err->GetInnerError());
+ EXPECT_EQ("network", err->GetInnerError()->GetDomain());
+ EXPECT_TRUE(err->HasDomain("network"));
+ EXPECT_TRUE(err->HasDomain("HTTP"));
+ EXPECT_FALSE(err->HasDomain("foo"));
+ EXPECT_TRUE(err->HasError("network", "not_found"));
+ EXPECT_FALSE(err->HasError("network", "404"));
+ EXPECT_TRUE(err->HasError("HTTP", "404"));
+ EXPECT_FALSE(err->HasError("HTTP", "not_found"));
+ EXPECT_FALSE(err->HasError("foo", "bar"));
+}