Skip to content
This repository has been archived by the owner on Dec 18, 2018. It is now read-only.

Commit

Permalink
Handle short file json parse errors
Browse files Browse the repository at this point in the history
  • Loading branch information
HaoK committed Aug 23, 2016
1 parent e82c7aa commit 74c68cd
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,18 +52,21 @@ public override void Load(Stream stream)

private static string RetrieveErrorContext(JsonReaderException e, IEnumerable<string> fileContent)
{
string errorLine;
string errorLine = null;
if (e.LineNumber >= 2)
{
var errorContext = fileContent.Skip(e.LineNumber - 2).Take(2).ToList();
errorLine = errorContext[0].Trim() + Environment.NewLine + errorContext[1].Trim();
// Handle situations when the line number reported is out of bounds
if (errorContext.Count() >= 2)

This comment has been minimized.

Copy link
@khellang

khellang Aug 24, 2016

Did you mean to use the LINQ method here? There's already a Count property on List<T> 😉

This comment has been minimized.

Copy link
@HaoK

HaoK Aug 25, 2016

Author Member

Doh, yeah I'll fix thanks :)

{
errorLine = errorContext[0].Trim() + Environment.NewLine + errorContext[1].Trim();
}
}
else
if (string.IsNullOrEmpty(errorLine))
{
var possibleLineContent = fileContent.Skip(e.LineNumber - 1).FirstOrDefault();
errorLine = possibleLineContent ?? string.Empty;
}

return errorLine;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,18 @@ public void ThrowExceptionWhenUnexpectedEndFoundBeforeFinishParsing()
Assert.NotNull(exception.Message);
}

[Fact]
public void ThrowExceptionWhenMissingCurlyBeforeFinishParsing()
{
var json = @"
{
'Data': {
";

var exception = Assert.Throws<FormatException>(() => LoadProvider(json));
Assert.Contains("Could not parse the JSON file.", exception.Message);
}

[Fact]
public void ThrowExceptionWhenPassingNullAsFilePath()
{
Expand Down

0 comments on commit 74c68cd

Please sign in to comment.