Skip to content

Commit b1557dd

Browse files
committed
Add try-catch for preview panel operation
1 parent ce07299 commit b1557dd

File tree

1 file changed

+85
-42
lines changed

1 file changed

+85
-42
lines changed

Plugins/Flow.Launcher.Plugin.Explorer/Views/PreviewPanel.xaml.cs

Lines changed: 85 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ namespace Flow.Launcher.Plugin.Explorer.Views;
1616

1717
public partial class PreviewPanel : UserControl, INotifyPropertyChanged
1818
{
19+
private static readonly string ClassName = nameof(PreviewPanel);
20+
1921
private string FilePath { get; }
2022
public string FileSize { get; } = "";
2123
public string CreatedAt { get; } = "";
@@ -87,78 +89,119 @@ private async Task LoadImageAsync()
8789

8890
public static string GetFileSize(string filePath)
8991
{
90-
var fileInfo = new FileInfo(filePath);
91-
return ResultManager.ToReadableSize(fileInfo.Length, 2);
92+
try
93+
{
94+
var fileInfo = new FileInfo(filePath);
95+
return ResultManager.ToReadableSize(fileInfo.Length, 2);
96+
}
97+
catch (Exception e)
98+
{
99+
Main.Context.API.LogException(ClassName, $"Failed to get file size for {filePath}", e);
100+
return Main.Context.API.GetTranslation("plugin_explorer_plugin_tooltip_more_info_unknown");
101+
}
92102
}
93103

94104
public static string GetFileCreatedAt(string filePath, string previewPanelDateFormat, string previewPanelTimeFormat, bool showFileAgeInPreviewPanel)
95105
{
96-
var createdDate = File.GetCreationTime(filePath);
97-
var formattedDate = createdDate.ToString(
98-
$"{previewPanelDateFormat} {previewPanelTimeFormat}",
99-
CultureInfo.CurrentCulture
100-
);
101-
102-
var result = formattedDate;
103-
if (showFileAgeInPreviewPanel) result = $"{GetFileAge(createdDate)} - {formattedDate}";
104-
return result;
106+
try
107+
{
108+
var createdDate = File.GetCreationTime(filePath);
109+
var formattedDate = createdDate.ToString(
110+
$"{previewPanelDateFormat} {previewPanelTimeFormat}",
111+
CultureInfo.CurrentCulture
112+
);
113+
114+
var result = formattedDate;
115+
if (showFileAgeInPreviewPanel) result = $"{GetFileAge(createdDate)} - {formattedDate}";
116+
return result;
117+
}
118+
catch (Exception e)
119+
{
120+
Main.Context.API.LogException(ClassName, $"Failed to get file created date for {filePath}", e);
121+
return Main.Context.API.GetTranslation("plugin_explorer_plugin_tooltip_more_info_unknown");
122+
}
105123
}
106124

107125
public static string GetFileLastModifiedAt(string filePath, string previewPanelDateFormat, string previewPanelTimeFormat, bool showFileAgeInPreviewPanel)
108126
{
109-
var lastModifiedDate = File.GetLastWriteTime(filePath);
110-
var formattedDate = lastModifiedDate.ToString(
111-
$"{previewPanelDateFormat} {previewPanelTimeFormat}",
112-
CultureInfo.CurrentCulture
113-
);
114-
115-
var result = formattedDate;
116-
if (showFileAgeInPreviewPanel) result = $"{GetFileAge(lastModifiedDate)} - {formattedDate}";
117-
return result;
127+
try
128+
{
129+
var lastModifiedDate = File.GetLastWriteTime(filePath);
130+
var formattedDate = lastModifiedDate.ToString(
131+
$"{previewPanelDateFormat} {previewPanelTimeFormat}",
132+
CultureInfo.CurrentCulture
133+
);
134+
135+
var result = formattedDate;
136+
if (showFileAgeInPreviewPanel) result = $"{GetFileAge(lastModifiedDate)} - {formattedDate}";
137+
return result;
138+
}
139+
catch (Exception e)
140+
{
141+
Main.Context.API.LogException(ClassName, $"Failed to get file modified date for {filePath}", e);
142+
return Main.Context.API.GetTranslation("plugin_explorer_plugin_tooltip_more_info_unknown");
143+
}
118144
}
119145

120146
public static string GetFolderSize(string folderPath)
121147
{
122-
var directoryInfo = new DirectoryInfo(folderPath);
123-
long size = 0;
124148
try
125149
{
150+
var directoryInfo = new DirectoryInfo(folderPath);
151+
long size = 0;
126152
foreach (var file in directoryInfo.GetFiles("*", SearchOption.AllDirectories))
127153
{
128154
size += file.Length;
129155
}
156+
return ResultManager.ToReadableSize(size, 2);
130157
}
131-
catch (Exception)
158+
catch (Exception e)
132159
{
160+
Main.Context.API.LogException(ClassName, $"Failed to get folder size for {folderPath}", e);
133161
return Main.Context.API.GetTranslation("plugin_explorer_plugin_tooltip_more_info_unknown");
134162
}
135-
return ResultManager.ToReadableSize(size, 2);
136163
}
137164

138165
public static string GetFolderCreatedAt(string folderPath, string previewPanelDateFormat, string previewPanelTimeFormat, bool showFileAgeInPreviewPanel)
139166
{
140-
var createdDate = Directory.GetCreationTime(folderPath);
141-
var formattedDate = createdDate.ToString(
142-
$"{previewPanelDateFormat} {previewPanelTimeFormat}",
143-
CultureInfo.CurrentCulture
144-
);
145-
146-
var result = formattedDate;
147-
if (showFileAgeInPreviewPanel) result = $"{GetFileAge(createdDate)} - {formattedDate}";
148-
return result;
167+
try
168+
{
169+
var createdDate = Directory.GetCreationTime(folderPath);
170+
var formattedDate = createdDate.ToString(
171+
$"{previewPanelDateFormat} {previewPanelTimeFormat}",
172+
CultureInfo.CurrentCulture
173+
);
174+
175+
var result = formattedDate;
176+
if (showFileAgeInPreviewPanel) result = $"{GetFileAge(createdDate)} - {formattedDate}";
177+
return result;
178+
}
179+
catch (Exception e)
180+
{
181+
Main.Context.API.LogException(ClassName, $"Failed to get folder created date for {folderPath}", e);
182+
return Main.Context.API.GetTranslation("plugin_explorer_plugin_tooltip_more_info_unknown");
183+
}
149184
}
150185

151186
public static string GetFolderLastModifiedAt(string folderPath, string previewPanelDateFormat, string previewPanelTimeFormat, bool showFileAgeInPreviewPanel)
152187
{
153-
var lastModifiedDate = Directory.GetLastWriteTime(folderPath);
154-
var formattedDate = lastModifiedDate.ToString(
155-
$"{previewPanelDateFormat} {previewPanelTimeFormat}",
156-
CultureInfo.CurrentCulture
157-
);
158-
159-
var result = formattedDate;
160-
if (showFileAgeInPreviewPanel) result = $"{GetFileAge(lastModifiedDate)} - {formattedDate}";
161-
return result;
188+
try
189+
{
190+
var lastModifiedDate = Directory.GetLastWriteTime(folderPath);
191+
var formattedDate = lastModifiedDate.ToString(
192+
$"{previewPanelDateFormat} {previewPanelTimeFormat}",
193+
CultureInfo.CurrentCulture
194+
);
195+
196+
var result = formattedDate;
197+
if (showFileAgeInPreviewPanel) result = $"{GetFileAge(lastModifiedDate)} - {formattedDate}";
198+
return result;
199+
}
200+
catch (Exception e)
201+
{
202+
Main.Context.API.LogException(ClassName, $"Failed to get folder modified date for {folderPath}", e);
203+
return Main.Context.API.GetTranslation("plugin_explorer_plugin_tooltip_more_info_unknown");
204+
}
162205
}
163206

164207
private static string GetFileAge(DateTime fileDateTime)

0 commit comments

Comments
 (0)