Skip to content

Commit cc3201f

Browse files
authored
Merge pull request #125 from Nkmol/fix-error-handling-resource-branch-restriction
fix(branch-restriction): handle http errors
2 parents 543095d + ea9d622 commit cc3201f

File tree

2 files changed

+34
-8
lines changed

2 files changed

+34
-8
lines changed

bitbucket/error.go

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package bitbucket
2+
3+
import (
4+
"encoding/json"
5+
6+
"github.com/DrFaust92/bitbucket-go-client"
7+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
8+
)
9+
10+
func handleClientError(err error) diag.Diagnostics {
11+
httpErr, ok := err.(bitbucket.GenericSwaggerError)
12+
if ok {
13+
var httpError bitbucket.ModelError
14+
if err := json.Unmarshal(httpErr.Body(), &httpError); err != nil {
15+
return diag.Errorf(string(httpErr.Body()))
16+
}
17+
18+
return diag.Errorf("%s: %s", httpErr.Error(), httpError.Error_.Message)
19+
}
20+
21+
if err != nil {
22+
return diag.FromErr(err)
23+
}
24+
25+
return nil
26+
}

bitbucket/resource_branch_restriction.go

+8-8
Original file line numberDiff line numberDiff line change
@@ -196,8 +196,8 @@ func resourceBranchRestrictionsCreate(ctx context.Context, d *schema.ResourceDat
196196
workspace := d.Get("owner").(string)
197197
branchRestrictionReq, _, err := brApi.RepositoriesWorkspaceRepoSlugBranchRestrictionsPost(c.AuthContext, *branchRestriction, repo, workspace)
198198

199-
if err != nil {
200-
return diag.FromErr(err)
199+
if diag := handleClientError(err); diag != nil {
200+
return diag
201201
}
202202

203203
d.SetId(string(fmt.Sprintf("%v", branchRestrictionReq.Id)))
@@ -218,8 +218,8 @@ func resourceBranchRestrictionsRead(ctx context.Context, d *schema.ResourceData,
218218
return nil
219219
}
220220

221-
if err != nil {
222-
return diag.FromErr(err)
221+
if diag := handleClientError(err); diag != nil {
222+
return diag
223223
}
224224

225225
d.SetId(string(fmt.Sprintf("%v", brRes.Id)))
@@ -243,8 +243,8 @@ func resourceBranchRestrictionsUpdate(ctx context.Context, d *schema.ResourceDat
243243
*branchRestriction, url.PathEscape(d.Id()),
244244
d.Get("repository").(string), d.Get("owner").(string))
245245

246-
if err != nil {
247-
return diag.FromErr(err)
246+
if diag := handleClientError(err); diag != nil {
247+
return diag
248248
}
249249

250250
return resourceBranchRestrictionsRead(ctx, d, m)
@@ -262,8 +262,8 @@ func resourceBranchRestrictionsDelete(ctx context.Context, d *schema.ResourceDat
262262
return nil
263263
}
264264

265-
if err != nil {
266-
return diag.FromErr(err)
265+
if diag := handleClientError(err); diag != nil {
266+
return diag
267267
}
268268

269269
return nil

0 commit comments

Comments
 (0)