Skip to content

Commit

Permalink
Merge of #12174
Browse files Browse the repository at this point in the history
  • Loading branch information
mergify[bot] authored Jan 10, 2025
2 parents b9f89c8 + 4bf020d commit c906ba9
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/libexpr/value-to-json.cc
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,11 @@ json printValueAsJSON(EvalState & state, bool strict,
void printValueAsJSON(EvalState & state, bool strict,
Value & v, const PosIdx pos, std::ostream & str, NixStringContext & context, bool copyToStore)
{
str << printValueAsJSON(state, strict, v, pos, context, copyToStore);
try {
str << printValueAsJSON(state, strict, v, pos, context, copyToStore);
} catch (nlohmann::json::exception & e) {
throw JSONSerializationError("JSON serialization error: %s", e.what());
}
}

json ExternalValueBase::printValueAsJSON(EvalState & state, bool strict,
Expand Down
3 changes: 3 additions & 0 deletions src/libexpr/value-to-json.hh
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,7 @@ nlohmann::json printValueAsJSON(EvalState & state, bool strict,
void printValueAsJSON(EvalState & state, bool strict,
Value & v, const PosIdx pos, std::ostream & str, NixStringContext & context, bool copyToStore = true);


MakeError(JSONSerializationError, Error);

}
8 changes: 8 additions & 0 deletions tests/functional/lang/eval-fail-toJSON-non-utf-8.err.exp
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
error:
… while calling the 'toJSON' builtin
at /pwd/lang/eval-fail-toJSON-non-utf-8.nix:1:1:
1| builtins.toJSON "_invalid UTF-8: �_"
| ^
2|

error: JSON serialization error: [json.exception.type_error.316] invalid UTF-8 byte at index 16: 0xFF
1 change: 1 addition & 0 deletions tests/functional/lang/eval-fail-toJSON-non-utf-8.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
builtins.toJSON "_invalid UTF-8: ÿ_"

0 comments on commit c906ba9

Please sign in to comment.