libstore/derivations: expand error message for the first misparse

If the derivation does not start with D, do not return a simple
"expected string 'D'" error but a full error message.

This contributes towards #447.

Change-Id: Iee05f3918e4cc43e79f244ab2fd64a52cf2bb6d2
Signed-off-by: Raito Bezarius <raito@lix.systems>
This commit is contained in:
Raito Bezarius
2025-12-06 20:30:38 +00:00
parent d2ca1810b1
commit 7f32b71b6b
+13 -3
View File
@@ -142,6 +142,13 @@ static void expect(StringViewStream & str, std::string_view s)
str.remaining.remove_prefix(s.size());
}
static void expectWithErrorMsg(StringViewStream & str, std::string_view s, const char * errorMsg)
{
if (!str.remaining.starts_with(s)) {
throw FormatError(errorMsg);
}
str.remaining.remove_prefix(s.size());
}
/* Read a C-style string from stream `str'. */
static BackedStringView parseString(StringViewStream & str)
@@ -276,13 +283,16 @@ Derivation parseDerivation(
drv.name = name;
StringViewStream str{s};
expect(str, "D");
const char * genericErrorMsg = "derivation does not start with 'Derive' or 'DrvWithVersion'";
expectWithErrorMsg(str, "D", genericErrorMsg);
switch (str.peek()) {
case 'e':
expect(str, "erive(");
expectWithErrorMsg(str, "erive(", genericErrorMsg);
break;
case 'r': {
expect(str, "rvWithVersion(");
expectWithErrorMsg(str, "rvWithVersion(", genericErrorMsg);
auto versionS = parseString(str);
throw FormatError("Unknown derivation ATerm format version '%s'", *versionS);
}