From 323e424c562c27b7c49916100f7cba6f53d13834 Mon Sep 17 00:00:00 2001 From: prophetal Date: Mon, 1 Aug 2016 14:11:20 +0200 Subject: [PATCH] Fix Crash PtcOfflineException and AccessTokenExpiredException FIX PtcOfflineException and AccessTokenExpiredException get not catched and Bot crash --- .../Common/ApiFailureStrategy.cs | 57 ++++++++++++------- PoGo.NecroBot.Logic/State/LoginState.cs | 19 +++---- 2 files changed, 43 insertions(+), 33 deletions(-) diff --git a/PoGo.NecroBot.Logic/Common/ApiFailureStrategy.cs b/PoGo.NecroBot.Logic/Common/ApiFailureStrategy.cs index 1fb5e4027..ca79cf044 100644 --- a/PoGo.NecroBot.Logic/Common/ApiFailureStrategy.cs +++ b/PoGo.NecroBot.Logic/Common/ApiFailureStrategy.cs @@ -5,6 +5,7 @@ using PoGo.NecroBot.Logic.Event; using PoGo.NecroBot.Logic.State; using PokemonGo.RocketAPI.Enums; +using PokemonGo.RocketAPI.Exceptions; using PokemonGo.RocketAPI.Extensions; #endregion @@ -29,7 +30,7 @@ public async Task HandleApiFailure() await Task.Delay(500); _retryCount++; - if (_retryCount%5 == 0) + if (_retryCount % 5 == 0) { DoLogin(); } @@ -44,32 +45,44 @@ public void HandleApiSuccess() private async void DoLogin() { - switch (_session.Settings.AuthType) + try { - case AuthType.Ptc: - try - { + switch (_session.Settings.AuthType) + { + case AuthType.Ptc: await _session.Client.Login.DoPtcLogin(_session.Settings.PtcUsername, _session.Settings.PtcPassword); - } - catch (AggregateException ae) - { - throw ae.Flatten().InnerException; - } - break; - case AuthType.Google: - await - _session.Client.Login.DoGoogleLogin(_session.Settings.GoogleUsername, - _session.Settings.GooglePassword); - break; - default: - _session.EventDispatcher.Send(new ErrorEvent - { - Message = _session.Translation.GetTranslation(TranslationString.WrongAuthType) - }); - break; + break; + case AuthType.Google: + await + _session.Client.Login.DoGoogleLogin(_session.Settings.GoogleUsername, + _session.Settings.GooglePassword); + break; + default: + _session.EventDispatcher.Send(new ErrorEvent + { + Message = _session.Translation.GetTranslation(TranslationString.WrongAuthType) + }); + break; + } + } + catch (AggregateException ae) + { + throw ae.Flatten().InnerException; } + catch (Exception ex) when (ex is PtcOfflineException || ex is AccessTokenExpiredException) + { + _session.EventDispatcher.Send(new ErrorEvent + { + Message = _session.Translation.GetTranslation(TranslationString.PtcOffline) + }); + _session.EventDispatcher.Send(new NoticeEvent + { + Message = _session.Translation.GetTranslation(TranslationString.TryingAgainIn, 20) + }); + } + } } } \ No newline at end of file diff --git a/PoGo.NecroBot.Logic/State/LoginState.cs b/PoGo.NecroBot.Logic/State/LoginState.cs index 0417a6e5f..c3af3f53b 100644 --- a/PoGo.NecroBot.Logic/State/LoginState.cs +++ b/PoGo.NecroBot.Logic/State/LoginState.cs @@ -33,16 +33,9 @@ public async Task Execute(ISession session, CancellationToken cancellati switch (session.Settings.AuthType) { case AuthType.Ptc: - try - { - await - session.Client.Login.DoPtcLogin(session.Settings.PtcUsername, - session.Settings.PtcPassword); - } - catch (AggregateException ae) - { - throw ae.Flatten().InnerException; - } + await + session.Client.Login.DoPtcLogin(session.Settings.PtcUsername, + session.Settings.PtcPassword); break; case AuthType.Google: await @@ -57,6 +50,10 @@ public async Task Execute(ISession session, CancellationToken cancellati return null; } } + catch (AggregateException ae) + { + throw ae.Flatten().InnerException; + } catch (Exception ex) when (ex is PtcOfflineException || ex is AccessTokenExpiredException) { session.EventDispatcher.Send(new ErrorEvent @@ -173,7 +170,7 @@ private static async Task CheckLogin(ISession session, CancellationToken cancell public async Task DownloadProfile(ISession session) { session.Profile = await session.Client.Player.GetPlayer(); - session.EventDispatcher.Send(new ProfileEvent {Profile = session.Profile}); + session.EventDispatcher.Send(new ProfileEvent { Profile = session.Profile }); } } } \ No newline at end of file