Skip to content

Commit 86ca41a

Browse files
authored
Merge pull request #3639 from Flow-Launcher/websearch_issue
Fix OpenUri Exception
2 parents a865a3b + 25a62c8 commit 86ca41a

File tree

3 files changed

+49
-17
lines changed

3 files changed

+49
-17
lines changed

Flow.Launcher.Plugin/SharedCommands/SearchWeb.cs

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
using Microsoft.Win32;
2-
using System;
1+
using System;
2+
using System.ComponentModel;
33
using System.Diagnostics;
44
using System.IO;
55
using System.Linq;
6+
using Microsoft.Win32;
67

78
namespace Flow.Launcher.Plugin.SharedCommands
89
{
@@ -13,7 +14,7 @@ public static class SearchWeb
1314
{
1415
private static string GetDefaultBrowserPath()
1516
{
16-
string name = string.Empty;
17+
var name = string.Empty;
1718
try
1819
{
1920
using var regDefault = Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Windows\\Shell\\Associations\\UrlAssociations\\http\\UserChoice", false);
@@ -23,8 +24,7 @@ private static string GetDefaultBrowserPath()
2324
name = regKey.GetValue(null).ToString().ToLower().Replace("\"", "");
2425

2526
if (!name.EndsWith("exe"))
26-
name = name.Substring(0, name.LastIndexOf(".exe") + 4);
27-
27+
name = name[..(name.LastIndexOf(".exe") + 4)];
2828
}
2929
catch
3030
{
@@ -65,12 +65,21 @@ public static void OpenInBrowserWindow(this string url, string browserPath = "",
6565
{
6666
Process.Start(psi)?.Dispose();
6767
}
68-
catch (System.ComponentModel.Win32Exception)
68+
// This error may be thrown if browser path is incorrect
69+
catch (Win32Exception)
6970
{
70-
Process.Start(new ProcessStartInfo
71+
try
72+
{
73+
Process.Start(new ProcessStartInfo
74+
{
75+
FileName = url,
76+
UseShellExecute = true
77+
});
78+
}
79+
catch
7180
{
72-
FileName = url, UseShellExecute = true
73-
});
81+
throw; // Re-throw the exception if we cannot open the URL in the default browser
82+
}
7483
}
7584
}
7685

@@ -100,12 +109,20 @@ public static void OpenInBrowserTab(this string url, string browserPath = "", bo
100109
Process.Start(psi)?.Dispose();
101110
}
102111
// This error may be thrown if browser path is incorrect
103-
catch (System.ComponentModel.Win32Exception)
112+
catch (Win32Exception)
104113
{
105-
Process.Start(new ProcessStartInfo
114+
try
115+
{
116+
Process.Start(new ProcessStartInfo
117+
{
118+
FileName = url,
119+
UseShellExecute = true
120+
});
121+
}
122+
catch
106123
{
107-
FileName = url, UseShellExecute = true
108-
});
124+
throw; // Re-throw the exception if we cannot open the URL in the default browser
125+
}
109126
}
110127
}
111128
}

Flow.Launcher/Languages/en.xaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -473,6 +473,7 @@
473473
</system:String>
474474
<system:String x:Key="errorTitle">Error</system:String>
475475
<system:String x:Key="folderOpenError">An error occurred while opening the folder. {0}</system:String>
476+
<system:String x:Key="browserOpenError">An error occurred while opening the URL in the browser. Please check your Default Web Browser configuration in the General section of the settings window</system:String>
476477

477478
<!-- General Notice -->
478479
<system:String x:Key="pleaseWait">Please wait...</system:String>

Flow.Launcher/PublicAPIInstance.cs

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -399,13 +399,27 @@ private void OpenUri(Uri uri, bool? inPrivate = null)
399399

400400
var path = browserInfo.Path == "*" ? "" : browserInfo.Path;
401401

402-
if (browserInfo.OpenInTab)
402+
try
403403
{
404-
uri.AbsoluteUri.OpenInBrowserTab(path, inPrivate ?? browserInfo.EnablePrivate, browserInfo.PrivateArg);
404+
if (browserInfo.OpenInTab)
405+
{
406+
uri.AbsoluteUri.OpenInBrowserTab(path, inPrivate ?? browserInfo.EnablePrivate, browserInfo.PrivateArg);
407+
}
408+
else
409+
{
410+
uri.AbsoluteUri.OpenInBrowserWindow(path, inPrivate ?? browserInfo.EnablePrivate, browserInfo.PrivateArg);
411+
}
405412
}
406-
else
413+
catch (Exception e)
407414
{
408-
uri.AbsoluteUri.OpenInBrowserWindow(path, inPrivate ?? browserInfo.EnablePrivate, browserInfo.PrivateArg);
415+
var tabOrWindow = browserInfo.OpenInTab ? "tab" : "window";
416+
LogException(ClassName, $"Failed to open URL in browser {tabOrWindow}: {path}, {inPrivate ?? browserInfo.EnablePrivate}, {browserInfo.PrivateArg}", e);
417+
ShowMsgBox(
418+
GetTranslation("browserOpenError"),
419+
GetTranslation("errorTitle"),
420+
MessageBoxButton.OK,
421+
MessageBoxImage.Error
422+
);
409423
}
410424
}
411425
else

0 commit comments

Comments
 (0)