Skip to content

Commit

Permalink
add OnServerErrorHandling
Browse files Browse the repository at this point in the history
  • Loading branch information
Ali-YousefiTelori committed Dec 29, 2023
1 parent 9d0ddd7 commit cb8de24
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,6 @@ namespace EasyMicroservices.UI.Cores;
/// </summary>
public class ApiBaseViewModel : BaseViewModel
{
/// <summary>
///
/// </summary>
public static Func<ErrorContract, Task<bool>> OnGlobalServiceErrorHandler { get; set; }
/// <summary>
///
/// </summary>
Expand All @@ -33,7 +29,7 @@ public async virtual Task ExecuteApi<TResult>(Func<Task<TResult>> getServerResul
if (response.IsSuccess)
await onSuccess(response);
else
await InternalDisplayServerError(response.Error);
await OnServerErrorHandling(response.Error);
}
catch (Exception ex)
{
Expand Down Expand Up @@ -67,7 +63,7 @@ public virtual async Task ExecuteApi(Func<Task<object>> getServerResult, Func<Ta
if (response.IsSuccess)
await onSuccess?.Invoke();
else
await InternalDisplayServerError(response.Error);
await OnServerErrorHandling(response.Error);
}
catch (Exception ex)
{
Expand All @@ -81,10 +77,4 @@ public virtual async Task ExecuteApi(Func<Task<object>> getServerResult, Func<Ta
UnBusy();
}
}

async Task InternalDisplayServerError(ErrorContract error)
{
if (OnGlobalServiceErrorHandler == null || !await OnGlobalServiceErrorHandler(error))
await OnServerError(error);
}
}
15 changes: 15 additions & 0 deletions src/CSharp/Cores/EasyMicroservices.UI.Cores.Mvvm/BaseViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ namespace EasyMicroservices.UI.Cores;
/// </summary>
public abstract class BaseViewModel : IBusyViewModel, INotifyPropertyChanged, IDisposable
{
/// <summary>
///
/// </summary>
public static Func<ErrorContract, Task<bool>> OnGlobalServiceErrorHandler { get; set; }
/// <summary>
///
/// </summary>
Expand Down Expand Up @@ -162,6 +166,17 @@ public virtual Task OnServerError(ErrorContract errorContract)
return Task.CompletedTask;
}

/// <summary>
///
/// </summary>
/// <param name="error"></param>
/// <returns></returns>
public virtual async Task OnServerErrorHandling(ErrorContract error)
{
if (OnGlobalServiceErrorHandler == null || !await OnGlobalServiceErrorHandler(error))
await OnServerError(error);
}

/// <summary>
///
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public virtual void Execute(object parameter)
}
catch (InvalidResultOfMessageContractException ex)
{
_busyViewModel?.OnServerError(ex.MessageContract.Error);
_busyViewModel?.OnServerErrorHandling(ex.MessageContract.Error);
}
catch (Exception ex)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ async Task InternalExecute(object parameter)
}
catch (InvalidResultOfMessageContractException ex)
{
_busyViewModel?.OnServerError(ex.MessageContract.Error);
_busyViewModel?.OnServerErrorHandling(ex.MessageContract.Error);
}
catch (Exception ex)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ public interface IBusyViewModel : INotifyPropertyChanged
/// <param name="errorContract"></param>
/// <returns></returns>
Task OnServerError(ErrorContract errorContract);
/// <summary>
///
/// </summary>
/// <param name="errorContract"></param>
/// <returns></returns>
Task OnServerErrorHandling(ErrorContract errorContract);

/// <summary>
///
Expand Down

0 comments on commit cb8de24

Please sign in to comment.