Skip to content

Commit 4e97589

Browse files
authored
Merge pull request #236 from Ner0theHer0/improve-oauth-error-handling
Improve oauth error handling
2 parents e0145f6 + 8794e2e commit 4e97589

12 files changed

+44
-19
lines changed

bitbucket/error.go

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,31 @@ import (
66
"net/http"
77

88
"github.com/DrFaust92/bitbucket-go-client"
9+
"golang.org/x/oauth2"
910
)
1011

12+
type ClientScopeError struct {
13+
Error struct {
14+
Message string `json:"message"`
15+
Detail struct {
16+
Required []string `json:"required"`
17+
Granted []string `json:"granted"`
18+
} `json:"detail"`
19+
} `json:"error"`
20+
}
21+
1122
func handleClientError(httpResponse *http.Response, err error) error {
23+
if oauthError, ok := err.(*oauth2.RetrieveError); ok {
24+
return fmt.Errorf("%s: %s", oauthError.Response.Status, oauthError.ErrorDescription)
25+
}
26+
1227
if httpResponse == nil || httpResponse.StatusCode < 400 {
1328
return nil
1429
}
1530

1631
clientHttpError, ok := err.(bitbucket.GenericSwaggerError)
1732
if ok {
18-
var errorBody string
19-
var bitbucketHttpError bitbucket.ModelError
20-
if err := json.Unmarshal(clientHttpError.Body(), &bitbucketHttpError); err != nil {
21-
errorBody = string(clientHttpError.Body()[:])
22-
} else {
23-
errorBody = bitbucketHttpError.Error_.Message
24-
}
25-
33+
errorBody := extractErrorMessage(clientHttpError.Body())
2634
return fmt.Errorf("%s: %s", httpResponse.Status, errorBody)
2735
}
2836

@@ -32,3 +40,20 @@ func handleClientError(httpResponse *http.Response, err error) error {
3240

3341
return nil
3442
}
43+
44+
func extractErrorMessage(body []byte) string {
45+
var bitbucketHttpError bitbucket.ModelError
46+
if err := json.Unmarshal(body, &bitbucketHttpError); err == nil {
47+
return bitbucketHttpError.Error_.Message
48+
}
49+
50+
var clientScopeErr ClientScopeError
51+
if err := json.Unmarshal(body, &clientScopeErr); err == nil {
52+
message := clientScopeErr.Error.Message
53+
required := clientScopeErr.Error.Detail.Required
54+
granted := clientScopeErr.Error.Detail.Granted
55+
return fmt.Sprintf("%s Required: %v Granted: %v", message, required, granted)
56+
}
57+
58+
return string(body[:])
59+
}

bitbucket/resource_deploy_key.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ func resourceDeployKeysRead(ctx context.Context, d *schema.ResourceData, m inter
107107

108108
deployKey, res, err := deployApi.RepositoriesWorkspaceRepoSlugDeployKeysKeyIdGet(c.AuthContext, keyId, repo, workspace)
109109

110-
if res.StatusCode == http.StatusNotFound {
110+
if res != nil && res.StatusCode == http.StatusNotFound {
111111
log.Printf("[WARN] Deploy Key (%s) not found, removing from state", d.Id())
112112
d.SetId("")
113113
return nil

bitbucket/resource_deployment_variable.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ func resourceDeploymentVariableRead(ctx context.Context, d *schema.ResourceData,
107107

108108
rvRes, res, err := pipeApi.GetDeploymentVariables(c.AuthContext, workspace, repoSlug, deployment, nil)
109109

110-
if res.StatusCode == http.StatusNotFound {
110+
if res != nil && res.StatusCode == http.StatusNotFound {
111111
log.Printf("[WARN] Deployment Variable (%s) not found, removing from state", d.Id())
112112
d.SetId("")
113113
return nil

bitbucket/resource_forked_repository.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ func resourceForkedRepositoryRead(ctx context.Context, d *schema.ResourceData, m
267267

268268
repoRes, res, err := repoApi.RepositoriesWorkspaceRepoSlugGet(c.AuthContext, repoSlug, workspace)
269269

270-
if res.StatusCode == http.StatusNotFound {
270+
if res != nil && res.StatusCode == http.StatusNotFound {
271271
log.Printf("[WARN] Repository (%s) not found, removing from state", d.Id())
272272
d.SetId("")
273273
return nil

bitbucket/resource_pipeline_schedule.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ func resourcePipelineScheduleRead(ctx context.Context, d *schema.ResourceData, m
150150

151151
schedule, res, err := pipeApi.GetRepositoryPipelineSchedule(c.AuthContext, workspace, repo, uuid)
152152

153-
if res.StatusCode == http.StatusNotFound {
153+
if res != nil && res.StatusCode == http.StatusNotFound {
154154
log.Printf("[WARN] Pipeline Schedule (%s) not found, removing from state", d.Id())
155155
d.SetId("")
156156
return nil

bitbucket/resource_pipeline_ssh_key.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ func resourcePipelineSshKeysRead(ctx context.Context, d *schema.ResourceData, m
7575

7676
key, res, err := pipeApi.GetRepositoryPipelineSshKeyPair(c.AuthContext, workspace, repo)
7777

78-
if res.StatusCode == http.StatusNotFound {
78+
if res != nil && res.StatusCode == http.StatusNotFound {
7979
log.Printf("[WARN] Pipeline Ssh Key (%s) not found, removing from state", d.Id())
8080
d.SetId("")
8181
return nil

bitbucket/resource_pipeline_ssh_known_host.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ func resourcePipelineSshKnownHostsRead(ctx context.Context, d *schema.ResourceDa
121121

122122
host, res, err := pipeApi.GetRepositoryPipelineKnownHost(c.AuthContext, workspace, repo, uuid)
123123

124-
if res.StatusCode == http.StatusNotFound {
124+
if res != nil && res.StatusCode == http.StatusNotFound {
125125
log.Printf("[WARN] Pipeline Ssh known host (%s) not found, removing from state", d.Id())
126126
d.SetId("")
127127
return nil

bitbucket/resource_project.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ func resourceProjectRead(ctx context.Context, d *schema.ResourceData, m interfac
198198

199199
projRes, res, err := projectApi.WorkspacesWorkspaceProjectsProjectKeyGet(c.AuthContext, projectKey, d.Get("owner").(string))
200200

201-
if res.StatusCode == http.StatusNotFound {
201+
if res != nil && res.StatusCode == http.StatusNotFound {
202202
log.Printf("[WARN] Project (%s) not found, removing from state", d.Id())
203203
d.SetId("")
204204
return nil

bitbucket/resource_repository.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,7 @@ func resourceRepositoryRead(ctx context.Context, d *schema.ResourceData, m inter
345345

346346
repoRes, res, err := repoApi.RepositoriesWorkspaceRepoSlugGet(c.AuthContext, repoSlug, workspace)
347347

348-
if res.StatusCode == http.StatusNotFound {
348+
if res != nil && res.StatusCode == http.StatusNotFound {
349349
log.Printf("[WARN] Repository (%s) not found, removing from state", d.Id())
350350
d.SetId("")
351351
return nil

bitbucket/resource_repository_variable.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ func resourceRepositoryVariableRead(ctx context.Context, d *schema.ResourceData,
105105

106106
rvRes, res, err := pipeApi.GetRepositoryPipelineVariable(c.AuthContext, workspace, repoSlug, d.Get("uuid").(string))
107107

108-
if res.StatusCode == http.StatusNotFound {
108+
if res != nil && res.StatusCode == http.StatusNotFound {
109109
log.Printf("[WARN] Repository Variable (%s) not found, removing from state", d.Id())
110110
d.SetId("")
111111
return nil

0 commit comments

Comments
 (0)