Skip to content

Commit

Permalink
Catch exception when network is disabled. (#750)
Browse files Browse the repository at this point in the history
* Catch exception when network is disabled.

* Check task.IsFaulted

* Do not catch all

* let it throw on critical exception

* no message

* no message

* no message
  • Loading branch information
Deren-Liao authored Jul 14, 2017
1 parent d38b124 commit 440f6c4
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 14 deletions.
13 changes: 10 additions & 3 deletions GoogleCloudExtension/GoogleAnalyticsUtils/HitSender.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,18 @@ public HitSender(bool debug, string userAgent)
public async void SendHitData(Dictionary<string, string> hitData)
{
var client = _httpClient.Value;
using (var form = new FormUrlEncodedContent(hitData))
using (var response = await client.PostAsync(_serverUrl, form).ConfigureAwait(false))
try
{
DebugPrintAnalyticsOutput(response.Content.ReadAsStringAsync(), hitData);
using (var form = new FormUrlEncodedContent(hitData))
using (var response = await client.PostAsync(_serverUrl, form).ConfigureAwait(false))
{
DebugPrintAnalyticsOutput(response.Content.ReadAsStringAsync(), hitData);
}
}
catch (Exception ex) when (
ex is HttpRequestException ||
ex is TaskCanceledException ) // timeout
{ }
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,8 @@ private void AwaitForValue()
{
_valueSource.ContinueWith((t) =>
{
if (t.IsCompleted)
{
Value = t.Result;
}
// Value is initiated with defaultValue at constructor.
Value = AsyncPropertyUtils.GetTaskResultSafe(t, defaultValue: Value);
_completionSource.Value.SetResult(true);
RaiseAllPropertyChanged();
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

using Microsoft.VisualStudio;
using System;
using System.Threading.Tasks;

Expand All @@ -31,7 +32,9 @@ public static class AsyncPropertyUtils
public static AsyncProperty<T> CreateAsyncProperty<TIn, T>(
Task<TIn> valueSource, Func<TIn, T> func, T defaultValue = default(T))
{
return new AsyncProperty<T>(valueSource.ContinueWith(t => func(t.Result)), defaultValue);
return new AsyncProperty<T>(
valueSource.ContinueWith(t => func(GetTaskResultSafe(t))),
defaultValue);
}

public static AsyncProperty<T> CreateAsyncProperty<T>(Task<T> valueSource, T defaultValue = default(T))
Expand All @@ -43,5 +46,17 @@ public static AsyncProperty CreateAsyncProperty(Task sourceTask)
{
return new AsyncProperty(sourceTask);
}

public static TIn GetTaskResultSafe<TIn>(Task<TIn> task, TIn defaultValue = default(TIn))
{
try
{
return task.Result;
}
catch (Exception ex) when (!ErrorHandler.IsCriticalException(ex))
{
return defaultValue;
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
Expand Down Expand Up @@ -38,6 +38,7 @@
<AssemblyOriginatorKeyFile>Key.snk</AssemblyOriginatorKeyFile>
</PropertyGroup>
<ItemGroup>
<Reference Include="Microsoft.VisualStudio.Shell.14.0, Version=14.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL" />
<Reference Include="Newtonsoft.Json, Version=10.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>..\packages\Newtonsoft.Json.10.0.2\lib\net45\Newtonsoft.Json.dll</HintPath>
<Private>True</Private>
Expand Down Expand Up @@ -90,4 +91,4 @@
<Target Name="AfterBuild">
</Target>
-->
</Project>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -262,10 +262,10 @@ private void UpdateUserProfile()
if (_plusDataSource.Value != null)
{
var profileTask = _plusDataSource.Value.GetProfileAsync();
ProfilePictureAsync = AsyncPropertyUtils.CreateAsyncProperty(profileTask, x => x.Image.Url);
ProfilePictureAsync = AsyncPropertyUtils.CreateAsyncProperty(profileTask, x => x?.Image.Url);
ProfileNameAsync = AsyncPropertyUtils.CreateAsyncProperty(
profileTask,
x => x.Emails.FirstOrDefault()?.Value,
x => x?.Emails.FirstOrDefault()?.Value,
Resources.CloudExplorerLoadingMessage);
}
else
Expand Down Expand Up @@ -382,6 +382,11 @@ private async void ResetCredentials()
button.IsEnabled = IsNotEmptyState;
}
}
// Catch all, otherwise it terminates Visual Studio
catch (Exception ex) when (!ErrorHandlerUtils.IsCriticalException(ex))
{
Debug.WriteLine($"Exception at CloudExplorerViewModel.ResetCredentials. {ex}");
}
finally
{
IsBusy = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ public UserAccountViewModel(UserAccount userAccount)
var personTask = dataSource.GetProfileAsync();

// TODO: Show the default image while it is being loaded.
ProfilePictureAsync = AsyncPropertyUtils.CreateAsyncProperty(personTask, x => x.Image.Url);
NameAsync = AsyncPropertyUtils.CreateAsyncProperty(personTask, x => x.DisplayName, Resources.CloudExplorerLoadingMessage);
ProfilePictureAsync = AsyncPropertyUtils.CreateAsyncProperty(personTask, x => x?.Image.Url);
NameAsync = AsyncPropertyUtils.CreateAsyncProperty(personTask, x => x?.DisplayName, Resources.CloudExplorerLoadingMessage);
}
}
}

0 comments on commit 440f6c4

Please sign in to comment.