Skip to content

Commit

Permalink
feat: show useful error message when token is expired
Browse files Browse the repository at this point in the history
* Update error messages to start with lowercase
* Add recommendation for GitLab to remove expiration date, as it started to be set by default
  • Loading branch information
wowu committed Aug 5, 2022
1 parent d153053 commit bec5c66
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 12 deletions.
1 change: 1 addition & 0 deletions commands/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ func authgitlab() {
fmt.Println("Generate your token at " + color.BlueString("https://gitlab.com/-/profile/personal_access_tokens?name=pro+cli&scopes=read_api"))
fmt.Println()
fmt.Println("The only required scope is 'read_api'")
color.Yellow("It's recommended to leave \"Expiration date\" blank.")
fmt.Println()

// Ask for token
Expand Down
12 changes: 9 additions & 3 deletions commands/open.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func findRepo(path string) (*git.Repository, error) {
if errors.Is(err, git.ErrRepositoryNotExists) {
// Base case - we've reached the root of the filesystem
if absolutePath == "/" {
return nil, errors.New("No git repository found")
return nil, errors.New("no git repository found")
}

// Recurse to parent directory
Expand All @@ -123,9 +123,12 @@ func openGitLab(branch string, projectPath string, print bool) {
fmt.Println("No open merge request found for current branch")
fmt.Println("Create pull request at", color.BlueString("https://gitlab.com/%s/merge_requests/new?merge_request%%5Bsource_branch%%5D=%s", projectPath, branch))
os.Exit(0)
} else if errors.Is(err, gitlab.ErrUnauthorized) || errors.Is(err, gitlab.ErrTokenExpired) {
color.Red("Unable to get merge requests: %s", err.Error())
fmt.Println("Connect GitLab again with `pro auth gitlab`.")
os.Exit(1)
} else {
color.Red("Unable to get merge requests: %s", err.Error())
fmt.Println("You may need to authorize GitLab again.")
os.Exit(1)
}
}
Expand Down Expand Up @@ -154,9 +157,12 @@ func openGitHub(branch string, projectPath string, print bool) {
fmt.Println("No open pull request found for current branch")
fmt.Println("Create pull request at", color.BlueString("https://github.com/%s/pull/new/%s", projectPath, branch))
os.Exit(0)
} else if errors.Is(err, github.ErrUnauthorized) {
color.Red("Unable to get pull requests: %s", err.Error())
fmt.Println("Token may be expired or deleted. Run `pro auth github` to connect GitHub again.")
os.Exit(1)
} else {
color.Red("Unable to get pull requests: %s", err.Error())
fmt.Println("You may need to authorize GitHub again.")
os.Exit(1)
}
}
Expand Down
8 changes: 4 additions & 4 deletions providers/github/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import (
"strings"
)

var ErrUnauthorized = errors.New("Unauthorized")
var ErrNotFound = errors.New("Not found")
var ErrUnauthorized = errors.New("unauthorized")
var ErrNotFound = errors.New("not found")

type ApiResponse struct {
StatusCode int
Expand Down Expand Up @@ -63,7 +63,7 @@ func User(token string) (UserResponse, error) {

return user, nil
default:
return UserResponse{}, errors.New("Unknown response code: " + fmt.Sprint(resp.StatusCode))
return UserResponse{}, errors.New("unknown response code: " + fmt.Sprint(resp.StatusCode))
}
}

Expand Down Expand Up @@ -102,6 +102,6 @@ func FindPullRequest(projectPath string, token string, branch string) (PullReque

return pullRequests[0], nil
default:
return PullRequestResponse{}, errors.New("Unknown response code: " + fmt.Sprint(resp.StatusCode))
return PullRequestResponse{}, errors.New("unknown response code: " + fmt.Sprint(resp.StatusCode))
}
}
21 changes: 16 additions & 5 deletions providers/gitlab/gitlab.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ import (
"net/url"
)

var ErrUnauthorized = errors.New("Unauthorized")
var ErrNotFound = errors.New("Not found")
var ErrUnauthorized = errors.New("unauthorized")
var ErrNotFound = errors.New("not found")
var ErrTokenExpired = errors.New("token expired")

type ApiResponse struct {
StatusCode int
Expand Down Expand Up @@ -61,7 +62,7 @@ func User(token string) (UserResponse, error) {

return user, nil
default:
return UserResponse{}, errors.New("Unknown response code")
return UserResponse{}, errors.New("unknown response code")
}
}

Expand All @@ -82,7 +83,17 @@ func FindMergeRequest(projectPath string, token string, branch string) (MergeReq

switch resp.StatusCode {
case http.StatusUnauthorized:
return MergeRequestResponse{}, ErrUnauthorized
var body map[string]interface{}
err = json.Unmarshal(resp.Body, &body)
if err != nil {
return MergeRequestResponse{}, err
}

if body["error_description"] == "Token is expired. You can either do re-authorization or token refresh." {
return MergeRequestResponse{}, ErrTokenExpired
} else {
return MergeRequestResponse{}, ErrUnauthorized
}
case http.StatusNotFound:
return MergeRequestResponse{}, ErrNotFound
case http.StatusOK:
Expand All @@ -98,6 +109,6 @@ func FindMergeRequest(projectPath string, token string, branch string) (MergeReq

return mergeRequests[0], nil
default:
return MergeRequestResponse{}, errors.New("Unknown response code")
return MergeRequestResponse{}, errors.New("unknown response code")
}
}

0 comments on commit bec5c66

Please sign in to comment.