Skip to content
This repository was archived by the owner on Sep 11, 2023. It is now read-only.

Commit c724fab

Browse files
committed
added exception handling in go to error
1 parent 5bbf9c7 commit c724fab

File tree

1 file changed

+54
-40
lines changed

1 file changed

+54
-40
lines changed

UI/MainWindow/MainWindowErrorResultGrid.cs

Lines changed: 54 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,74 +1,88 @@
1-
using System.IO;
1+
using System;
2+
using System.IO;
23
using System.Threading.Tasks;
34
using System.Windows;
45
using System.Windows.Controls;
56
using System.Windows.Input;
7+
using MahApps.Metro.Controls.Dialogs;
68
using SPCode.Interop;
79
using SPCode.UI.Components;
810
using SPCode.Utils;
11+
using static SPCode.Interop.TranslationProvider;
912

1013
namespace SPCode.UI
1114
{
1215
public partial class MainWindow
1316
{
1417
private async void ErrorResultGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
1518
{
16-
var row = (ErrorDataGridRow)ErrorResultGrid.SelectedItem;
17-
if (row == null)
19+
try
1820
{
19-
return;
20-
}
21-
22-
var editors = GetAllEditorElements();
23-
if (editors == null)
24-
{
25-
return;
26-
}
21+
var row = (ErrorDataGridRow)ErrorResultGrid.SelectedItem;
22+
if (row == null)
23+
{
24+
return;
25+
}
2726

28-
// Create a file info with the supplied file
29-
var fileName = row.File;
30-
var fInfo = new FileInfo(fileName);
27+
var editors = GetAllEditorElements();
28+
if (editors == null)
29+
{
30+
return;
31+
}
3132

32-
// If it doesn't exist, it's probably a local file relative to one of the scripts being compiled
33-
if (!fInfo.Exists)
34-
{
35-
var exists = false;
33+
// Create a file info with the supplied file
34+
var fileName = row.File;
35+
var fInfo = new FileInfo(fileName);
3636

37-
// We're gonna search for the file containing the error
38-
// inside every compiled scripts location
39-
foreach (var script in ScriptsCompiled)
37+
// If it doesn't exist, it's probably a local file relative to one of the scripts being compiled
38+
if (!fInfo.Exists)
4039
{
41-
var scriptDirInfo = Path.GetDirectoryName(script);
42-
fInfo = new FileInfo(scriptDirInfo + Path.DirectorySeparatorChar + fileName);
43-
if (fInfo.Exists)
40+
var exists = false;
41+
42+
// We're gonna search for the file containing the error
43+
// inside every compiled scripts location
44+
foreach (var script in ScriptsCompiled)
45+
{
46+
var scriptDirInfo = Path.GetDirectoryName(script);
47+
fInfo = new FileInfo(scriptDirInfo + Path.DirectorySeparatorChar + fileName);
48+
if (fInfo.Exists)
49+
{
50+
exists = true;
51+
break;
52+
}
53+
}
54+
55+
if (!exists)
4456
{
45-
exists = true;
46-
break;
57+
LoggingControl.LogAction($"Failed to select {fInfo.Name}:{row.Line} - unable to find file.", 2);
58+
return;
4759
}
4860
}
4961

50-
if (!exists)
62+
// Look for the file that has the error among those that are open
63+
foreach (var ed in editors)
5164
{
52-
LoggingControl.LogAction($"Failed to select {fInfo.Name}:{row.Line} - unable to find file.", 2);
53-
return;
65+
if (ed.FullFilePath == fInfo.FullName)
66+
{
67+
await Task.Delay(50);
68+
GoToErrorLine(ed, row);
69+
}
5470
}
55-
}
5671

57-
// Look for the file that has the error among those that are open
58-
foreach (var ed in editors)
59-
{
60-
if (ed.FullFilePath == fInfo.FullName)
72+
// If it's not opened, open it and go to the error line
73+
if (TryLoadSourceFile(fInfo.FullName, out var editor, true, false, true) && editor != null)
6174
{
6275
await Task.Delay(50);
63-
GoToErrorLine(ed, row);
76+
GoToErrorLine(editor, row);
6477
}
6578
}
66-
67-
// If it's not opened, open it and go to the error line
68-
if (TryLoadSourceFile(fInfo.FullName, out var editor, true, false, true) && editor != null)
79+
catch (UnauthorizedAccessException ex)
80+
{
81+
await this.ShowMessageAsync(Translate("Error"), Translate("PermissionAccessError"), settings: MetroDialogOptions);
82+
}
83+
catch (Exception ex)
6984
{
70-
await Task.Delay(50);
71-
GoToErrorLine(editor, row);
85+
LoggingControl.LogAction($"Could not go to error! {ex.Message}");
7286
}
7387
}
7488

0 commit comments

Comments
 (0)