6
6
#include < cstdlib>
7
7
#include < iomanip>
8
8
#include < nlohmann/json.hpp>
9
+ #include < typeinfo>
9
10
10
11
11
12
namespace nix {
12
13
using json = nlohmann::json;
13
14
// TODO: rename. It doesn't print.
14
- json printValueAsJSON (EvalState & state, bool strict,
15
+ json printValueAsJSON (EvalState & state, bool strict, bool replaceEvalErrors,
15
16
Value & v, const PosIdx pos, NixStringContext & context, bool copyToStore)
16
17
{
17
18
checkInterrupt ();
@@ -54,13 +55,27 @@ json printValueAsJSON(EvalState & state, bool strict,
54
55
break ;
55
56
}
56
57
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);
58
59
else {
59
60
out = json::object ();
60
61
for (auto & a : v.attrs ()->lexicographicOrder (state.symbols )) {
61
62
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));
63
64
} 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
+
64
79
e.addTrace (state.positions [a->pos ],
65
80
HintFmt (" while evaluating attribute '%1%'" , state.symbols [a->name ]));
66
81
throw ;
@@ -75,8 +90,9 @@ json printValueAsJSON(EvalState & state, bool strict,
75
90
int i = 0 ;
76
91
for (auto elem : v.listItems ()) {
77
92
try {
78
- out.push_back (printValueAsJSON (state, strict, *elem, pos, context, copyToStore));
93
+ out.push_back (printValueAsJSON (state, strict, replaceEvalErrors, *elem, pos, context, copyToStore));
79
94
} catch (Error & e) {
95
+ // TODO: Missing catch
80
96
e.addTrace (state.positions [pos],
81
97
HintFmt (" while evaluating list element at index %1%" , i));
82
98
throw ;
@@ -106,11 +122,11 @@ json printValueAsJSON(EvalState & state, bool strict,
106
122
return out;
107
123
}
108
124
109
- void printValueAsJSON (EvalState & state, bool strict,
125
+ void printValueAsJSON (EvalState & state, bool strict, bool replaceEvalErrors,
110
126
Value & v, const PosIdx pos, std::ostream & str, NixStringContext & context, bool copyToStore)
111
127
{
112
128
try {
113
- str << printValueAsJSON (state, strict, v, pos, context, copyToStore);
129
+ str << printValueAsJSON (state, strict, replaceEvalErrors, v, pos, context, copyToStore);
114
130
} catch (nlohmann::json::exception & e) {
115
131
throw JSONSerializationError (" JSON serialization error: %s" , e.what ());
116
132
}
0 commit comments