Skip to content

Commit fe550e9

Browse files
committed
Add more error information for invalid dictionaries
#33
1 parent da2bf20 commit fe550e9

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

BencodeNET/Parsing/BDictionaryParser.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,16 @@ public override BDictionary Parse(BencodeStream stream)
8080
catch (BencodeException ex)
8181
{
8282
throw InvalidException(
83-
"Could not parse dictionary value. There needs to be a value for each key.",
83+
$"Could not parse dictionary value for the key '{key}'. There needs to be a value for each key.",
8484
ex, startPosition);
8585
}
8686

87+
if (dictionary.ContainsKey(key))
88+
{
89+
throw InvalidException(
90+
$"The dictionary already contains the key '{key}'. Duplicate keys are not supported.", startPosition);
91+
}
92+
8793
dictionary.Add(key, value);
8894
}
8995

@@ -96,6 +102,12 @@ public override BDictionary Parse(BencodeStream stream)
96102
return dictionary;
97103
}
98104

105+
private static InvalidBencodeException<BDictionary> InvalidException(string message, long startPosition)
106+
{
107+
return new InvalidBencodeException<BDictionary>(
108+
$"{message} The dictionary starts at position {startPosition}.", startPosition);
109+
}
110+
99111
private static InvalidBencodeException<BDictionary> InvalidException(string message, Exception inner, long startPosition)
100112
{
101113
return new InvalidBencodeException<BDictionary>(

0 commit comments

Comments
 (0)