Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Preference Header Exception Handling #483

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Microsoft.OData.Core/Microsoft.OData.Core.cs
Original file line number Diff line number Diff line change
Expand Up @@ -869,6 +869,7 @@ internal sealed class TextRes {
internal const string JsonReaderExtensions_CannotReadValueAsString = "JsonReaderExtensions_CannotReadValueAsString";
internal const string JsonReaderExtensions_CannotReadValueAsDouble = "JsonReaderExtensions_CannotReadValueAsDouble";
internal const string JsonReaderExtensions_UnexpectedInstanceAnnotationName = "JsonReaderExtensions_UnexpectedInstanceAnnotationName";
internal const string PreferenceHeader_IntegerExpected = "PreferenceHeader_IntegerExpected";

static TextRes loader = null;
ResourceManager resources;
Expand Down
2 changes: 2 additions & 0 deletions src/Microsoft.OData.Core/Microsoft.OData.Core.txt
Original file line number Diff line number Diff line change
Expand Up @@ -980,3 +980,5 @@ JsonReaderExtensions_CannotReadPropertyValueAsString=Cannot read the value '{0}'
JsonReaderExtensions_CannotReadValueAsString=Cannot read the value '{0}' as a quoted JSON string value.
JsonReaderExtensions_CannotReadValueAsDouble=Cannot read the value '{0}' as a double numeric value.
JsonReaderExtensions_UnexpectedInstanceAnnotationName=An unexpected instance annotation name '{0}' was found when reading from the JSON reader, In OData, Instance annotation name must start with @.

PreferenceHeader_IntegerExpected=Invalid value '{0}' for {1} preference header found. The {1} preference header requires an integer value.
19 changes: 14 additions & 5 deletions src/Microsoft.OData.Core/ODataPreferenceHeader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,13 @@ public int? Wait

if (wait != null)
{
return int.Parse(wait.Value, CultureInfo.InvariantCulture);
int value;
if (int.TryParse(wait.Value, out value))
{
return value;
}

throw new ODataException(Strings.PreferenceHeader_IntegerExpected(wait.Value, ODataPreferenceHeader.WaitPreferenceTokenName));
}

return null;
Expand Down Expand Up @@ -346,12 +352,15 @@ public int? MaxPageSize
{
var maxPageSizeHttpHeaderValueElement = this.Get(ODataMaxPageSizePreferenceToken);

// Should check maxPageSizeHttpHeaderValueElement.Value != null.
// Should do int.TryParse.
// If either of the above fail, should throw an ODataException for parsing, not a System.Exception (such as FormatException, etc.).
if (maxPageSizeHttpHeaderValueElement != null)
{
return int.Parse(maxPageSizeHttpHeaderValueElement.Value, CultureInfo.InvariantCulture);
int value;
if (int.TryParse(maxPageSizeHttpHeaderValueElement.Value, out value))
{
return value;
}

throw new ODataException(Strings.PreferenceHeader_IntegerExpected(maxPageSizeHttpHeaderValueElement.Value, ODataPreferenceHeader.ODataMaxPageSizePreferenceToken));
}

return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6131,6 +6131,13 @@ internal static string JsonReaderExtensions_UnexpectedInstanceAnnotationName(obj
return Microsoft.OData.Core.TextRes.GetString(Microsoft.OData.Core.TextRes.JsonReaderExtensions_UnexpectedInstanceAnnotationName, p0);
}

/// <summary>
/// A string like "Invalid value '{0}' for {1} preference header found. The {1} preference header requires an integer value."
/// </summary>
internal static string PreferenceHeader_IntegerExpected(object p0, object p1) {
return Microsoft.OData.Core.TextRes.GetString(Microsoft.OData.Core.TextRes.PreferenceHeader_IntegerExpected, p0, p1);
}

}

/// <summary>
Expand Down