Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Handle invalid UTF-8 sequences gracefully when converting structured log events to JSON. #52

Merged
merged 4 commits into from
Jan 20, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion src/clp_ffi_js/ir/StructuredIrStreamReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include <clp/ffi/ir_stream/Deserializer.hpp>
#include <clp/TraceableException.hpp>
#include <emscripten/val.h>
#include <json/single_include/nlohmann/json.hpp>
#include <spdlog/spdlog.h>

#include <clp_ffi_js/ClpFfiJsException.hpp>
Expand All @@ -26,6 +27,20 @@ namespace {
constexpr std::string_view cEmptyJsonStr{"{}"};
constexpr std::string_view cReaderOptionsLogLevelKey{"logLevelKey"};
constexpr std::string_view cReaderOptionsTimestampKey{"timestampKey"};

/**
* @see nlohmann::basic_json::dump
* Serializes a JSON value into a string with invalid UTF-8 sequences replaced rather than
* throwing an exception.
* @param json
* @return Serialized JSON.
*/
auto dump_json_with_replace(nlohmann::json const& json) -> std::string;

auto dump_json_with_replace(nlohmann::json const& json) -> std::string {
return json.dump(-1, ' ', false, nlohmann::json::error_handler_t::replace);
}

} // namespace

auto StructuredIrStreamReader::create(
Expand Down Expand Up @@ -132,7 +147,7 @@ auto StructuredIrStreamReader::decode_range(size_t begin_idx, size_t end_idx, bo
);
json_str = std::string(cEmptyJsonStr);
} else {
json_str = json_result.value().dump();
json_str = dump_json_with_replace(json_result.value());
}
return json_str;
};
Expand Down
Loading