Skip to content

Commit 5c64de1

Browse files
committed
Fix: Fixed SocketException in RequireGitAuthenticationAsync
Update GitHelpers.cs
1 parent b12f1be commit 5c64de1

File tree

1 file changed

+41
-32
lines changed

1 file changed

+41
-32
lines changed

src/Files.App/Utils/Git/GitHelpers.cs

Lines changed: 41 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using Microsoft.Extensions.Logging;
99
using System.Net.Http;
1010
using System.Net.Http.Json;
11+
using System.Net.Sockets;
1112
using System.Text.Json;
1213
using System.Text.RegularExpressions;
1314

@@ -582,50 +583,58 @@ public static async Task RequireGitAuthenticationAsync()
582583

583584
while (!loginCTS.Token.IsCancellationRequested && pending && expiresIn > 0)
584585
{
585-
var loginResponse = await client.PostAsync(
586+
try
587+
{
588+
var loginResponse = await client.PostAsync(
586589
$"https://github.com/login/oauth/access_token?client_id={_clientId}&device_code={deviceCode}&grant_type=urn:ietf:params:oauth:grant-type:device_code",
587590
new StringContent(""));
588591

589-
expiresIn -= interval;
590-
591-
if (!loginResponse.IsSuccessStatusCode)
592-
{
593-
dialog.Hide();
594-
break;
595-
}
592+
expiresIn -= interval;
596593

597-
var loginJsonContent = await loginResponse.Content.ReadFromJsonAsync<JsonDocument>();
598-
if (loginJsonContent is null)
599-
{
600-
dialog.Hide();
601-
break;
602-
}
594+
if (!loginResponse.IsSuccessStatusCode)
595+
{
596+
dialog.Hide();
597+
break;
598+
}
603599

604-
if (loginJsonContent.RootElement.TryGetProperty("error", out var error))
605-
{
606-
if (error.GetString() == "authorization_pending")
600+
var loginJsonContent = await loginResponse.Content.ReadFromJsonAsync<JsonDocument>();
601+
if (loginJsonContent is null)
607602
{
608-
await Task.Delay(TimeSpan.FromSeconds(interval));
609-
continue;
603+
dialog.Hide();
604+
break;
610605
}
611606

612-
dialog.Hide();
613-
break;
614-
}
607+
if (loginJsonContent.RootElement.TryGetProperty("error", out var error))
608+
{
609+
if (error.GetString() == "authorization_pending")
610+
{
611+
await Task.Delay(TimeSpan.FromSeconds(interval));
612+
continue;
613+
}
615614

616-
var token = loginJsonContent.RootElement.GetProperty("access_token").GetString();
617-
if (token is null)
618-
continue;
615+
dialog.Hide();
616+
break;
617+
}
619618

620-
pending = false;
619+
var token = loginJsonContent.RootElement.GetProperty("access_token").GetString();
620+
if (token is null)
621+
continue;
622+
623+
pending = false;
621624

622-
CredentialsHelpers.SavePassword(
623-
GIT_RESOURCE_NAME,
624-
GIT_RESOURCE_USERNAME,
625-
token);
625+
CredentialsHelpers.SavePassword(
626+
GIT_RESOURCE_NAME,
627+
GIT_RESOURCE_USERNAME,
628+
token);
626629

627-
viewModel.Subtitle = "AuthorizationSucceded".GetLocalizedResource();
628-
viewModel.LoginConfirmed = true;
630+
viewModel.Subtitle = "AuthorizationSucceded".GetLocalizedResource();
631+
viewModel.LoginConfirmed = true;
632+
}
633+
catch (SocketException ex)
634+
{
635+
_logger.LogWarning(ex.Message);
636+
return;
637+
}
629638
}
630639

631640
await loginDialogTask;

0 commit comments

Comments
 (0)