Skip to content

Commit d57901a

Browse files
gautamdshethGautam Sheth
andauthored
Enhance error handling for clipboard operations and improve error messages for browser opening failures (#4992)
* Enhance error handling for clipboard operations and improve error messages for browser opening failures * Update error messages for Linux web page opening failures to provide a more relevant URL --------- Co-authored-by: Gautam Sheth <gautam.sheth@staffbase.com>
1 parent b1ca466 commit d57901a

File tree

4 files changed

+25
-9
lines changed

4 files changed

+25
-9
lines changed

src/Commands/AzureAD/RegisterAzureADApp.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -659,7 +659,13 @@ private void StartConsentFlow(string loginEndPoint, AzureADApp azureApp, string
659659
{
660660
using (var authManager = AuthenticationManager.CreateWithDeviceLogin(azureApp.AppId, Tenant, (deviceCodeResult) =>
661661
{
662-
ClipboardService.SetText(deviceCodeResult.UserCode);
662+
try
663+
{
664+
ClipboardService.SetText(deviceCodeResult.UserCode);
665+
}
666+
catch
667+
{
668+
}
663669
Host.UI.WriteWarningLine($"\n\nPlease login.\n\nWe opened a browser and navigated to {deviceCodeResult.VerificationUrl}\n\nEnter code: {deviceCodeResult.UserCode} (we copied this code to your clipboard)\n\nNOTICE: close the browser tab after you authenticated successfully to continue the process.");
664670
BrowserHelper.OpenBrowserForInteractiveLogin(deviceCodeResult.VerificationUrl, BrowserHelper.FindFreeLocalhostRedirectUri(), cancellationTokenSource);
665671
return Task.FromResult(0);

src/Commands/AzureAD/RegisterEntraIDAppForInteractiveLogin.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -496,12 +496,17 @@ private void StartConsentFlow(string loginEndPoint, AzureADApp azureApp, string
496496
// {
497497
if (!Stopping)
498498
{
499-
500499
if (ParameterSpecified(nameof(DeviceLogin)))
501500
{
502501
using (var authManager = AuthenticationManager.CreateWithDeviceLogin(azureApp.AppId, Tenant, (deviceCodeResult) =>
503502
{
504-
ClipboardService.SetText(deviceCodeResult.UserCode);
503+
try
504+
{
505+
ClipboardService.SetText(deviceCodeResult.UserCode);
506+
}
507+
catch
508+
{
509+
}
505510
Host.UI.WriteWarningLine($"\n\nPlease login.\n\nWe opened a browser and navigated to {deviceCodeResult.VerificationUrl}\n\nEnter code: {deviceCodeResult.UserCode} (we copied this code to your clipboard)\n\nNOTICE: close the browser tab after you authenticated successfully to continue the process.");
506511
BrowserHelper.OpenBrowserForInteractiveLogin(deviceCodeResult.VerificationUrl, BrowserHelper.FindFreeLocalhostRedirectUri(), cancellationTokenSource);
507512
return Task.FromResult(0);
@@ -523,7 +528,6 @@ private void StartConsentFlow(string loginEndPoint, AzureADApp azureApp, string
523528
authManager.GetAccessToken(resource, Microsoft.Identity.Client.Prompt.Consent);
524529
}
525530
}
526-
527531
// Write results
528532

529533
WriteObject($"App created. You can now connect to your tenant using Connect-PnPOnline -Url <yourtenanturl> -ClientId {azureApp.AppId}");

src/Commands/Base/PnPConnection.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,13 @@ internal static PnPConnection CreateWithDeviceLogin(string clientId, string url,
280280
{
281281
authManager = Framework.AuthenticationManager.CreateWithDeviceLogin(clientId, tenantId, (deviceCodeResult) =>
282282
{
283-
ClipboardService.SetText(deviceCodeResult.UserCode);
283+
try
284+
{
285+
ClipboardService.SetText(deviceCodeResult.UserCode);
286+
}
287+
catch
288+
{
289+
}
284290
messageWriter.LogWarning($"\n\nCode {deviceCodeResult.UserCode} has been copied to your clipboard and a new tab in the browser has been opened. Please paste this code in there and proceed.\n\n");
285291
BrowserHelper.OpenBrowserForInteractiveLogin(deviceCodeResult.VerificationUrl, BrowserHelper.FindFreeLocalhostRedirectUri(), cancellationTokenSource);
286292

src/Commands/Utilities/BrowserHelper.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ internal static void OpenBrowserForInteractiveLogin(string url, int port, Cancel
198198
string sudoUser = Environment.GetEnvironmentVariable("SUDO_USER");
199199
if (!string.IsNullOrWhiteSpace(sudoUser))
200200
{
201-
throw new MsalClientException(MsalError.LinuxXdgOpen);
201+
throw new MsalClientException(MsalError.LinuxXdgOpen, "Unable to open a web page using xdg-open, gnome-open, kfmclient or wslview tools in sudo mode. Please run the process as non-sudo user.");
202202
}
203203
try
204204
{
@@ -215,12 +215,12 @@ internal static void OpenBrowserForInteractiveLogin(string url, int port, Cancel
215215

216216
if (!opened)
217217
{
218-
throw new MsalClientException(MsalError.LinuxXdgOpen);
218+
throw new MsalClientException(MsalError.LinuxXdgOpen, "Unable to open a web page using xdg-open, gnome-open, kfmclient or wslview tools. See inner exception for details. Possible causes for this error are: tools are not installed or they cannot open a URL. Make sure you can open a web page by invoking from a terminal: xdg-open https://aka.ms/pnp/powershell");
219219
}
220220
}
221-
catch
221+
catch (Exception ex)
222222
{
223-
throw new MsalClientException(MsalError.LinuxXdgOpen);
223+
throw new MsalClientException(MsalError.LinuxXdgOpen, "Unable to open a web page using xdg-open, gnome-open, kfmclient or wslview tools. See inner exception for details. Possible causes for this error are: tools are not installed or they cannot open a URL. Make sure you can open a web page by invoking from a terminal: xdg-open https://aka.ms/pnp/powershell", ex);
224224
}
225225
}
226226
else if (OperatingSystem.IsMacOS())

0 commit comments

Comments
 (0)