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

Commit

Permalink
Improve JSON parser exception message
Browse files Browse the repository at this point in the history
  • Loading branch information
troydai committed May 15, 2015
1 parent 5779585 commit 6b0dc2b
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 26 deletions.
Expand Up @@ -21,6 +21,11 @@ public sealed class FileFormatException : Exception
public string Path { get; private set; }
public int Line { get; private set; }
public int Column { get; private set; }

public override string ToString()
{
return $"{Path}({Line},{Column}): Error: {base.ToString()}";
}

internal static FileFormatException Create(Exception exception, string filePath)
{
Expand Down Expand Up @@ -69,7 +74,6 @@ internal static FileFormatException Create(string message, string filePath)
return result;
}


internal static FileFormatException Create(string message, JsonValue jsonValue)
{
var result = new FileFormatException(message)
Expand Down
2 changes: 1 addition & 1 deletion src/Microsoft.Framework.Runtime.Hosting/Json/JsonBuffer.cs
Expand Up @@ -43,7 +43,7 @@ public JsonToken Read()
}
}

_token.Value = null;
_token.Value = ((char)first).ToString();
_token.Line = _line;
_token.Column = _column;

Expand Down
Expand Up @@ -69,7 +69,9 @@ private static JsonValue DeserializeInternal(JsonToken next, JsonBuffer buffer)
return new JsonNumber(next);
}

throw new JsonDeserializerException(JsonDeserializerResource.Format_UnexpectedToken(next.Value, next.Type), next);
throw new JsonDeserializerException(JsonDeserializerResource.Format_InvalidTokenExpectation(
next.Value, "'{', '[', true, false, null, JSON string, JSON number, or the end of the file"),
next);
}

private static JsonArray DeserializeArray(JsonToken head, JsonBuffer buffer)
Expand Down
Expand Up @@ -7,80 +7,77 @@ internal class JsonDeserializerResource
{
internal static string Format_IllegalCharacter(int value)
{
return string.Format("Illegal character {0} {0:X4}.", (char)value, value);
return $"Illegal character '{(char)value}' (Unicode hexadecimal {value:X4}).";
}

internal static string Format_IllegalTrailingCharacterAfterLiteral(int value, string literal)
{
return string.Format("Illegal character {0} ({1:X4}) after the literal name {2}.",
(char)value,
value,
literal);
return $"Illegal character '{(char)value}' (Unicode hexadecimal {value:X4}) after the literal name '{literal}'.";
}

internal static string Format_UnrecognizedLiteral(string literal)
{
return string.Format("Invalid JSON literal. {0} is not legal JSON literal.", literal);
}

internal static string Format_UnexpectedToken(string tokenValue, JsonTokenType type)
{
return string.Format("Unexpected token, type: {0} value: {1}.", type.ToString(), tokenValue);
return $"Invalid JSON literal. Expected literal '{literal}'.";
}

internal static string Format_DuplicateObjectMemberName(string memberName)
{
return Format_InvalidSyntax("JSON object", string.Format("Duplicate member name {0}.", memberName));
return Format_InvalidSyntax("JSON object", $"Duplicate member name '{memberName}'");
}

internal static string Format_InvalidFloatNumberFormat(string raw)
{
return string.Format("Invalid float number format: {0}", raw);
return $"Invalid float number format: {raw}";
}

internal static string Format_FloatNumberOverflow(string raw)
{
return string.Format("Float number overflow: {0}", raw);
return $"Float number overflow: {raw}";
}

internal static string Format_InvalidSyntax(string syntaxName, string issue)
{
return string.Format("Invalid {0} syntax. {1}.");
return $"Invalid {syntaxName} syntax. {issue}.";
}

internal static string Format_InvalidSyntaxNotExpected(string syntaxName, char unexpected)
{
return string.Format("Invalid {0} syntax. Unexpected '{1}'.", syntaxName, unexpected);
return $"Invalid {syntaxName} syntax. Unexpected '{unexpected}'.";
}

internal static string Format_InvalidSyntaxNotExpected(string syntaxName, string unexpected)
{
return string.Format("Invalid {0} syntax. Unexpected {1}.", syntaxName, unexpected);
return $"Invalid {syntaxName} syntax. Unexpected {unexpected}.";
}

internal static string Format_InvalidSyntaxExpectation(string syntaxName, char expectation)
{
return string.Format("Invalid {0} syntax. Expected '{1}'.", syntaxName, expectation);
return $"Invalid {syntaxName} syntax. Expected '{expectation}'.";
}

internal static string Format_InvalidSyntaxExpectation(string syntaxName, string expectation)
{
return string.Format("Invalid {0} syntax. Expected {1}.", syntaxName, expectation);
return $"Invalid {syntaxName} syntax. Expected {expectation}.";
}

internal static string Format_InvalidSyntaxExpectation(string syntaxName, char expectation1, char expectation2)
{
return string.Format("Invalid {0} syntax. Expected '{1}' or '{2}'.", syntaxName, expectation1, expectation2);
return $"Invalid {syntaxName} syntax. Expected '{expectation1}' or '{expectation2}'.";
}

internal static string Format_InvalidTokenExpectation(string tokenValue, string expectation)
{
return $"Unexpected token '{tokenValue}'. Expected {expectation}.";
}

internal static string Format_InvalidUnicode(string unicode)
{
return string.Format("Invalid Unicode [{0}]", unicode);
return $"Invalid Unicode [{unicode}]";
}

internal static string Format_UnfinishedJSON(string nextTokenValue)
{
return string.Format("Invalid JSON end. Unprocessed token {0}.", nextTokenValue);
return $"Invalid JSON end. Unprocessed token {nextTokenValue}.";
}

internal static string JSON_OpenString
Expand Down
2 changes: 1 addition & 1 deletion src/Microsoft.Framework.Runtime/Project.cs
Expand Up @@ -173,7 +173,7 @@ internal static Project GetProjectFromStream(Stream stream, string projectName,
if (rawProject == null)
{
throw FileFormatException.Create(
"The JSON file can't be deserialized to a Json object.",
"The JSON file can't be deserialized to a JSON object.",
projectPath);
}

Expand Down

0 comments on commit 6b0dc2b

Please sign in to comment.