Skip to content
This repository has been archived by the owner on Jan 27, 2025. It is now read-only.

Commit

Permalink
metadata command shows ugly stack trace when used on a package that d…
Browse files Browse the repository at this point in the history
…oesn't exist. Fixes #54
  • Loading branch information
gdivis committed Apr 26, 2019
1 parent a15ee49 commit d4822f6
Showing 1 changed file with 26 additions and 4 deletions.
30 changes: 26 additions & 4 deletions src/dotnet/upack/Metadata.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,33 @@ public override async Task<int> RunAsync(CancellationToken cancellationToken)
}

JObject data;
using (var stream = await client.GetPackageFileStreamAsync(packageId, version, string.IsNullOrEmpty(this.FilePath) ? "upack.json" : this.FilePath, cancellationToken))
using (var reader = new StreamReader(stream, Encoding.UTF8, true, 4096, true))
using (var jsonReader = new JsonTextReader(reader) { CloseInput = false })
try
{
using (var stream = await client.GetPackageFileStreamAsync(packageId, version, string.IsNullOrEmpty(this.FilePath) ? "upack.json" : this.FilePath, cancellationToken))
using (var reader = new StreamReader(stream, Encoding.UTF8, true, 4096, true))
using (var jsonReader = new JsonTextReader(reader) { CloseInput = false })
{
data = await JObject.LoadAsync(jsonReader, cancellationToken);
}
}
catch (WebException ex) when (ex.Response is HttpWebResponse r)
{
data = await JObject.LoadAsync(jsonReader, cancellationToken);
var error = $"Server returned {(int)r.StatusCode}: ";
if (string.Equals(r.ContentType, "text/plain", StringComparison.OrdinalIgnoreCase))
{
using (var reader = new StreamReader(r.GetResponseStream(), Encoding.UTF8))
{
var buffer = new char[1000];
reader.Read(buffer, 0, buffer.Length);
error += new string(buffer);
}
}
else
{
error += r.StatusDescription;
}

throw new UpackException(error, ex);
}

foreach (var p in data.Properties())
Expand Down

0 comments on commit d4822f6

Please sign in to comment.