Skip to content
This repository has been archived by the owner on Sep 3, 2024. It is now read-only.

Commit

Permalink
feat: job error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
julien-wff committed Dec 15, 2023
1 parent fdd0dc8 commit b406684
Show file tree
Hide file tree
Showing 9 changed files with 136 additions and 4 deletions.
4 changes: 4 additions & 0 deletions EasyGUI/Controls/JobDisplay.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@
x:Name="PausedBreadCrumb"
BreadCrumbText="{x:Static resx:Strings.JobState_Paused}"
BreadCrumbColor="{StaticResource OrangeBrush}" Margin="0,0,8,0" />
<local:BreadCrumb
x:Name="ErrorBreadCrumb"
BreadCrumbText="{x:Static resx:Strings.JobState_Error}"
BreadCrumbColor="{StaticResource RedBrush}" Margin="0,0,8,0" />
</StackPanel>

<StackPanel Grid.Column="3" VerticalAlignment="Center" HorizontalAlignment="Right" Orientation="Horizontal">
Expand Down
38 changes: 37 additions & 1 deletion EasyGUI/Controls/JobDisplay.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@
using System.Windows;
using System.Windows.Input;
using EasyGUI.Events;
using EasyGUI.Resources;
using EasyLib.Enums;
using EasyLib.Events;
using EasyLib.Job;
using Application = System.Windows.Application;
using MessageBox = System.Windows.MessageBox;

namespace EasyGUI.Controls;

Expand All @@ -36,6 +38,10 @@ public partial class JobDisplay : INotifyPropertyChanged, IJobStatusSubscriber
new PropertyMetadata(default(ObservableCollection<Job>))
);

private readonly object _errorMessageLock = new();

private string? _errorMessage;

private bool _isJobSelectedLocked;

private bool _selectedJobsLocked;
Expand Down Expand Up @@ -80,14 +86,43 @@ public string JobProgressText

public void OnJobStateChange(JobState state, Job job)
{
Application.Current.Dispatcher.Invoke(() => OnPropertyChanged(nameof(Job)));
Application.Current.Dispatcher.Invoke(() =>
{
_errorMessage = null;
OnPropertyChanged(nameof(Job));
});
}

public void OnJobProgress(Job job)
{
Application.Current.Dispatcher.Invoke(() => OnPropertyChanged(nameof(Job)));
}

public void OnJobError(Exception error)
{
lock (_errorMessageLock)
{
if (_errorMessage != null)
return;

_errorMessage = error.Message;
}

Application.Current.Dispatcher.Invoke(() =>
{
var jobName = Job.Name;

Task.Run(() => MessageBox.Show(
string.Format(Strings.JobDisplay_ErrorPopup_Message, jobName, error.Message),
Strings.JobDisplay_ErrorPopup_Title,
MessageBoxButton.OK,
MessageBoxImage.Error
));

OnPropertyChanged(nameof(Job));
});
}

public event PropertyChangedEventHandler? PropertyChanged;
public event EventHandler<JobEventArgs>? JobStarted;
public event EventHandler<JobEventArgs>? JobEdited;
Expand Down Expand Up @@ -127,6 +162,7 @@ private void UpdateBreadcrumbs()
SetElementVisibility(StructureBreadCrumb, Job.State == JobState.DestinationStructureCreation);
SetElementVisibility(CopyBreadCrumb, Job.State == JobState.Copy);
SetElementVisibility(PausedBreadCrumb, Job.State != JobState.End && !Job.CurrentlyRunning);
SetElementVisibility(ErrorBreadCrumb, !string.IsNullOrEmpty(_errorMessage));
}

private void UpdateButtons()
Expand Down
27 changes: 27 additions & 0 deletions EasyGUI/Resources/Strings.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 27 additions & 0 deletions EasyGUI/Resources/Strings.fr.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions EasyGUI/Resources/Strings.fr.resx
Original file line number Diff line number Diff line change
Expand Up @@ -148,4 +148,13 @@

Nous nous excusons pour la gêne occasionnée.</value>
</data>
<data name="JobState_Error" xml:space="preserve">
<value>Erreur</value>
</data>
<data name="JobDisplay_ErrorPopup_Title" xml:space="preserve">
<value>Erreur de tâche</value>
</data>
<data name="JobDisplay_ErrorPopup_Message" xml:space="preserve">
<value>Une erreur est survenue pendant l'exécution de la tâche {0} : {1}</value>
</data>
</root>
9 changes: 9 additions & 0 deletions EasyGUI/Resources/Strings.resx
Original file line number Diff line number Diff line change
Expand Up @@ -148,4 +148,13 @@

We are sorry for this inconvenience.</value>
</data>
<data name="JobState_Error" xml:space="preserve">
<value>Error</value>
</data>
<data name="JobDisplay_ErrorPopup_Title" xml:space="preserve">
<value>Job error</value>
</data>
<data name="JobDisplay_ErrorPopup_Message" xml:space="preserve">
<value>An error occured during the execution of {0}: {1}</value>
</data>
</root>
2 changes: 1 addition & 1 deletion EasyLib/Events/IJobStatusSubscriber.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public void OnJobStateChange(JobState state, Job.Job job)
/// Event triggered when an error occurs during the job
/// </summary>
/// <param name="error"></param>
public void OnJobError(string error)
public void OnJobError(Exception error)
{
// Implementation optional
}
Expand Down
22 changes: 21 additions & 1 deletion EasyLib/Job/Job.cs
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,19 @@ private bool Start(bool resume)
var folders = selector.SelectFolders(directories, lastFolder, Type, DestinationFolder);

// Run the job in a new thread
var thread = new Thread(() => JobSteps(transferManager, folders));
var thread = new Thread(() =>
{
try
{
JobSteps(transferManager, folders);
}
catch (Exception e)
{
CurrentlyRunning = false;
Pause();
_notifySubscribersForError(e);
}
});
thread.Start();
return true;
}
Expand Down Expand Up @@ -249,6 +261,14 @@ private void _notifySubscribersForChangeState(JobState subState)
}
}

private void _notifySubscribersForError(Exception error)
{
foreach (var subscriber in Subscribers)
{
subscriber.OnJobError(error);
}
}

private void _setJobState(JobState pubState)
{
State = pubState;
Expand Down
2 changes: 1 addition & 1 deletion EasyLib/JobManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public void Unsubscribe(IJobStatusSubscriber subscriber)
_subscribers.Remove(subscriber);
}

public void OnJobError(string error)
public void OnJobError(Exception error)
{
foreach (var subscriber in _subscribers)
{
Expand Down

0 comments on commit b406684

Please sign in to comment.