Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Crash PtcOfflineException and AccessTokenExpiredException #1742

Merged
merged 1 commit into from
Aug 1, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 35 additions & 22 deletions PoGo.NecroBot.Logic/Common/ApiFailureStrategy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -29,7 +30,7 @@ public async Task<ApiOperation> HandleApiFailure()
await Task.Delay(500);
_retryCount++;

if (_retryCount%5 == 0)
if (_retryCount % 5 == 0)
{
DoLogin();
}
Expand All @@ -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)
});
}

}
}
}
19 changes: 8 additions & 11 deletions PoGo.NecroBot.Logic/State/LoginState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,9 @@ public async Task<IState> 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
Expand All @@ -57,6 +50,10 @@ public async Task<IState> 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
Expand Down Expand Up @@ -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 });
}
}
}