Skip to content

Commit

Permalink
Standardised exception on JSON path errors
Browse files Browse the repository at this point in the history
  • Loading branch information
MarkMpn committed May 7, 2024
1 parent b355829 commit 9b83d46
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 6 deletions.
5 changes: 5 additions & 0 deletions MarkMpn.Sql4Cds.Engine/Ado/Sql4CdsError.cs
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,11 @@ internal static Sql4CdsError JsonNotArrayOrObject(TSqlFragment fragment)
return Create(13611, fragment);
}

internal static Sql4CdsError JsonPathFormatError(char c, int index)
{
return Create(13607, null, Collation.USEnglish.ToSqlString(c.ToString()), (SqlInt32)index);
}

internal static Sql4CdsError XQueryMissingVariable(string name)
{
return Create(9501, null, (SqlInt32)name.Length, Collation.USEnglish.ToSqlString(name));
Expand Down
3 changes: 2 additions & 1 deletion MarkMpn.Sql4Cds.Engine/ExpressionFunctions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1181,7 +1181,7 @@ public static SqlString FormatMessage(SqlString message, ExpressionExecutionCont
if (message.IsNull)
return SqlString.Null;

var regex = new Regex("%(?<flag>[-+0# ])?(?<width>([0-9]+|\\*))?(\\.(?<precision>([0-9]+|\\*)))?(?<size>h|l)?(?<type>[diosuxX]|I64d|S_MSG)");
var regex = new Regex("%(?<flag>[-+0# ])?(?<width>([0-9]+|\\*))?(\\.(?<precision>([0-9]+|\\*)))?(?<size>h|l)?(?<type>[diosuxXc]|I64d|S_MSG)");
var paramIndex = 0;

T GetValue<T>()
Expand Down Expand Up @@ -1290,6 +1290,7 @@ T GetValue<T>()

case "s":
case "S_MSG":
case "c":
var strValue = GetValue<SqlString>();

if (strValue.IsNull)
Expand Down
22 changes: 17 additions & 5 deletions MarkMpn.Sql4Cds.Engine/JsonPath.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ private static JsonPathPart[] Parse(string expression, out JsonPathMode mode)
i++;

if (i == expression.Length)
throw new JsonException($"Invalid JSON path - missing property name after '.' at end of '{expression}'");
throw new JsonPathException(Sql4CdsError.JsonPathFormatError(expression[i], i + 1));

if (expression[i] == '"')
{
Expand Down Expand Up @@ -130,7 +130,7 @@ private static JsonPathPart[] Parse(string expression, out JsonPathMode mode)
else
{
// Error
throw new JsonException($"Invalid JSON path - invalid property name at index {i} of '{expression}'");
throw new JsonPathException(Sql4CdsError.JsonPathFormatError(expression[i], i + 1));
}
}
else if (expression[i] == '[')
Expand All @@ -139,20 +139,20 @@ private static JsonPathPart[] Parse(string expression, out JsonPathMode mode)
var end = expression.IndexOf(']', i);

if (end == -1)
throw new JsonException($"Invalid JSON path - missing closing bracket for indexer at index {i} of '{expression}'");
throw new JsonPathException(Sql4CdsError.JsonPathFormatError('.', expression.Length));

var indexStr = expression.Substring(i + 1, end - i - 1);

if (!UInt32.TryParse(indexStr, out var index))
throw new JsonException($"Invalid JSON path - invalid indexer at index {i} of '{expression}'");
throw new JsonPathException(Sql4CdsError.JsonPathFormatError(expression[i+1], i + 2));

parts.Add(new ArrayElementJsonPathPart(index));
i = end ;
}
else
{
// Error
throw new JsonException($"JSON path is not properly formatted. Unexpected character '{expression[i]}' is found at position {i}");
throw new JsonPathException(Sql4CdsError.JsonPathFormatError(expression[i], i + 1));
}
}

Expand Down Expand Up @@ -275,4 +275,16 @@ enum JsonPathMode
Lax,
Strict
}

class JsonPathException : ApplicationException, ISql4CdsErrorException
{
private readonly Sql4CdsError[] _errors;

public JsonPathException(Sql4CdsError error) : base(error.Message)
{
_errors = new[] { error };
}

public IReadOnlyList<Sql4CdsError> Errors => _errors;
}
}

0 comments on commit 9b83d46

Please sign in to comment.