Skip to content

Commit 0605b94

Browse files
committed
Handle expected error
1 parent 9269c34 commit 0605b94

File tree

3 files changed

+49
-18
lines changed

3 files changed

+49
-18
lines changed

src/UserInputMacro/MainWindow.xaml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
66
xmlns:local="clr-namespace:UserInputMacro"
77
mc:Ignorable="d"
8-
Title="UserInputMacro" Height="101.867" Width="414.6">
8+
Title="UserInputMacro" Height="301.867" Width="414.6">
99
<Grid>
1010
<Button x:Name="recordButton" Content="Record"
1111
HorizontalAlignment="Left" Margin="10,10,0,0"
@@ -31,5 +31,7 @@
3131
Margin="90,40,0,0" TextWrapping="NoWrap" Width="173" VerticalAlignment="Top" Height="19" Text="{Binding ScriptPath}"/>
3232
<TextBlock x:Name="scriptPathText" HorizontalAlignment="Left"
3333
Margin="19,40,0,0" TextWrapping="Wrap" Text="Script Path" VerticalAlignment="Top" Width="66" Height="19"/>
34+
<TextBox x:Name="textBox" HorizontalAlignment="Left" Height="177" Margin="10,75,0,0" TextWrapping="Wrap"
35+
Text="{Binding ErrorMessage}" VerticalAlignment="Top" Width="380" IsReadOnly="True" VerticalScrollBarVisibility="Auto"/>
3436
</Grid>
3537
</Window>

src/UserInputMacro/MainWindowViewModel.cs

Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
using System.IO;
1+
using System;
2+
using System.IO;
23
using System.Windows.Threading;
34
using System.Threading.Tasks;
45
using System.ComponentModel;
56
using Microsoft.Win32;
6-
using System;
77
using System.Windows.Input;
8+
using Microsoft.CodeAnalysis.Scripting;
89

910
namespace UserInputMacro
1011
{
@@ -13,8 +14,11 @@ class MainWindowViewModel : INotifyPropertyChanged
1314
private ButtonState buttonState;
1415
private ScriptRecorder recorder;
1516
private string scriptPath;
17+
private string errorMessage;
1618

1719
private static readonly PropertyChangedEventArgs scriptPathChangedEventArgs = new PropertyChangedEventArgs( nameof( ScriptPath ) );
20+
private static readonly PropertyChangedEventArgs errorMessageChangedEventArgs = new PropertyChangedEventArgs( nameof( ErrorMessage ) );
21+
1822
public event PropertyChangedEventHandler PropertyChanged;
1923

2024
public DelegateCommand RecordCommand { get; set; }
@@ -37,6 +41,19 @@ public string ScriptPath
3741
}
3842
}
3943

44+
public string ErrorMessage
45+
{
46+
get { return errorMessage; }
47+
set {
48+
if( errorMessage == value ) {
49+
return;
50+
}
51+
52+
errorMessage = value;
53+
PropertyChanged?.Invoke( this, errorMessageChangedEventArgs );
54+
}
55+
}
56+
4057
public MainWindowViewModel()
4158
{
4259
buttonState = new ButtonState();
@@ -46,6 +63,8 @@ public MainWindowViewModel()
4663
StopCommand = new DelegateCommand( StopCmd_Execute, StopCmd_CanExecute );
4764
BrowseCommand = new DelegateCommand( BrowseCmd_Execute );
4865
PlayCommand = new AsyncDelegateCommand( PlayCmd_ExecuteAsync, PlayCmd_CanExecute );
66+
67+
ErrorMessage = "[Message]" + Environment.NewLine + "If expected error occur, view to this text box.";
4968
}
5069

5170
private bool RecordCmd_CanExecute()
@@ -71,11 +90,27 @@ private void RecordCmd_Execute()
7190

7291
private async Task PlayCmd_ExecuteAsync()
7392
{
74-
buttonState.IsPlaying = true;
75-
await Task.Delay( 50 );
76-
WinDispacher?.Invoke( new Action( CommandManager.InvalidateRequerySuggested ) );
93+
if( !File.Exists( scriptPath ) ) {
94+
ErrorMessage = "[File Error]" + Environment.NewLine + "'" + scriptPath + "' is not found.";
95+
return;
96+
}
97+
98+
ErrorMessage = "";
99+
100+
try {
101+
buttonState.IsPlaying = true;
102+
await Task.Delay( 50 );
103+
WinDispacher?.Invoke( new Action( CommandManager.InvalidateRequerySuggested ) );
104+
105+
await ScriptExecuter.ExecuteAsync( ScriptPath );
106+
}
107+
catch( CompilationErrorException ex ) {
108+
ErrorMessage = "[Compile Error]" + Environment.NewLine + ex.Message;
109+
}
110+
catch( Exception ) {
111+
throw;
112+
}
77113

78-
await ScriptExecuter.ExecuteAsync( ScriptPath );
79114
buttonState.IsPlaying = false;
80115
}
81116

src/UserInputMacro/ScriptExecuter.cs

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
using System;
2-
using System.IO;
1+
using System.IO;
32
using System.Threading.Tasks;
43
using Microsoft.CodeAnalysis.Scripting;
54
using Microsoft.CodeAnalysis.CSharp.Scripting;
@@ -10,16 +9,11 @@ static class ScriptExecuter
109
{
1110
public static async Task ExecuteAsync( string scriptPath )
1211
{
13-
try {
14-
using( var hook = new UserInputHook() ) {
15-
HookSetting( hook );
12+
using( var hook = new UserInputHook() ) {
13+
HookSetting( hook );
1614

17-
var script = CSharpScript.Create( File.ReadAllText( scriptPath ), ScriptOptions.Default, typeof( MacroScript ) );
18-
await script.RunAsync( new MacroScript() );
19-
}
20-
}
21-
catch( Exception ex ) {
22-
CommonUtil.HandleException( ex );
15+
var script = CSharpScript.Create( File.ReadAllText( scriptPath ), ScriptOptions.Default, typeof( MacroScript ) );
16+
await script.RunAsync( new MacroScript() );
2317
}
2418
}
2519

0 commit comments

Comments
 (0)