From 3bdfe167de275b7a89ee79431aa3ca45933acda4 Mon Sep 17 00:00:00 2001 From: John Keiser Date: Fri, 6 Mar 2020 12:14:23 -0800 Subject: [PATCH] Support cout << error --- README.md | 2 +- amalgamation.sh | 4 ++-- benchmark/parse_stream.cpp | 4 ++-- include/simdjson/document.h | 8 ++++---- include/simdjson/error.h | 22 +++++++++++++++++++--- include/simdjson/inline/error.h | 4 ++++ singleheader/amalgamation_demo.cpp | 2 +- tests/basictests.cpp | 6 +++--- tests/parse_many_test.cpp | 2 +- tests/readme_examples.cpp | 6 +++--- 10 files changed, 40 insertions(+), 20 deletions(-) diff --git a/README.md b/README.md index 7c9d44d9..d8e9f585 100644 --- a/README.md +++ b/README.md @@ -179,7 +179,7 @@ if (!parser.allocate_capacity(1024*1024)) { exit(1); } for (padded_string json : { string("[1, 2, 3]"), string("true"), string("[ true, false ]") }) { cout << "Parsing " << json.data() << " ..." << endl; auto [doc, error] = parser.parse(json); - if (error) { cerr << "Error: " << error_message(error) << endl; exit(1); } + if (error) { cerr << "Error: " << error << endl; exit(1); } doc.print_json(cout); cout << endl; } diff --git a/amalgamation.sh b/amalgamation.sh index 2a8bde09..b284d6e3 100755 --- a/amalgamation.sh +++ b/amalgamation.sh @@ -132,7 +132,7 @@ int main(int argc, char *argv[]) { if (error) { std::cout << "parse failed" << std::endl; std::cout << "error code: " << error << std::endl; - std::cout << error_message(error) << std::endl; + std::cout << error << std::endl; } else { std::cout << "parse valid" << std::endl; } @@ -150,7 +150,7 @@ int main(int argc, char *argv[]) { if (error) { std::cout << "parse_many failed" << std::endl; std::cout << "error code: " << error << std::endl; - std::cout << error_message(error) << std::endl; + std::cout << error << std::endl; } else { std::cout << "parse_many valid" << std::endl; } diff --git a/benchmark/parse_stream.cpp b/benchmark/parse_stream.cpp index 3aafd731..f312796c 100755 --- a/benchmark/parse_stream.cpp +++ b/benchmark/parse_stream.cpp @@ -96,7 +96,7 @@ int main (int argc, char *argv[]){ batch_size_res[i] = speedinGBs; if (error != simdjson::SUCCESS) { - std::wcerr << "Parsing failed with: " << simdjson::error_message(error) << std::endl; + std::wcerr << "Parsing failed with: " << error << std::endl; exit(1); } } @@ -132,7 +132,7 @@ int main (int argc, char *argv[]){ res.push_back(secs.count()); if (error != simdjson::SUCCESS) { - std::wcerr << "Parsing failed with: " << simdjson::error_message(error) << std::endl; + std::wcerr << "Parsing failed with: " << error << std::endl; exit(1); } diff --git a/include/simdjson/document.h b/include/simdjson/document.h index 3c8c86a5..2a1596be 100644 --- a/include/simdjson/document.h +++ b/include/simdjson/document.h @@ -949,7 +949,7 @@ public: * * document::parser parser; * for (auto [doc, error] : parser.parse_many(buf, len)) { - * if (error) { cerr << error_message(error) << endl; exit(1); } + * if (error) { cerr << error << endl; exit(1); } * cout << std::string(doc["title"]) << endl; * } * @@ -1011,7 +1011,7 @@ public: * * document::parser parser; * for (auto [doc, error] : parser.parse_many(buf, len)) { - * if (error) { cerr << error_message(error) << endl; exit(1); } + * if (error) { cerr << error << endl; exit(1); } * cout << std::string(doc["title"]) << endl; * } * @@ -1073,7 +1073,7 @@ public: * * document::parser parser; * for (auto [doc, error] : parser.parse_many(buf, len)) { - * if (error) { cerr << error_message(error) << endl; exit(1); } + * if (error) { cerr << error << endl; exit(1); } * cout << std::string(doc["title"]) << endl; * } * @@ -1134,7 +1134,7 @@ public: * * document::parser parser; * for (auto [doc, error] : parser.parse_many(buf, len)) { - * if (error) { cerr << error_message(error) << endl; exit(1); } + * if (error) { cerr << error << endl; exit(1); } * cout << std::string(doc["title"]) << endl; * } * diff --git a/include/simdjson/error.h b/include/simdjson/error.h index ceebe920..5f8174b7 100644 --- a/include/simdjson/error.h +++ b/include/simdjson/error.h @@ -44,10 +44,26 @@ enum error_code { */ inline const char *error_message(error_code error) noexcept; +/** + * Write the error message to the output stream + */ +inline std::ostream& operator<<(std::ostream& out, error_code error) noexcept; + +/** + * Exception thrown when an exception-supporting simdjson method is called + */ struct invalid_json : public std::exception { - invalid_json(error_code _error) noexcept : error{_error} { } - const char *what() const noexcept { return error_message(error); } - error_code error; + /** + * Create an exception from a simdjson error code. + * @param error The error code + */ + invalid_json(error_code error) noexcept : _error{error} { } + /** The error message */ + const char *what() const noexcept { return error_message(error()); } + error_code error() const noexcept { return _error; } +private: + /** The error code that was used */ + error_code _error; }; /** diff --git a/include/simdjson/inline/error.h b/include/simdjson/inline/error.h index 3a488bbf..b6367d47 100644 --- a/include/simdjson/inline/error.h +++ b/include/simdjson/inline/error.h @@ -50,6 +50,10 @@ inline const std::string &error_message(int error) noexcept { return internal::error_codes[error].message; } +inline std::ostream& operator<<(std::ostream& out, error_code error) noexcept { + return out << error_message(error); +} + } // namespace simdjson #endif // SIMDJSON_INLINE_ERROR_H diff --git a/singleheader/amalgamation_demo.cpp b/singleheader/amalgamation_demo.cpp index 5b920bb3..759dc41c 100755 --- a/singleheader/amalgamation_demo.cpp +++ b/singleheader/amalgamation_demo.cpp @@ -13,7 +13,7 @@ int main(int argc, char *argv[]) { if (error) { std::cout << "document::parse failed" << std::endl; std::cout << "error code: " << error << std::endl; - std::cout << error_message(error) << std::endl; + std::cout << error << std::endl; } else { std::cout << "document::parse valid" << std::endl; } diff --git a/tests/basictests.cpp b/tests/basictests.cpp index 394186a5..e2c5be91 100644 --- a/tests/basictests.cpp +++ b/tests/basictests.cpp @@ -254,7 +254,7 @@ static bool parse_json_message_issue467(char const* message, std::size_t len, si simdjson::padded_string str(message,len); for (auto [doc, error] : parser.parse_many(str, parser.capacity())) { if (error) { - std::cerr << "Failed with simdjson error= " << simdjson::error_message(error) << std::endl; + std::cerr << "Failed with simdjson error= " << error << std::endl; return false; } count++; @@ -784,7 +784,7 @@ namespace dom_api { if (uint64_t(doc["a"]) != 1) { cerr << "Expected uint64_t(doc[\"a\"]) to be 1, was " << uint64_t(doc["a"]) << endl; return false; } auto [val, error] = doc["d"]; - if (error != simdjson::NO_SUCH_FIELD) { cerr << "Expected NO_SUCH_FIELD error for uint64_t(doc[\"d\"]), got " << error_message(error) << endl; return false; } + if (error != simdjson::NO_SUCH_FIELD) { cerr << "Expected NO_SUCH_FIELD error for uint64_t(doc[\"d\"]), got " << error << endl; return false; } return true; } @@ -802,7 +802,7 @@ namespace dom_api { if (uint64_t(obj["a"]) != 1) { cerr << "Expected uint64_t(obj[\"a\"]) to be 1, was " << uint64_t(obj["a"]) << endl; return false; } auto [val, error] = obj["d"]; - if (error != simdjson::NO_SUCH_FIELD) { cerr << "Expected NO_SUCH_FIELD error for uint64_t(obj[\"d\"]), got " << error_message(error) << endl; return false; } + if (error != simdjson::NO_SUCH_FIELD) { cerr << "Expected NO_SUCH_FIELD error for uint64_t(obj[\"d\"]), got " << error << endl; return false; } return true; } diff --git a/tests/parse_many_test.cpp b/tests/parse_many_test.cpp index 4ef2e9fb..8ea7b6fb 100644 --- a/tests/parse_many_test.cpp +++ b/tests/parse_many_test.cpp @@ -99,7 +99,7 @@ bool validate(const char *dirname) { } else if (starts_with("pass", name) and (has_extension(extension1, name) or has_extension(extension2, name)) and error) { is_file_as_expected[i] = false; printf("warning: file %s should pass but it fails. Error is: %s\n", - name, simdjson::error_message(error).data()); + name, error_message(error)); printf("size of file in bytes: %zu \n", json.size()); everything_fine = false; } else if ( starts_with("fail", name) and (not starts_with("fail10.json", name)) and !error) { diff --git a/tests/readme_examples.cpp b/tests/readme_examples.cpp index db85cdbe..e3e4d2b9 100644 --- a/tests/readme_examples.cpp +++ b/tests/readme_examples.cpp @@ -8,7 +8,7 @@ void document_parse_error_code() { string json("[ 1, 2, 3 ]"); auto [doc, error] = document::parse(json); - if (error) { cerr << "Error: " << error_message(error) << endl; exit(1); } + if (error) { cerr << "Error: " << error << endl; exit(1); } if (!doc.print_json(cout)) { exit(1); } cout << endl; } @@ -51,7 +51,7 @@ void parser_parse() { for (padded_string json : { string("[1, 2, 3]"), string("true"), string("[ true, false ]") }) { cout << "Parsing " << json.data() << " ..." << endl; auto [doc, error] = parser.parse(json); - if (error) { cerr << "Error: " << error_message(error) << endl; exit(1); } + if (error) { cerr << "Error: " << error << endl; exit(1); } if (!doc.print_json(cout)) { exit(1); } cout << endl; } @@ -65,7 +65,7 @@ void parser_parse_many_error_code() { cout << "Parsing " << json.data() << " ..." << endl; document::parser parser; for (auto [doc, error] : parser.parse_many(json)) { - if (error) { cerr << "Error: " << error_message(error) << endl; exit(1); } + if (error) { cerr << "Error: " << error << endl; exit(1); } if (!doc.print_json(cout)) { exit(1); } cout << endl; }