DrFaust92 / terraform-provider-bitbucket

Terraform Bitbucket Cloud provider.
https://registry.terraform.io/providers/drfaust92/bitbucket
Mozilla Public License 2.0
38 stars 30 forks source link

regression: apply output no longer shows error info, only http status code returned #93

Closed blbradley closed 1 year ago

blbradley commented 1 year ago

Terraform Version

Terraform v0.15.5
on linux_amd64
+ provider registry.terraform.io/drfaust92/bitbucket v2.21.3
+ provider registry.terraform.io/hashicorp/aws v3.75.2

Affected Resource(s)

Please list the resources as a list, for example:

If this issue appears to affect multiple resources, it may be an issue with Terraform's core, so please mention this.

Expected Behavior

What should have happened?

Error: API Error: 400 2.0/repositories/kaena1/api-cms/branch-restrictions write or admin access required: bitbucket-username

provider version 2.4.1 and terraform 0.14.11

Actual Behavior

│ Error: 400 Bad Request

provider version 2.21.3 and terraform 0.15.5

This is more difficult to debug.

Nkmol commented 1 year ago

I had a quick look at this as I also noticed no raw API error messages at some resources.

It seems that the Bitbucket Client that is being used (which is not used for all the resources), sometimes returns a custom/unmarshalled error object instead of the basic error object. This custom error object contains properties which hold the actual API error, and these properties are basically never accessed in the Terraform resource.

It was able to fix this by returning two errors: one error for internal errors and one for explicitly caught HTTP (Bitbucket) errors. Then I can simply handle the HTTP error in the Terraform resource code:

if httpErr != nil {
    return diag.Errorf("%s: %s", httpErr.Error(), string(httpErr.Body()))
}

Which results JSONfied result:

│ Error: 400 Bad Request: {"type": "error", "error": {"message": "kind require_passing_builds_to_merge requires value"}} │ │ with bitbucket_branch_restriction.development, │ on example.tf line 13, in resource "bitbucket_branch_restriction" "development": │ 13: resource "bitbucket_branch_restriction" "development" { │

I can see if I can directly output the “error” value, so we get:

│ Error: 400 Bad Request: "kind require_passing_builds_to_merge requires value"


Just wanted to share my first findings :) This certainly hits more resources, so looking at how I can structure this.

@DrFaust92 I see there are also HTTP calls being done directly from Terraform. Had it been an idea to deprecate the bitbucket/client.go of this repo in place for explicit https://github.com/DrFaust92/bitbucket-go-client calls?

DrFaust92 commented 1 year ago

thanks @Nkmol for looking into this. i wish to deprecate the raw http requests but the go client is not working (pagination doesnt always, work, responses are incorrect /: this mostly stems for the raw swagger spec)