Skip to content

Commit

Permalink
Better error handling when using AutoDiscover
Browse files Browse the repository at this point in the history
  • Loading branch information
jvyden committed May 4, 2024
1 parent 68f71f9 commit 9dcfc31
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 14 deletions.
2 changes: 0 additions & 2 deletions Refresher/Accessors/ConsolePatchAccessor.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
using System.Net;
using System.Net.Sockets;
using FluentFTP;
using Refresher.Exceptions;

Expand Down
50 changes: 38 additions & 12 deletions Refresher/UI/PatchForm.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.ComponentModel;
using System.Diagnostics;
using System.Net.Sockets;
using System.Runtime.InteropServices;
using Eto.Drawing;
using Eto.Forms;
Expand Down Expand Up @@ -144,41 +145,66 @@ private void AutoDiscover(object? sender, EventArgs arg)
{
using HttpClient client = new();
client.BaseAddress = autodiscoverUri;

HttpResponseMessage response = client.GetAsync("/autodiscover").Result;
response.EnsureSuccessStatusCode();

Stream stream = response.Content.ReadAsStream();

JsonSerializer serializer = new();
using StreamReader streamReader = new(stream);
using JsonTextReader jsonReader = new(streamReader);

AutodiscoverResponse? autodiscover = serializer.Deserialize<AutodiscoverResponse>(jsonReader);
if (autodiscover == null) throw new InvalidOperationException("autoresponse was null");

string text = $"Successfully found a '{autodiscover.ServerBrand}' server at the given URL!\n\n" +
$"Server's recommended patch URL: {autodiscover.Url}\n" +
$"Custom digest key?: {(autodiscover.UsesCustomDigestKey.GetValueOrDefault() ? "Yes" : "No")}\n\n" +
$"Use this server's configuration?";

DialogResult result = MessageBox.Show(text, MessageBoxButtons.YesNo);
if (result != DialogResult.Yes) return;

this.UrlField.Text = autodiscover.Url;
this.PatchDigest = autodiscover.UsesCustomDigestKey ?? false;
this._usedAutoDiscover = true;
}
catch (HttpRequestException e)
catch (AggregateException aggregate)
{
if (e.StatusCode == null) throw;
MessageBox.Show($"AutoDiscover failed, because the server responded with {(int)e.StatusCode} {e.StatusCode}.");
aggregate.Handle(HandleAutoDiscoverError);
}
catch(Exception e)
{
SentrySdk.CaptureException(e);
MessageBox.Show($"AutoDiscover failed: {e}", MessageBoxType.Error);
if (!HandleAutoDiscoverError(e))
{
SentrySdk.CaptureException(e);
MessageBox.Show($"AutoDiscover failed: {e}", MessageBoxType.Error);
}
}
}

private static bool HandleAutoDiscoverError(Exception inner)
{
if (inner is HttpRequestException httpException)
{
if (httpException.StatusCode == null)
{
MessageBox.Show($"AutoDiscover failed, because we couldn't communicate with the server: {inner.Message}");
return true;
}

MessageBox.Show($"AutoDiscover failed, because the server responded with {(int)httpException.StatusCode} {httpException.StatusCode}.");
return true;
}

if (inner is SocketException)
{
MessageBox.Show($"AutoDiscover failed, because we couldn't communicate with the server: {inner.Message}");
return true;
}

return false;
}

/// <summary>
Expand Down

0 comments on commit 9dcfc31

Please sign in to comment.