Print the value in error: cannot coerce messages (#9553)
* Print the value in `error: cannot coerce` messages This extends the `error: cannot coerce a TYPE to a string` message to print the value that could not be coerced. This helps with debugging by making it easier to track down where the value is being produced from, especially in errors with deep or unhelpful stack traces. Co-authored-by: Valentin Gagarin <valentin.gagarin@tweag.io>
This commit is contained in:
@@ -26,9 +26,9 @@
|
||||
#include <unistd.h>
|
||||
#include <sys/time.h>
|
||||
#include <sys/resource.h>
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <functional>
|
||||
#include <strstream>
|
||||
|
||||
#include <sys/resource.h>
|
||||
#include <nlohmann/json.hpp>
|
||||
@@ -2286,7 +2286,7 @@ BackedStringView EvalState::coerceToString(
|
||||
return std::move(*maybeString);
|
||||
auto i = v.attrs->find(sOutPath);
|
||||
if (i == v.attrs->end()) {
|
||||
error("cannot coerce %1% to a string", showType(v))
|
||||
error("cannot coerce %1% to a string: %2%", showType(v), printValue(*this, v))
|
||||
.withTrace(pos, errorCtx)
|
||||
.debugThrow<TypeError>();
|
||||
}
|
||||
@@ -2332,7 +2332,7 @@ BackedStringView EvalState::coerceToString(
|
||||
}
|
||||
}
|
||||
|
||||
error("cannot coerce %1% to a string", showType(v))
|
||||
error("cannot coerce %1% to a string: %2%", showType(v), printValue(*this, v))
|
||||
.withTrace(pos, errorCtx)
|
||||
.debugThrow<TypeError>();
|
||||
}
|
||||
@@ -2691,8 +2691,10 @@ void EvalState::printStatistics()
|
||||
|
||||
std::string ExternalValueBase::coerceToString(const Pos & pos, NixStringContext & context, bool copyMore, bool copyToStore) const
|
||||
{
|
||||
std::strstream printed;
|
||||
print(printed);
|
||||
throw TypeError({
|
||||
.msg = hintfmt("cannot coerce %1% to a string", showType())
|
||||
.msg = hintfmt("cannot coerce %1% to a string: %2%", showType(), printed.str())
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user