Skip to content
Merged
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
11 changes: 10 additions & 1 deletion src/TraceClient/TraceClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,16 @@ public TraceClient(string baseUrl, string application, string version, string ke
{
throw new ArgumentException("version is longer than " + MaxLength + " characters", "version");
}
_endpoint = new Uri(baseUrl.Trim().TrimEnd('/') + "/api/metrics");
try
{
_endpoint = new Uri(baseUrl.Trim().TrimEnd('/') + "/api/metrics");
}
catch (UriFormatException malformed)
{
// UriFormatException is a FormatException, not an ArgumentException;
// rethrow so a malformed baseUrl fails the way the docs promise.
throw new ArgumentException("baseUrl is not a valid URL", "baseUrl", malformed);
}
_application = application.Trim();
_version = version.Trim();
_key = key == null ? "" : key.Trim();
Expand Down
12 changes: 12 additions & 0 deletions tests/TraceClient.Tests/TraceClientTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,18 @@ public void Constructor_RejectsAMissingBaseUrlOrApplication()
Assert.Throws<ArgumentException>(() => new TraceClient("http://x", "", "1.2.3"));
}

[Fact]
public void Constructor_RejectsAMalformedBaseUrlWithAnArgumentException()
{
// Exactly ArgumentException, as documented -- not the UriFormatException
// underneath, which a caller catching ArgumentException would miss.
ArgumentException thrown = Assert.Throws<ArgumentException>(
() => new TraceClient("not a url", "MyGame", "1.2.3", key: "k"));

Assert.Equal("baseUrl", thrown.ParamName);
Assert.IsType<UriFormatException>(thrown.InnerException);
}

[Fact]
public void Constructor_RejectsAMissingOrOverlongVersion()
{
Expand Down
Loading