Skip to content

Commit b3b577e

Browse files
Jack251970TBM13
authored andcommitted
Merge pull request Flow-Launcher#4004 from dcog989/404-local-file-crash
Crash when opening non-existent local file
1 parent d825c80 commit b3b577e

File tree

3 files changed

+29
-7
lines changed

3 files changed

+29
-7
lines changed

Flow.Launcher.Plugin/SharedCommands/FilesFolders.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,16 @@ public static bool FileExists(this string filePath)
150150
return File.Exists(filePath);
151151
}
152152

153+
/// <summary>
154+
/// Checks if a file or directory exists
155+
/// </summary>
156+
/// <param name="path"></param>
157+
/// <returns></returns>
158+
public static bool FileOrLocationExists(this string path)
159+
{
160+
return LocationExists(path) || FileExists(path);
161+
}
162+
153163
/// <summary>
154164
/// Open a directory window (using the OS's default handler, usually explorer)
155165
/// </summary>

Flow.Launcher/Languages/en.xaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -564,6 +564,7 @@
564564
<system:String x:Key="errorTitle">Error</system:String>
565565
<system:String x:Key="folderOpenError">An error occurred while opening the folder. {0}</system:String>
566566
<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>
567+
<system:String x:Key="fileNotFoundError">File or directory not found: {0}</system:String>
567568

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

Flow.Launcher/PublicAPIInstance.cs

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,6 @@ public void ChangeQuery(string query, bool requery = false)
6767
_mainVM.ChangeQueryText(query, requery);
6868
}
6969

70-
[System.Diagnostics.CodeAnalysis.SuppressMessage("Usage", "VSTHRD100:Avoid async void methods", Justification = "<Pending>")]
7170
public void RestartApp()
7271
{
7372
_mainVM.Hide();
@@ -384,6 +383,12 @@ public void OpenDirectory(string directoryPath, string fileNameOrFilePath = null
384383

385384
private void OpenUri(Uri uri, bool? inPrivate = null, bool forceBrowser = false)
386385
{
386+
if (uri.IsFile && !FilesFolders.FileOrLocationExists(uri.LocalPath))
387+
{
388+
ShowMsgError(GetTranslation("errorTitle"), string.Format(GetTranslation("fileNotFoundError"), uri.LocalPath));
389+
return;
390+
}
391+
387392
if (forceBrowser || uri.Scheme == Uri.UriSchemeHttp || uri.Scheme == Uri.UriSchemeHttps)
388393
{
389394
var browserInfo = _settings.CustomBrowser;
@@ -413,13 +418,19 @@ private void OpenUri(Uri uri, bool? inPrivate = null, bool forceBrowser = false)
413418
}
414419
else
415420
{
416-
Process.Start(new ProcessStartInfo()
421+
try
417422
{
418-
FileName = uri.AbsoluteUri,
419-
UseShellExecute = true
420-
})?.Dispose();
421-
422-
return;
423+
Process.Start(new ProcessStartInfo()
424+
{
425+
FileName = uri.AbsoluteUri,
426+
UseShellExecute = true
427+
})?.Dispose();
428+
}
429+
catch (Exception e)
430+
{
431+
LogException(ClassName, $"Failed to open: {uri.AbsoluteUri}", e);
432+
ShowMsgError(GetTranslation("errorTitle"), e.Message);
433+
}
423434
}
424435
}
425436

0 commit comments

Comments
 (0)