66#include < cstdlib>
77#include < iomanip>
88#include < nlohmann/json.hpp>
9+ #include < typeinfo>
910
1011
1112namespace nix {
1213using json = nlohmann::json;
1314// TODO: rename. It doesn't print.
14- json printValueAsJSON (EvalState & state, bool strict,
15+ json printValueAsJSON (EvalState & state, bool strict, bool replaceEvalErrors,
1516 Value & v, const PosIdx pos, NixStringContext & context, bool copyToStore)
1617{
1718 checkInterrupt ();
@@ -54,13 +55,27 @@ json printValueAsJSON(EvalState & state, bool strict,
5455 break ;
5556 }
5657 if (auto i = v.attrs ()->get (state.sOutPath ))
57- return printValueAsJSON (state, strict, *i->value , i->pos , context, copyToStore);
58+ return printValueAsJSON (state, strict, replaceEvalErrors, *i->value , i->pos , context, copyToStore);
5859 else {
5960 out = json::object ();
6061 for (auto & a : v.attrs ()->lexicographicOrder (state.symbols )) {
6162 try {
62- out.emplace (state.symbols [a->name ], printValueAsJSON (state, strict, *a->value , a->pos , context, copyToStore));
63+ out.emplace (state.symbols [a->name ], printValueAsJSON (state, strict, replaceEvalErrors, *a->value , a->pos , context, copyToStore));
6364 } catch (Error & e) {
65+ std::cerr << " Caught an Error of type: " << typeid (e).name () << std::endl;
66+ // std::cerr << "Caught an Error of type: " << e.message() << std::endl;
67+ // std::cerr << "Caught an Error of type: " << e.what() << std::endl;
68+
69+ // TODO: Figure out what Error is here?
70+ // We seem to be not catching FileNotFoundError.
71+ bool isEvalError = dynamic_cast <EvalError *>(&e);
72+ bool isFileNotFoundError = dynamic_cast <FileNotFound *>(&e);
73+ // Restrict replaceEvalErrors only only evaluation errors
74+ if (replaceEvalErrors && (isEvalError || isFileNotFoundError)) {
75+ out.emplace (state.symbols [a->name ], " «evaluation error»" );
76+ continue ;
77+ }
78+
6479 e.addTrace (state.positions [a->pos ],
6580 HintFmt (" while evaluating attribute '%1%'" , state.symbols [a->name ]));
6681 throw ;
@@ -75,8 +90,9 @@ json printValueAsJSON(EvalState & state, bool strict,
7590 int i = 0 ;
7691 for (auto elem : v.listItems ()) {
7792 try {
78- out.push_back (printValueAsJSON (state, strict, *elem, pos, context, copyToStore));
93+ out.push_back (printValueAsJSON (state, strict, replaceEvalErrors, *elem, pos, context, copyToStore));
7994 } catch (Error & e) {
95+ // TODO: Missing catch
8096 e.addTrace (state.positions [pos],
8197 HintFmt (" while evaluating list element at index %1%" , i));
8298 throw ;
@@ -106,11 +122,11 @@ json printValueAsJSON(EvalState & state, bool strict,
106122 return out;
107123}
108124
109- void printValueAsJSON (EvalState & state, bool strict,
125+ void printValueAsJSON (EvalState & state, bool strict, bool replaceEvalErrors,
110126 Value & v, const PosIdx pos, std::ostream & str, NixStringContext & context, bool copyToStore)
111127{
112128 try {
113- str << printValueAsJSON (state, strict, v, pos, context, copyToStore);
129+ str << printValueAsJSON (state, strict, replaceEvalErrors, v, pos, context, copyToStore);
114130 } catch (nlohmann::json::exception & e) {
115131 throw JSONSerializationError (" JSON serialization error: %s" , e.what ());
116132 }
0 commit comments