feature: Server error provider added, client notification service added

This commit is contained in:
Mikhail Shabanov committed 2021-05-16 22:04:42 +02:00
1 parent 3c67595d94
commit b928e0fec9
14 files changed
+200 -11

No files matched your search

@@ -1,6 +1,8 @@
using System.Collections.Generic;
using System.Globalization;
using System.Threading.Tasks;
using Newtonsoft.Json.Linq;
using StrawberryShake;
using WotBlitzStatisticsPro.Blazor.GraphQl;
namespace WotBlitzStatisticsPro.Blazor.Services
@@ -8,10 +10,14 @@ namespace WotBlitzStatisticsPro.Blazor.Services
public class GraphQlBackendService: IGraphQlBackendService
{
private readonly WotBlitzStatisticsProClient _client;
private readonly INotificationsService _notificationsService;
public GraphQlBackendService(WotBlitzStatisticsProClient client)
public GraphQlBackendService(
WotBlitzStatisticsProClient client,
INotificationsService notificationsService)
{
_client = client;
_notificationsService = notificationsService;
}
public async Task<IReadOnlyList<IFindPlayers_Players>?> FindPlayers(string accountNick, RealmType realmType)
@@ -19,6 +25,7 @@ namespace WotBlitzStatisticsPro.Blazor.Services
var accounts =
await _client.FindPlayers.ExecuteAsync(accountNick, realmType, GetLanguage());
CheckErrors(accounts.Errors);
return accounts.Data?.Players;
}
@@ -27,6 +34,7 @@ namespace WotBlitzStatisticsPro.Blazor.Services
var clans =
await _client.FindClans.ExecuteAsync(clanNameOrTag, realmType, GetLanguage());
CheckErrors(clans.Errors);
return clans.Data?.Clans;
}
@@ -45,5 +53,39 @@ namespace WotBlitzStatisticsPro.Blazor.Services
}
}
private void CheckErrors(IReadOnlyCollection<IClientError> errors)
{
var messages = new List<string>();
if (errors.Count > 0)
{
foreach (var error in errors)
{
if (error.Extensions != null)
{
foreach (var errorExt in error.Extensions)
{
var serializedError = errorExt.Value;
if (serializedError is string realError)
{
var jObject = JObject.Parse(realError);
var firstErrorInfo = jObject["errors"]?[0];
var message = firstErrorInfo?["extensions"]?["message"];
if (message != null)
{
messages.Add(message.ToString());
}
}
}
}
}
}
if (messages.Count > 0)
{
_notificationsService.ReportError("Backend error",string.Join(";", messages));
}
}
}
}
@@ -0,0 +1,19 @@
using System;
using WotBlitzStatisticsPro.Blazor.Model;
namespace WotBlitzStatisticsPro.Blazor.Services
{
public interface INotificationsService
{
NotificationType Type { get; }
string Summary { get; }
string Message { get; }
event Action MessageArrived;
void ReportInfo(string summary, string message);
void ReportWarning(string summary, string message);
void ReportError(string summary, string message);
}
}
@@ -0,0 +1,42 @@
using System;
using WotBlitzStatisticsPro.Blazor.Model;
namespace WotBlitzStatisticsPro.Blazor.Services
{
public class NotificationsService: INotificationsService
{
// ToDo: Add JSInterop console.log
public NotificationType Type { get; private set; } = NotificationType.Info;
public string Message { get; private set; } = string.Empty;
public string Summary { get; private set; }
public event Action MessageArrived;
public void ReportInfo(string summary, string message)
{
Type = NotificationType.Info;
Message = message;
Summary = summary;
MessageArrived?.Invoke();
}
public void ReportWarning(string summary, string message)
{
Type = NotificationType.Warning;
Message = message;
Summary = summary;
MessageArrived?.Invoke();
}
public void ReportError(string summary, string message)
{
Type = NotificationType.Error;
Message = message;
Summary = summary;
MessageArrived?.Invoke();
}
}
}