Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: make sure the authorization error is correctly shown to the end user #200

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions bitbucket/data_current_user.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ func dataReadCurrentUser(ctx context.Context, d *schema.ResourceData, m interfac
httpClient := m.(Clients).httpClient
usersApi := c.ApiClient.UsersApi

curUser, _, err := usersApi.UserGet(c.AuthContext)
if err := handleClientError(err); err != nil {
curUser, res, err := usersApi.UserGet(c.AuthContext)
if err := handleClientError(res, err); err != nil {
return diag.FromErr(err)
}

Expand Down
4 changes: 2 additions & 2 deletions bitbucket/data_deployments.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ func dataReadDeployments(ctx context.Context, d *schema.ResourceData, m interfac
workspace := d.Get("workspace").(string)
repoId := d.Get("repository").(string)

deploymentsResp, _, err := deployApi.GetEnvironmentsForRepository(c.AuthContext, workspace, repoId, nil)
if err := handleClientError(err); err != nil {
deploymentsResp, res, err := deployApi.GetEnvironmentsForRepository(c.AuthContext, workspace, repoId, nil)
if err := handleClientError(res, err); err != nil {
return diag.FromErr(err)
}

Expand Down
4 changes: 2 additions & 2 deletions bitbucket/data_hook_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ func dataReadHookTypes(ctx context.Context, d *schema.ResourceData, m interface{
webhooksApi := c.ApiClient.WebhooksApi

subjectType := d.Get("subject_type").(string)
hookTypes, _, err := webhooksApi.HookEventsSubjectTypeGet(c.AuthContext, subjectType, nil)
if err := handleClientError(err); err != nil {
hookTypes, res, err := webhooksApi.HookEventsSubjectTypeGet(c.AuthContext, subjectType, nil)
if err := handleClientError(res, err); err != nil {
return diag.FromErr(err)
}

Expand Down
4 changes: 2 additions & 2 deletions bitbucket/data_user.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ func dataReadUser(ctx context.Context, d *schema.ResourceData, m interface{}) di
selectedUser = v.(string)
}

user, _, err := usersApi.UsersSelectedUserGet(c.AuthContext, selectedUser)
if err := handleClientError(err); err != nil {
user, res, err := usersApi.UsersSelectedUserGet(c.AuthContext, selectedUser)
if err := handleClientError(res, err); err != nil {
return diag.FromErr(err)
}

Expand Down
4 changes: 2 additions & 2 deletions bitbucket/data_workspace.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ func dataReadWorkspace(ctx context.Context, d *schema.ResourceData, m interface{
workspaceApi := c.ApiClient.WorkspacesApi

workspace := d.Get("workspace").(string)
workspaceReq, _, err := workspaceApi.WorkspacesWorkspaceGet(c.AuthContext, workspace)
if err := handleClientError(err); err != nil {
workspaceReq, res, err := workspaceApi.WorkspacesWorkspaceGet(c.AuthContext, workspace)
if err := handleClientError(res, err); err != nil {
return diag.FromErr(err)
}

Expand Down
4 changes: 2 additions & 2 deletions bitbucket/data_workspace_members.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ func dataReadWorkspaceMembers(ctx context.Context, d *schema.ResourceData, m int
options := bitbucket.WorkspacesApiWorkspacesWorkspaceMembersGetOpts{}

for {
flattenAccountsReq, _, err := workspaceApi.WorkspacesWorkspaceMembersGet(c.AuthContext, workspace, &options)
if err := handleClientError(err); err != nil {
flattenAccountsReq, res, err := workspaceApi.WorkspacesWorkspaceMembersGet(c.AuthContext, workspace, &options)
if err := handleClientError(res, err); err != nil {
return diag.FromErr(err)
}

Expand Down
20 changes: 14 additions & 6 deletions bitbucket/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,27 @@ package bitbucket
import (
"encoding/json"
"fmt"
"net/http"

"github.com/DrFaust92/bitbucket-go-client"
)

func handleClientError(err error) error {
httpErr, ok := err.(bitbucket.GenericSwaggerError)
func handleClientError(httpResponse *http.Response, err error) error {
if httpResponse == nil || httpResponse.StatusCode < 400 {
return nil
}

clientHttpError, ok := err.(bitbucket.GenericSwaggerError)
if ok {
var httpError bitbucket.ModelError
if err := json.Unmarshal(httpErr.Body(), &httpError); err != nil {
return fmt.Errorf(string(httpErr.Body()))
var errorBody string
var bitbucketHttpError bitbucket.ModelError
if err := json.Unmarshal(clientHttpError.Body(), &bitbucketHttpError); err != nil {
errorBody = string(clientHttpError.Body()[:])
} else {
errorBody = bitbucketHttpError.Error_.Message
}

return fmt.Errorf("%s: %s", httpErr.Error(), httpError.Error_.Message)
return fmt.Errorf("%s: %s", httpResponse.Status, errorBody)
}

if err != nil {
Expand Down
13 changes: 6 additions & 7 deletions bitbucket/resource_branch_restriction.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,9 +194,8 @@ func resourceBranchRestrictionsCreate(ctx context.Context, d *schema.ResourceDat

repo := d.Get("repository").(string)
workspace := d.Get("owner").(string)
branchRestrictionReq, _, err := brApi.RepositoriesWorkspaceRepoSlugBranchRestrictionsPost(c.AuthContext, *branchRestriction, repo, workspace)

if err := handleClientError(err); err != nil {
branchRestrictionReq, res, err := brApi.RepositoriesWorkspaceRepoSlugBranchRestrictionsPost(c.AuthContext, *branchRestriction, repo, workspace)
if err := handleClientError(res, err); err != nil {
return diag.FromErr(err)
}

Expand All @@ -218,7 +217,7 @@ func resourceBranchRestrictionsRead(ctx context.Context, d *schema.ResourceData,
return nil
}

if err := handleClientError(err); err != nil {
if err := handleClientError(res, err); err != nil {
return diag.FromErr(err)
}

Expand All @@ -239,11 +238,11 @@ func resourceBranchRestrictionsUpdate(ctx context.Context, d *schema.ResourceDat
brApi := c.ApiClient.BranchRestrictionsApi
branchRestriction := createBranchRestriction(d)

_, _, err := brApi.RepositoriesWorkspaceRepoSlugBranchRestrictionsIdPut(c.AuthContext,
_, res, err := brApi.RepositoriesWorkspaceRepoSlugBranchRestrictionsIdPut(c.AuthContext,
*branchRestriction, url.PathEscape(d.Id()),
d.Get("repository").(string), d.Get("owner").(string))

if err := handleClientError(err); err != nil {
if err := handleClientError(res, err); err != nil {
return diag.FromErr(err)
}

Expand All @@ -262,7 +261,7 @@ func resourceBranchRestrictionsDelete(ctx context.Context, d *schema.ResourceDat
return nil
}

if err := handleClientError(err); err != nil {
if err := handleClientError(res, err); err != nil {
return diag.FromErr(err)
}

Expand Down
12 changes: 6 additions & 6 deletions bitbucket/resource_commit_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,22 +115,22 @@ func resourceCommitFilePut(ctx context.Context, d *schema.ResourceData, m interf
return diag.FromErr(err)
}

response, err := client.PostWithContentType(fmt.Sprintf("2.0/repositories/%s/%s/src",
res, err := client.PostWithContentType(fmt.Sprintf("2.0/repositories/%s/%s/src",
workspace,
repoSlug,
), writer.FormDataContentType(), body)

if err := handleClientError(err); err != nil {
if err := handleClientError(res, err); err != nil {
return diag.FromErr(err)
}

if response.StatusCode != http.StatusCreated {
if res.StatusCode != http.StatusCreated {
return diag.FromErr(fmt.Errorf(""))
}

d.SetId(string(fmt.Sprintf("%s/%s/%s/%s", workspace, repoSlug, branch, filename)))

location, _ := response.Location()
location, _ := res.Location()
splitPath := strings.Split(location.Path, "/")
d.Set("commit_sha", splitPath[len(splitPath)-1])

Expand All @@ -146,9 +146,9 @@ func resourceCommitFileRead(ctx context.Context, d *schema.ResourceData, m inter
filename := d.Get("filename").(string)
commit := d.Get("commit_sha").(string)

_, _, err := sourceApi.RepositoriesWorkspaceRepoSlugSrcCommitPathGet(c.AuthContext, commit, filename, repoSlug, workspace, &bitbucket.SourceApiRepositoriesWorkspaceRepoSlugSrcCommitPathGetOpts{})
_, res, err := sourceApi.RepositoriesWorkspaceRepoSlugSrcCommitPathGet(c.AuthContext, commit, filename, repoSlug, workspace, &bitbucket.SourceApiRepositoriesWorkspaceRepoSlugSrcCommitPathGetOpts{})

if err := handleClientError(err); err != nil {
if err := handleClientError(res, err); err != nil {
return diag.FromErr(err)
}

Expand Down
18 changes: 9 additions & 9 deletions bitbucket/resource_default_reviewers.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ func resourceDefaultReviewersCreate(ctx context.Context, d *schema.ResourceData,
for _, user := range d.Get("reviewers").(*schema.Set).List() {
userName := user.(string)

_, _, err := prApi.RepositoriesWorkspaceRepoSlugDefaultReviewersTargetUsernamePut(c.AuthContext, repo, userName, workspace)
if err := handleClientError(err); err != nil {
_, res, err := prApi.RepositoriesWorkspaceRepoSlugDefaultReviewersTargetUsernamePut(c.AuthContext, repo, userName, workspace)
if err := handleClientError(res, err); err != nil {
return diag.FromErr(err)
}
}
Expand All @@ -77,7 +77,7 @@ func resourceDefaultReviewersRead(ctx context.Context, d *schema.ResourceData, m

for {
reviewers, res, err := prApi.RepositoriesWorkspaceRepoSlugDefaultReviewersGet(c.AuthContext, repo, owner, &options)
if err := handleClientError(err); err != nil {
if err := handleClientError(res, err); err != nil {
return diag.FromErr(err)
}

Expand Down Expand Up @@ -121,16 +121,16 @@ func resourceDefaultReviewersUpdate(ctx context.Context, d *schema.ResourceData,

for _, user := range add.List() {
userName := user.(string)
_, _, err := prApi.RepositoriesWorkspaceRepoSlugDefaultReviewersTargetUsernamePut(c.AuthContext, repo, userName, workspace)
if err := handleClientError(err); err != nil {
_, res, err := prApi.RepositoriesWorkspaceRepoSlugDefaultReviewersTargetUsernamePut(c.AuthContext, repo, userName, workspace)
if err := handleClientError(res, err); err != nil {
return diag.FromErr(err)
}
}

for _, user := range remove.List() {
userName := user.(string)
_, err := prApi.RepositoriesWorkspaceRepoSlugDefaultReviewersTargetUsernameDelete(c.AuthContext, repo, userName, workspace)
if err := handleClientError(err); err != nil {
res, err := prApi.RepositoriesWorkspaceRepoSlugDefaultReviewersTargetUsernameDelete(c.AuthContext, repo, userName, workspace)
if err := handleClientError(res, err); err != nil {
return diag.FromErr(err)
}
}
Expand All @@ -146,8 +146,8 @@ func resourceDefaultReviewersDelete(ctx context.Context, d *schema.ResourceData,
workspace := d.Get("owner").(string)
for _, user := range d.Get("reviewers").(*schema.Set).List() {
userName := user.(string)
_, err := prApi.RepositoriesWorkspaceRepoSlugDefaultReviewersTargetUsernameDelete(c.AuthContext, repo, userName, workspace)
if err := handleClientError(err); err != nil {
res, err := prApi.RepositoriesWorkspaceRepoSlugDefaultReviewersTargetUsernameDelete(c.AuthContext, repo, userName, workspace)
if err := handleClientError(res, err); err != nil {
return diag.FromErr(err)
}
}
Expand Down
10 changes: 5 additions & 5 deletions bitbucket/resource_deploy_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,15 +105,15 @@ func resourceDeployKeysRead(ctx context.Context, d *schema.ResourceData, m inter
return diag.FromErr(err)
}

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

if deployKeyRes.StatusCode == http.StatusNotFound {
if res.StatusCode == http.StatusNotFound {
log.Printf("[WARN] Deploy Key (%s) not found, removing from state", d.Id())
d.SetId("")
return nil
}

if err := handleClientError(err); err != nil {
if err := handleClientError(res, err); err != nil {
return diag.FromErr(err)
}

Expand Down Expand Up @@ -164,8 +164,8 @@ func resourceDeployKeysDelete(ctx context.Context, d *schema.ResourceData, m int
return diag.FromErr(err)
}

_, err = deployApi.RepositoriesWorkspaceRepoSlugDeployKeysKeyIdDelete(c.AuthContext, keyId, repo, workspace)
if err := handleClientError(err); err != nil {
res, err := deployApi.RepositoriesWorkspaceRepoSlugDeployKeysKeyIdDelete(c.AuthContext, keyId, repo, workspace)
if err := handleClientError(res, err); err != nil {
return diag.FromErr(err)
}

Expand Down
4 changes: 2 additions & 2 deletions bitbucket/resource_deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,8 +248,8 @@ func resourceDeploymentDelete(ctx context.Context, d *schema.ResourceData, m int
return diag.FromErr(err)
}

_, err = deployApi.DeleteEnvironmentForRepository(c.AuthContext, workspaceId, repoId, d.Get("uuid").(string))
if err := handleClientError(err); err != nil {
res, err := deployApi.DeleteEnvironmentForRepository(c.AuthContext, workspaceId, repoId, d.Get("uuid").(string))
if err := handleClientError(res, err); err != nil {
return diag.FromErr(err)
}

Expand Down
14 changes: 7 additions & 7 deletions bitbucket/resource_deployment_variable.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@ func resourceDeploymentVariableCreate(ctx context.Context, d *schema.ResourceDat
return diag.FromErr(err)
}

rvRes, _, err := pipeApi.CreateDeploymentVariable(c.AuthContext, *rvcr, workspace, repoSlug, deployment)
if err := handleClientError(err); err != nil {
rvRes, res, err := pipeApi.CreateDeploymentVariable(c.AuthContext, *rvcr, workspace, repoSlug, deployment)
if err := handleClientError(res, err); err != nil {
return diag.FromErr(err)
}

Expand Down Expand Up @@ -113,7 +113,7 @@ func resourceDeploymentVariableRead(ctx context.Context, d *schema.ResourceData,
return nil
}

if err := handleClientError(err); err != nil {
if err := handleClientError(res, err); err != nil {
return diag.FromErr(err)
}

Expand Down Expand Up @@ -162,8 +162,8 @@ func resourceDeploymentVariableUpdate(ctx context.Context, d *schema.ResourceDat
return diag.FromErr(err)
}

_, _, err = pipeApi.UpdateDeploymentVariable(c.AuthContext, *rvcr, workspace, repoSlug, deployment, d.Get("uuid").(string))
if err := handleClientError(err); err != nil {
_, res, err := pipeApi.UpdateDeploymentVariable(c.AuthContext, *rvcr, workspace, repoSlug, deployment, d.Get("uuid").(string))
if err := handleClientError(res, err); err != nil {
return diag.FromErr(err)
}

Expand All @@ -180,8 +180,8 @@ func resourceDeploymentVariableDelete(ctx context.Context, d *schema.ResourceDat
return diag.FromErr(err)
}

_, err = pipeApi.DeleteDeploymentVariable(c.AuthContext, workspace, repoSlug, deployment, d.Get("uuid").(string))
if err := handleClientError(err); err != nil {
res, err := pipeApi.DeleteDeploymentVariable(c.AuthContext, workspace, repoSlug, deployment, d.Get("uuid").(string))
if err := handleClientError(res, err); err != nil {
return diag.FromErr(err)
}

Expand Down
10 changes: 5 additions & 5 deletions bitbucket/resource_forked_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,8 +210,8 @@ func resourceForkedRepositoryCreate(ctx context.Context, d *schema.ResourceData,
repoBody := &bitbucket.RepositoriesApiRepositoriesWorkspaceRepoSlugForksPostOpts{
Body: optional.NewInterface(requestRepo),
}
_, _, err := repoApi.RepositoriesWorkspaceRepoSlugForksPost(c.AuthContext, parentRepoSlug, parentWorkspace, repoBody)
if err := handleClientError(err); err != nil {
_, res, err := repoApi.RepositoriesWorkspaceRepoSlugForksPost(c.AuthContext, parentRepoSlug, parentWorkspace, repoBody)
if err := handleClientError(res, err); err != nil {
return diag.FromErr(err)
}

Expand All @@ -228,7 +228,7 @@ func resourceForkedRepositoryCreate(ctx context.Context, d *schema.ResourceData,
)
}

if err := handleClientError(err); err != nil {
if err := handleClientError(pipelineResponse, err); err != nil {
return resource.NonRetryableError(err)
}
return nil
Expand Down Expand Up @@ -272,7 +272,7 @@ func resourceForkedRepositoryRead(ctx context.Context, d *schema.ResourceData, m
return nil
}

if err := handleClientError(err); err != nil {
if err := handleClientError(res, err); err != nil {
return diag.FromErr(err)
}

Expand Down Expand Up @@ -311,7 +311,7 @@ func resourceForkedRepositoryRead(ctx context.Context, d *schema.ResourceData, m
d.Set("link", flattenLinks(repoRes.Links))

pipelinesConfigReq, res, err := pipeApi.GetRepositoryPipelineConfig(c.AuthContext, workspace, repoSlug)
if err := handleClientError(err); err != nil && res.StatusCode != http.StatusNotFound {
if err := handleClientError(res, err); err != nil && res.StatusCode != http.StatusNotFound {
return diag.FromErr(err)
}

Expand Down
Loading
Loading