blob: daefd33788f159b905b38dc7da74249b9e24ff3f [file] [log] [blame]
// Copyright 2015 The Weave 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 "src/utils.h"
#include <base/bind_helpers.h>
#include <base/json/json_reader.h>
#include "src/json_error_codes.h"
namespace weave {
namespace {
// Truncates a string if it is too long. Used for error reporting with really
// long JSON strings.
std::string LimitString(const std::string& text, size_t max_len) {
if (text.size() <= max_len)
return text;
return text.substr(0, max_len - 3) + "...";
const size_t kMaxStrLen = 1700; // Log messages are limited to 2000 chars.
const char kErrorCodeKey[] = "code";
const char kErrorMessageKey[] = "message";
} // anonymous namespace
namespace errors {
const char kErrorDomain[] = "weave";
const char kSchemaError[] = "schema_error";
const char kInvalidCategoryError[] = "invalid_category";
const char kInvalidPackageError[] = "invalid_package";
} // namespace errors
std::unique_ptr<base::DictionaryValue> LoadJsonDict(
const std::string& json_string,
ErrorPtr* error) {
std::unique_ptr<base::DictionaryValue> result;
std::string error_message;
// TODO(crbug/1066447): migrate to new ReadAndReturnError API after uprev.
auto value = base::JSONReader::ReadAndReturnErrorDeprecated(
json_string, base::JSON_PARSE_RFC, nullptr, &error_message);
if (!value) {
Error::AddToPrintf(error, FROM_HERE, errors::json::kDomain,
"Error parsing JSON string '%s' (%zu): %s",
LimitString(json_string, kMaxStrLen).c_str(),
json_string.size(), error_message.c_str());
return result;
base::DictionaryValue* dict_value = nullptr;
if (!value->GetAsDictionary(&dict_value)) {
Error::AddToPrintf(error, FROM_HERE, errors::json::kDomain,
"JSON string '%s' is not a JSON object",
LimitString(json_string, kMaxStrLen).c_str());
return result;
} else {
// |value| is now owned by |dict_value|.
return result;
std::unique_ptr<base::DictionaryValue> ErrorInfoToJson(const Error& error) {
std::unique_ptr<base::DictionaryValue> output{new base::DictionaryValue};
output->SetString(kErrorMessageKey, error.GetMessage());
output->SetString(kErrorCodeKey, error.GetCode());
return output;
} // namespace weave