buffet: extract useful error code definitionss into a separate file Moved json parser error information from http_utils into its own file, error_codes.h/.cc, where all future common error codes would go. BUG=None TEST=USE=buffet P2_TEST_FILTER="buffet::*" FEATURES=test emerge-link platform2 Change-Id: Ie5457cedc248612ab512a7161b1fc2cb75758ac5 Reviewed-on: https://chromium-review.googlesource.com/209248 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/buffet.gyp b/buffet/buffet.gyp index 2197989..001d610 100644 --- a/buffet/buffet.gyp +++ b/buffet/buffet.gyp
@@ -39,6 +39,7 @@ 'dbus_utils.cc', 'device_registration_info.cc', 'error.cc', + 'error_codes.cc', 'exported_object_manager.cc', 'exported_property_set.cc', 'http_request.cc',
diff --git a/buffet/error_codes.cc b/buffet/error_codes.cc new file mode 100644 index 0000000..bf51efc --- /dev/null +++ b/buffet/error_codes.cc
@@ -0,0 +1,22 @@ +// 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_codes.h" + +namespace buffet { +namespace errors { + +namespace json { +const char kDomain[] = "json_parser"; +const char kParseError[] = "json_parse_error"; +const char kObjectExpected[] = "json_object_expected"; +} // namespace json + +namespace file_system { +const char kDomain[] = "file_system"; +const char kFileReadError[] = "file_read_error"; +} // namespace file_system + +} // namespace errors +} // namespace buffet
diff --git a/buffet/error_codes.h b/buffet/error_codes.h new file mode 100644 index 0000000..e2f5961 --- /dev/null +++ b/buffet/error_codes.h
@@ -0,0 +1,25 @@ +// 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. + +#ifndef BUFFET_ERROR_CODES_H_ +#define BUFFET_ERROR_CODES_H_ + +namespace buffet { +namespace errors { + +namespace json { +extern const char kDomain[]; +extern const char kParseError[]; +extern const char kObjectExpected[]; +} // namespace json + +namespace file_system { +extern const char kDomain[]; +extern const char kFileReadError[]; +} // namespace file_system + +} // namespace errors +} // namespace buffet + +#endif // BUFFET_ERROR_CODES_H_
diff --git a/buffet/http_utils.cc b/buffet/http_utils.cc index f7f1190..3924505 100644 --- a/buffet/http_utils.cc +++ b/buffet/http_utils.cc
@@ -10,14 +10,13 @@ #include <base/json/json_writer.h> #include <base/values.h> -#include "buffet/mime_utils.h" #include "buffet/data_encoding.h" +#include "buffet/error_codes.h" +#include "buffet/mime_utils.h" namespace buffet { namespace http { -const char kErrorDomainJSON[] = "json_parser"; - std::unique_ptr<Response> Get(const std::string& url, const HeaderList& headers, std::shared_ptr<Transport> transport, @@ -140,7 +139,7 @@ auto content_type = mime::RemoveParameters(response->GetContentType()); if (content_type != mime::application::kJson && content_type != mime::text::kPlain) { - Error::AddTo(error, kErrorDomainJSON, "non_json_content_type", + Error::AddTo(error, errors::json::kDomain, "non_json_content_type", "Unexpected response content type: " + content_type); return std::unique_ptr<base::DictionaryValue>(); } @@ -150,13 +149,14 @@ base::Value* value = base::JSONReader::ReadAndReturnError( json, base::JSON_PARSE_RFC, nullptr, &error_message); if (!value) { - Error::AddTo(error, kErrorDomainJSON, "json_parse_error", error_message); + Error::AddTo(error, errors::json::kDomain, errors::json::kParseError, + error_message); return std::unique_ptr<base::DictionaryValue>(); } base::DictionaryValue* dict_value = nullptr; if (!value->GetAsDictionary(&dict_value)) { delete value; - Error::AddTo(error, kErrorDomainJSON, "json_object_error", + Error::AddTo(error, errors::json::kDomain, errors::json::kObjectExpected, "Response is not a valid JSON object"); return std::unique_ptr<base::DictionaryValue>(); }
diff --git a/buffet/http_utils.h b/buffet/http_utils.h index 20e255b..0333c82 100644 --- a/buffet/http_utils.h +++ b/buffet/http_utils.h
@@ -20,8 +20,6 @@ namespace buffet { namespace http { -extern const char kErrorDomainJSON[]; - typedef std::vector<std::pair<std::string, std::string>> FormFieldList; ////////////////////////////////////////////////////////////////////////////////