GitHost AutoConfigure: Handle errors a bit better

This commit is contained in:
Vishesh Handa
2019-01-25 14:52:56 +01:00
parent 15cedd75f1
commit fb202e0f1f
4 changed files with 79 additions and 30 deletions

View File

@ -20,3 +20,16 @@ class GitRepo {
return 'GitRepo{fulleName: $fullName, cloneUrl: $cloneUrl}';
}
}
class GitHostException implements Exception {
static const OAuthFailed = const GitHostException("OAuthFailed");
static const RepoExists = const GitHostException("RepoExists");
final String cause;
const GitHostException(this.cause);
@override
String toString() {
return "GitHostException: " + cause;
}
}

View File

@ -30,7 +30,7 @@ class GitHub implements GitHost {
var authCode = uri.queryParameters['code'];
if (authCode == null) {
print("GitHub: Missing auth code. Now what?");
callback();
throw GitHostException.OAuthFailed;
}
this._accessCode = await _getAccessCode(authCode);
@ -51,7 +51,7 @@ class GitHub implements GitHost {
response.statusCode.toString() +
": " +
response.body);
return null;
throw GitHostException.OAuthFailed;
}
print("GithubResponse: " + response.body);

View File

@ -27,19 +27,21 @@ class GitLab implements GitHost {
print("GitLab: Called onUrl with " + call.arguments.toString());
var url = call.arguments["URL"];
var uri = Uri.parse(url);
String url = call.arguments["URL"];
var queryParamters = url.substring(url.indexOf('#') + 1);
var map = Uri.splitQueryString(queryParamters);
var state = uri.queryParameters['state'];
var state = map['state'];
if (state != _stateOAuth) {
print("GitLab: OAuth State incorrect");
callback();
print("Required State: " + _stateOAuth);
print("Actual State: " + state);
throw GitHostException.OAuthFailed;
}
_accessCode = uri.queryParameters['access_token'];
_accessCode = map['access_token'];
if (_accessCode == null) {
print("GitLab: Missing access code. Now what?");
callback();
throw GitHostException.OAuthFailed;
}
callback();

View File

@ -19,40 +19,74 @@ class GitHostSetupAutoConfigure extends StatefulWidget {
class GitHostSetupAutoConfigureState extends State<GitHostSetupAutoConfigure> {
GitHost gitHost;
String errorMessage = "";
@override
void initState() {
super.initState();
print("Starting autoconfigure");
gitHost = createGitHost(widget.gitHostType);
gitHost.init(() async {
print("GitHub Initalized");
try {
gitHost.init(() async {
print("GitHost Initalized: " + widget.gitHostType.toString());
var repo = await gitHost.createRepo("journal");
var publicKey = await generateSSHKeys(comment: "GitJournal");
await gitHost.addDeployKey(publicKey, repo.fullName);
var repo = await gitHost.createRepo("journal");
var publicKey = await generateSSHKeys(comment: "GitJournal");
await gitHost.addDeployKey(publicKey, repo.fullName);
widget.onDone(repo.cloneUrl);
});
gitHost.launchOAuthScreen();
widget.onDone(repo.cloneUrl);
});
gitHost.launchOAuthScreen();
} on GitHostException catch (e) {
print("GitHostSetupAutoConfigur: " + e.toString());
setState(() {
errorMessage = widget.gitHostType.toString() + ": " + e.toString();
});
}
}
@override
Widget build(BuildContext context) {
var children = <Widget>[
Text(
'Configuring ...',
textAlign: TextAlign.center,
style: Theme.of(context).textTheme.display1,
),
SizedBox(height: 8.0),
Padding(
padding: const EdgeInsets.all(8.0),
child: CircularProgressIndicator(
value: null,
var children = <Widget>[];
if (this.errorMessage == null || this.errorMessage.isEmpty) {
children = <Widget>[
Padding(
padding: const EdgeInsets.all(8.0),
child: Text(
'Configuring ...',
textAlign: TextAlign.center,
style: Theme.of(context).textTheme.display1,
),
),
),
];
SizedBox(height: 8.0),
Padding(
padding: const EdgeInsets.all(8.0),
child: CircularProgressIndicator(
value: null,
),
),
];
} else {
children = <Widget>[
Padding(
padding: const EdgeInsets.all(8.0),
child: Text(
'Failed',
textAlign: TextAlign.center,
style: Theme.of(context).textTheme.display1,
),
),
Padding(
padding: const EdgeInsets.all(8.0),
child: Text(
this.errorMessage,
textAlign: TextAlign.center,
style: Theme.of(context).textTheme.display1,
),
),
];
}
return Column(
mainAxisAlignment: MainAxisAlignment.center,