From c5c4d4443141f10d341fb4dc10ae59c6b5899c75 Mon Sep 17 00:00:00 2001 From: Vishesh Handa Date: Wed, 13 Feb 2019 22:43:34 +0100 Subject: [PATCH] GitHost: Improve error handling In the case of OAuth errors which we get through the message channel, throwing an exception doesn't help us as the init function will not be aware about it. So for now, I've moved the nodejs style error handling in this particular case. --- lib/apis/github.dart | 9 +++++++-- lib/apis/gitlab.dart | 8 +++++--- lib/screens/githostsetup_autoconfigure.dart | 5 ++++- 3 files changed, 16 insertions(+), 6 deletions(-) diff --git a/lib/apis/github.dart b/lib/apis/github.dart index 12655208..513a75dd 100644 --- a/lib/apis/github.dart +++ b/lib/apis/github.dart @@ -30,11 +30,16 @@ class GitHub implements GitHost { var authCode = uri.queryParameters['code']; if (authCode == null) { print("GitHub: Missing auth code. Now what?"); - throw GitHostException.OAuthFailed; + callback(GitHostException.OAuthFailed); } this._accessCode = await _getAccessCode(authCode); - callback(); + if (this._accessCode == null || this._accessCode.isEmpty) { + print("GitHub: AccessCode is invalid: " + this._accessCode); + callback(GitHostException.OAuthFailed); + } + + callback(null); } _platform.setMethodCallHandler(_handleMessages); diff --git a/lib/apis/gitlab.dart b/lib/apis/gitlab.dart index 70067c7a..4226d43b 100644 --- a/lib/apis/gitlab.dart +++ b/lib/apis/gitlab.dart @@ -36,15 +36,17 @@ class GitLab implements GitHost { print("GitLab: OAuth State incorrect"); print("Required State: " + _stateOAuth); print("Actual State: " + state); - throw GitHostException.OAuthFailed; + callback(GitHostException.OAuthFailed); + return; } _accessCode = map['access_token']; if (_accessCode == null) { - throw GitHostException.OAuthFailed; + callback(GitHostException.OAuthFailed); + return; } - callback(); + callback(null); } _platform.setMethodCallHandler(_handleMessages); diff --git a/lib/screens/githostsetup_autoconfigure.dart b/lib/screens/githostsetup_autoconfigure.dart index f0e0af94..12d57df1 100644 --- a/lib/screens/githostsetup_autoconfigure.dart +++ b/lib/screens/githostsetup_autoconfigure.dart @@ -29,7 +29,10 @@ class GitHostSetupAutoConfigureState extends State { print("Starting autoconfigure"); gitHost = createGitHost(widget.gitHostType); try { - gitHost.init(() async { + gitHost.init((Exception error) async { + if (error != null) { + throw error; + } print("GitHost Initalized: " + widget.gitHostType.toString()); GitRepo repo;