Skip to content

Commit

Permalink
Throw BadRequest Exception in QuantitySearchValue on malformed input. F…
Browse files Browse the repository at this point in the history
…ixes #196, #21. (#264)
  • Loading branch information
JeffreyTaylor authored and brandonpollett committed Dec 12, 2018
1 parent f9e014d commit 3a41ba9
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 235 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

using System;
using System.Collections.Generic;
using Microsoft.Health.Fhir.Core.Features.Persistence;
using Microsoft.Health.Fhir.Core.Features.Search.SearchValues;
using Xunit;

Expand Down Expand Up @@ -66,7 +67,7 @@ public void GivenAnInvalidString_WhenParsing_ThenExceptionShouldBeThrown(string
[InlineData(@"abc|system|code")]
public void GivenAnInvalidNumber_WhenParsing_ThenExceptionShouldBeThrown(string s)
{
Assert.Throws<FormatException>(() => QuantitySearchValue.Parse(s));
Assert.Throws<BadRequestException>(() => QuantitySearchValue.Parse(s));
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using System.Globalization;
using System.Text;
using EnsureThat;
using Microsoft.Health.Fhir.Core.Features.Persistence;

namespace Microsoft.Health.Fhir.Core.Features.Search.SearchValues
{
Expand Down Expand Up @@ -63,7 +64,12 @@ public static QuantitySearchValue Parse(string s)
throw new FormatException(Core.Resources.MoreThanTwoTokenSeparatorSpecified);
}

decimal quantity = decimal.Parse(parts[0], NumberStyles.Number, CultureInfo.InvariantCulture);
decimal quantity;
if (!decimal.TryParse(parts[0], NumberStyles.Number, CultureInfo.InvariantCulture, out quantity))
{
throw new BadRequestException(string.Format(Core.Resources.MalformedSearchValue, parts[0]));
}

string system = parts.Count > 1 ? parts[1] : string.Empty;
string code = parts.Count > 2 ? parts[2] : string.Empty;

Expand Down
Loading

0 comments on commit 3a41ba9

Please sign in to comment.