Skip to content

Commit

Permalink
Provide more descriptive error messages in RuntimeTypeNameParser (#…
Browse files Browse the repository at this point in the history
…9343)

Provide more descriptive error messages in RuntimeTypeNameParser
  • Loading branch information
ReubenBond authored Feb 12, 2025
1 parent db1c867 commit fda4f4f
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
12 changes: 9 additions & 3 deletions src/Orleans.Serialization/TypeSystem/RuntimeTypeNameParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ private static TypeSpec ParseInternal(ref BufferReader input)
}
}

coreType = new TupleTypeSpec(elements.ToArray(), input.TotalGenericArity);
coreType = new TupleTypeSpec([.. elements], input.TotalGenericArity);
}
else
{
Expand Down Expand Up @@ -378,7 +378,7 @@ public void ConsumeCharacter(char assertChar)
var c = Input[Index];
if (assertChar != c)
{
ThrowUnexpectedCharacter(assertChar, c);
ThrowUnexpectedCharacter(Input, Index, assertChar, c);
}

++Index;
Expand All @@ -396,7 +396,13 @@ public void ConsumeWhitespace()
}
}

private static void ThrowUnexpectedCharacter(char expected, char actual) => throw new InvalidOperationException($"Encountered unexpected character. Expected '{expected}', actual '{actual}'.");
private static void ThrowUnexpectedCharacter(ReadOnlySpan<char> value, int position, char expected, char actual)
{
var valueString = new string(value);
var posString = position > 0 ? new string(' ', position) : "";
var message = $"Encountered unexpected character. Expected '{expected}', actual '{actual}' in string:\n> {valueString}\n> {posString}^";
throw new InvalidOperationException(message);
}

private static void ThrowEndOfInput() => throw new InvalidOperationException("Tried to read past the end of the input");

Expand Down
13 changes: 11 additions & 2 deletions test/NonSilo.Tests/RuntimeTypeNameFormatterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ public void FormattedTypeNamesAreRecoverable()
/// </summary>
[Fact]
public void ParsedTypeNamesAreIdenticalToFormattedNames()

{
foreach (var type in _types)
{
Expand All @@ -85,7 +84,17 @@ public void ParsedTypeNamesAreIdenticalToFormattedNames()
_output.WriteLine($"Reparsed : {reparsed}");
Assert.Equal(formatted, reparsed.Format());
}
}
}

[Fact]
public void InvalidNamesThrowDescriptiveErrorMessage()
{
var input = "MalformedName[`";
var exception = Assert.Throws<InvalidOperationException>(() => RuntimeTypeNameParser.Parse(input));
_output.WriteLine(exception.Message);
Assert.Contains(input, exception.Message);
Assert.Contains("^", exception.Message); // Position indicator
}

public class Inner<T>
{
Expand Down

0 comments on commit fda4f4f

Please sign in to comment.