Vitaly Buka | 4615e0d | 2015-10-14 15:35:12 -0700 | [diff] [blame] | 1 | // Copyright 2015 The Weave Authors. All rights reserved. |
Alex Vakulenko | b04936f | 2014-09-19 14:53:58 -0700 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Stefan Sauer | 2d16dfa | 2015-09-25 17:08:35 +0200 | [diff] [blame] | 5 | #include "src/utils.h" |
Alex Vakulenko | b04936f | 2014-09-19 14:53:58 -0700 | [diff] [blame] | 6 | |
Alex Vakulenko | ae1ffbc | 2015-06-15 12:53:22 -0700 | [diff] [blame] | 7 | #include <base/bind_helpers.h> |
Alex Vakulenko | b04936f | 2014-09-19 14:53:58 -0700 | [diff] [blame] | 8 | #include <base/json/json_reader.h> |
Vitaly Buka | ea2f15f | 2015-08-13 15:26:20 -0700 | [diff] [blame] | 9 | |
Stefan Sauer | 2d16dfa | 2015-09-25 17:08:35 +0200 | [diff] [blame] | 10 | #include "src/json_error_codes.h" |
Alex Vakulenko | b04936f | 2014-09-19 14:53:58 -0700 | [diff] [blame] | 11 | |
Vitaly Buka | b6f015a | 2015-07-09 14:59:23 -0700 | [diff] [blame] | 12 | namespace weave { |
Alex Vakulenko | b04936f | 2014-09-19 14:53:58 -0700 | [diff] [blame] | 13 | |
Alex Vakulenko | 36c85aa | 2015-04-09 09:06:39 -0700 | [diff] [blame] | 14 | namespace { |
| 15 | |
| 16 | // Truncates a string if it is too long. Used for error reporting with really |
| 17 | // long JSON strings. |
| 18 | std::string LimitString(const std::string& text, size_t max_len) { |
| 19 | if (text.size() <= max_len) |
| 20 | return text; |
| 21 | return text.substr(0, max_len - 3) + "..."; |
| 22 | } |
| 23 | |
| 24 | const size_t kMaxStrLen = 1700; // Log messages are limited to 2000 chars. |
| 25 | |
Vitaly Buka | 70f77d9 | 2015-10-07 15:42:40 -0700 | [diff] [blame] | 26 | const char kErrorCodeKey[] = "code"; |
| 27 | const char kErrorMessageKey[] = "message"; |
| 28 | |
Alex Vakulenko | 36c85aa | 2015-04-09 09:06:39 -0700 | [diff] [blame] | 29 | } // anonymous namespace |
| 30 | |
Vitaly Buka | e2810e0 | 2015-08-16 23:31:55 -0700 | [diff] [blame] | 31 | namespace errors { |
Vitaly Buka | 0fa51e5 | 2015-07-10 00:12:25 -0700 | [diff] [blame] | 32 | const char kErrorDomain[] = "weave"; |
Vitaly Buka | e2810e0 | 2015-08-16 23:31:55 -0700 | [diff] [blame] | 33 | const char kSchemaError[] = "schema_error"; |
Alex Vakulenko | 07216fe | 2014-09-19 15:31:09 -0700 | [diff] [blame] | 34 | const char kInvalidCategoryError[] = "invalid_category"; |
| 35 | const char kInvalidPackageError[] = "invalid_package"; |
Vitaly Buka | e2810e0 | 2015-08-16 23:31:55 -0700 | [diff] [blame] | 36 | } // namespace errors |
Alex Vakulenko | 9e2f8cd | 2015-04-07 16:28:09 -0700 | [diff] [blame] | 37 | |
Vitaly Buka | 207c1cb | 2015-05-14 17:06:18 -0700 | [diff] [blame] | 38 | std::unique_ptr<base::DictionaryValue> LoadJsonDict( |
| 39 | const std::string& json_string, |
Vitaly Buka | 0801a1f | 2015-08-14 10:03:46 -0700 | [diff] [blame] | 40 | ErrorPtr* error) { |
Vitaly Buka | 207c1cb | 2015-05-14 17:06:18 -0700 | [diff] [blame] | 41 | std::unique_ptr<base::DictionaryValue> result; |
Alex Vakulenko | b04936f | 2014-09-19 14:53:58 -0700 | [diff] [blame] | 42 | std::string error_message; |
Alex Vakulenko | ae1ffbc | 2015-06-15 12:53:22 -0700 | [diff] [blame] | 43 | auto value = base::JSONReader::ReadAndReturnError( |
Alex Vakulenko | b04936f | 2014-09-19 14:53:58 -0700 | [diff] [blame] | 44 | json_string, base::JSON_PARSE_RFC, nullptr, &error_message); |
| 45 | if (!value) { |
Vitaly Buka | 0801a1f | 2015-08-14 10:03:46 -0700 | [diff] [blame] | 46 | Error::AddToPrintf(error, FROM_HERE, errors::json::kDomain, |
| 47 | errors::json::kParseError, |
| 48 | "Error parsing JSON string '%s' (%zu): %s", |
| 49 | LimitString(json_string, kMaxStrLen).c_str(), |
| 50 | json_string.size(), error_message.c_str()); |
Alex Vakulenko | b04936f | 2014-09-19 14:53:58 -0700 | [diff] [blame] | 51 | return result; |
| 52 | } |
Vitaly Buka | 207c1cb | 2015-05-14 17:06:18 -0700 | [diff] [blame] | 53 | base::DictionaryValue* dict_value = nullptr; |
Alex Vakulenko | b04936f | 2014-09-19 14:53:58 -0700 | [diff] [blame] | 54 | if (!value->GetAsDictionary(&dict_value)) { |
Vitaly Buka | 0801a1f | 2015-08-14 10:03:46 -0700 | [diff] [blame] | 55 | Error::AddToPrintf(error, FROM_HERE, errors::json::kDomain, |
| 56 | errors::json::kObjectExpected, |
| 57 | "JSON string '%s' is not a JSON object", |
| 58 | LimitString(json_string, kMaxStrLen).c_str()); |
Alex Vakulenko | b04936f | 2014-09-19 14:53:58 -0700 | [diff] [blame] | 59 | return result; |
Alex Vakulenko | ae1ffbc | 2015-06-15 12:53:22 -0700 | [diff] [blame] | 60 | } else { |
| 61 | // |value| is now owned by |dict_value|. |
| 62 | base::IgnoreResult(value.release()); |
Alex Vakulenko | b04936f | 2014-09-19 14:53:58 -0700 | [diff] [blame] | 63 | } |
| 64 | result.reset(dict_value); |
| 65 | return result; |
| 66 | } |
| 67 | |
Vitaly Buka | 70f77d9 | 2015-10-07 15:42:40 -0700 | [diff] [blame] | 68 | std::unique_ptr<base::DictionaryValue> ErrorInfoToJson(const Error& error) { |
| 69 | std::unique_ptr<base::DictionaryValue> output{new base::DictionaryValue}; |
| 70 | output->SetString(kErrorMessageKey, error.GetMessage()); |
| 71 | output->SetString(kErrorCodeKey, error.GetCode()); |
| 72 | return output; |
| 73 | } |
| 74 | |
Vitaly Buka | b6f015a | 2015-07-09 14:59:23 -0700 | [diff] [blame] | 75 | } // namespace weave |