mirror of
https://github.com/GitJournal/GitJournal.git
synced 2025-06-28 09:47:35 +08:00
GitHosts: Improve error handling
This commit is contained in:
@ -23,7 +23,10 @@ class GitRepo {
|
|||||||
|
|
||||||
class GitHostException implements Exception {
|
class GitHostException implements Exception {
|
||||||
static const OAuthFailed = const GitHostException("OAuthFailed");
|
static const OAuthFailed = const GitHostException("OAuthFailed");
|
||||||
|
static const MissingAccessCode = const GitHostException("MissingAccessCode");
|
||||||
static const RepoExists = const GitHostException("RepoExists");
|
static const RepoExists = const GitHostException("RepoExists");
|
||||||
|
static const CreateRepoFailed = const GitHostException("CreateRepoFailed");
|
||||||
|
static const DeployKeyFailed = const GitHostException("DeployKeyFailed");
|
||||||
|
|
||||||
final String cause;
|
final String cause;
|
||||||
const GitHostException(this.cause);
|
const GitHostException(this.cause);
|
||||||
|
@ -72,7 +72,7 @@ class GitHub implements GitHost {
|
|||||||
@override
|
@override
|
||||||
Future<List<GitRepo>> listRepos() async {
|
Future<List<GitRepo>> listRepos() async {
|
||||||
if (_accessCode.isEmpty) {
|
if (_accessCode.isEmpty) {
|
||||||
throw "GitHub Access Code Missing";
|
throw GitHostException.MissingAccessCode;
|
||||||
}
|
}
|
||||||
|
|
||||||
var url =
|
var url =
|
||||||
@ -103,7 +103,7 @@ class GitHub implements GitHost {
|
|||||||
Future<GitRepo> createRepo(String name) async {
|
Future<GitRepo> createRepo(String name) async {
|
||||||
// FIXME: Proper error when the repo exists!
|
// FIXME: Proper error when the repo exists!
|
||||||
if (_accessCode.isEmpty) {
|
if (_accessCode.isEmpty) {
|
||||||
throw "GitHub Access Code Missing";
|
throw GitHostException.MissingAccessCode;
|
||||||
}
|
}
|
||||||
|
|
||||||
var url = "https://api.github.com/user/repos?access_token=$_accessCode";
|
var url = "https://api.github.com/user/repos?access_token=$_accessCode";
|
||||||
@ -123,7 +123,8 @@ class GitHub implements GitHost {
|
|||||||
response.statusCode.toString() +
|
response.statusCode.toString() +
|
||||||
": " +
|
": " +
|
||||||
response.body);
|
response.body);
|
||||||
return null;
|
|
||||||
|
throw GitHostException.CreateRepoFailed;
|
||||||
}
|
}
|
||||||
|
|
||||||
print("GitHub createRepo: " + response.body);
|
print("GitHub createRepo: " + response.body);
|
||||||
@ -134,7 +135,7 @@ class GitHub implements GitHost {
|
|||||||
// FIXME: Proper error when the repo exists!
|
// FIXME: Proper error when the repo exists!
|
||||||
Future addDeployKey(String sshPublicKey, String repo) async {
|
Future addDeployKey(String sshPublicKey, String repo) async {
|
||||||
if (_accessCode.isEmpty) {
|
if (_accessCode.isEmpty) {
|
||||||
throw "GitHub Access Code Missing";
|
throw GitHostException.MissingAccessCode;
|
||||||
}
|
}
|
||||||
|
|
||||||
var url =
|
var url =
|
||||||
@ -157,7 +158,7 @@ class GitHub implements GitHost {
|
|||||||
response.statusCode.toString() +
|
response.statusCode.toString() +
|
||||||
": " +
|
": " +
|
||||||
response.body);
|
response.body);
|
||||||
return null;
|
throw GitHostException.DeployKeyFailed;
|
||||||
}
|
}
|
||||||
|
|
||||||
print("GitHub addDeployKey: " + response.body);
|
print("GitHub addDeployKey: " + response.body);
|
||||||
|
@ -63,7 +63,7 @@ class GitLab implements GitHost {
|
|||||||
@override
|
@override
|
||||||
Future<List<GitRepo>> listRepos() async {
|
Future<List<GitRepo>> listRepos() async {
|
||||||
if (_accessCode.isEmpty) {
|
if (_accessCode.isEmpty) {
|
||||||
throw "GitHub Access Code Missing";
|
throw GitHostException.MissingAccessCode;
|
||||||
}
|
}
|
||||||
|
|
||||||
// FIXME: pagination!
|
// FIXME: pagination!
|
||||||
@ -95,7 +95,7 @@ class GitLab implements GitHost {
|
|||||||
Future<GitRepo> createRepo(String name) async {
|
Future<GitRepo> createRepo(String name) async {
|
||||||
// FIXME: Proper error when the repo exists!
|
// FIXME: Proper error when the repo exists!
|
||||||
if (_accessCode.isEmpty) {
|
if (_accessCode.isEmpty) {
|
||||||
throw "GitLab Access Code Missing";
|
throw GitHostException.MissingAccessCode;
|
||||||
}
|
}
|
||||||
|
|
||||||
var url = "https://gitlab.com/api/v4/projects?access_token=$_accessCode";
|
var url = "https://gitlab.com/api/v4/projects?access_token=$_accessCode";
|
||||||
@ -115,7 +115,15 @@ class GitLab implements GitHost {
|
|||||||
response.statusCode.toString() +
|
response.statusCode.toString() +
|
||||||
": " +
|
": " +
|
||||||
response.body);
|
response.body);
|
||||||
return null;
|
|
||||||
|
if (response.statusCode == 400) {
|
||||||
|
Map<String, dynamic> data = json.decode(response.body);
|
||||||
|
Map<String, dynamic> message = data['message'];
|
||||||
|
var name = message['name'];
|
||||||
|
print(name);
|
||||||
|
}
|
||||||
|
|
||||||
|
throw GitHostException.CreateRepoFailed;
|
||||||
}
|
}
|
||||||
|
|
||||||
print("GitLab createRepo: " + response.body);
|
print("GitLab createRepo: " + response.body);
|
||||||
@ -126,7 +134,7 @@ class GitLab implements GitHost {
|
|||||||
@override
|
@override
|
||||||
Future addDeployKey(String sshPublicKey, String repo) async {
|
Future addDeployKey(String sshPublicKey, String repo) async {
|
||||||
if (_accessCode.isEmpty) {
|
if (_accessCode.isEmpty) {
|
||||||
throw "GitLab Access Code Missing";
|
throw GitHostException.MissingAccessCode;
|
||||||
}
|
}
|
||||||
|
|
||||||
repo = repo.replaceAll('/', '%2F');
|
repo = repo.replaceAll('/', '%2F');
|
||||||
@ -150,7 +158,7 @@ class GitLab implements GitHost {
|
|||||||
response.statusCode.toString() +
|
response.statusCode.toString() +
|
||||||
": " +
|
": " +
|
||||||
response.body);
|
response.body);
|
||||||
return null;
|
throw GitHostException.DeployKeyFailed;
|
||||||
}
|
}
|
||||||
|
|
||||||
print("GitLab addDeployKey: " + response.body);
|
print("GitLab addDeployKey: " + response.body);
|
||||||
|
Reference in New Issue
Block a user