From 1bca78b25bd101b2dacc39ef2320e1ad3801fc97 Mon Sep 17 00:00:00 2001 From: Marco Garbelini Date: Mon, 12 Oct 2020 18:00:40 +0200 Subject: [PATCH] More informative error messages (#7) * More informative error messages * Removed redundancies --- drone/resource_cron.go | 8 +++++++- drone/resource_orgsecret.go | 5 ++--- drone/resource_repo.go | 8 +++++++- drone/resource_secret.go | 8 +++++++- drone/resource_user.go | 9 +++++---- 5 files changed, 28 insertions(+), 10 deletions(-) diff --git a/drone/resource_cron.go b/drone/resource_cron.go index f196d03..4b8c806 100644 --- a/drone/resource_cron.go +++ b/drone/resource_cron.go @@ -98,6 +98,9 @@ func resourceCronRead(data *schema.ResourceData, meta interface{}) error { } cronName := data.Get("name").(string) cron, err := client.Cron(owner, repo, cronName) + if err != nil { + return fmt.Errorf("failed to read Drone Cron: %s/%s/%s", owner, repo, cronName) + } return readCron(data, cron, owner, repo, err) } @@ -138,8 +141,11 @@ func resourceCronExists(data *schema.ResourceData, meta interface{}) (bool, erro } cronName := data.Get("name").(string) cron, err := client.Cron(owner, repo, cronName) + if err != nil { + return false, fmt.Errorf("failed to read Drone Cron: %s/%s/%s", owner, repo, cronName) + } - exists := (cron.Name == cronName) && (err == nil) + exists := cron.Name == cronName return exists, err } diff --git a/drone/resource_orgsecret.go b/drone/resource_orgsecret.go index 29529f9..7dd1dbf 100644 --- a/drone/resource_orgsecret.go +++ b/drone/resource_orgsecret.go @@ -2,7 +2,6 @@ package drone import ( "fmt" - "github.com/Lucretius/terraform-provider-drone/drone/utils" "github.com/drone/drone-go/drone" "github.com/hashicorp/terraform/helper/schema" @@ -74,7 +73,7 @@ func resourceOrgSecretRead(data *schema.ResourceData, meta interface{}) error { secret, err := client.OrgSecret(namespace, name) if err != nil { - return err + return fmt.Errorf("failed to read Drone Org Secret: %s/%s", namespace, name) } readOrgSecret(data, namespace, secret) @@ -120,7 +119,7 @@ func resourceOrgSecretExists(data *schema.ResourceData, meta interface{}) (bool, secret, err := client.OrgSecret(namespace, name) if err != nil { - return false, err + return false, fmt.Errorf("failed to read Drone Org Secret: %s/%s", namespace, name) } exists := (secret.Name == name) && (err == nil) diff --git a/drone/resource_repo.go b/drone/resource_repo.go index 4da5a60..fc3d0b6 100644 --- a/drone/resource_repo.go +++ b/drone/resource_repo.go @@ -104,6 +104,9 @@ func resourceRepoRead(data *schema.ResourceData, meta interface{}) error { } repository, err := client.Repo(owner, repo) + if err != nil { + return fmt.Errorf("failed to read Drone Repo: %s/%s", owner, repo) + } return readRepo(data, repository, err) } @@ -144,8 +147,11 @@ func resourceRepoExists(data *schema.ResourceData, meta interface{}) (bool, erro } repository, err := client.Repo(owner, repo) + if err != nil { + return false, fmt.Errorf("failed to read Drone Repo: %s/%s", owner, repo) + } - exists := (repository.Namespace == owner) && (repository.Name == repo) && (err == nil) + exists := (repository.Namespace == owner) && (repository.Name == repo) return exists, err } diff --git a/drone/resource_secret.go b/drone/resource_secret.go index 3ac476e..b586cd2 100644 --- a/drone/resource_secret.go +++ b/drone/resource_secret.go @@ -77,6 +77,9 @@ func resourceSecretRead(data *schema.ResourceData, meta interface{}) error { } secret, err := client.Secret(owner, repo, name) + if err != nil { + return fmt.Errorf("failed to read Drone Secret: %s/%s/%s", owner, repo, name) + } return readSecret(data, owner, repo, secret, err) } @@ -119,8 +122,11 @@ func resourceSecretExists(data *schema.ResourceData, meta interface{}) (bool, er } secret, err := client.Secret(owner, repo, name) + if err != nil { + return false, fmt.Errorf("failed to read Drone Secret: %s/%s/%s", owner, repo, name) + } - exists := (secret.Name == name) && (err == nil) + exists := secret.Name == name return exists, err } diff --git a/drone/resource_user.go b/drone/resource_user.go index e590c2d..705f05f 100644 --- a/drone/resource_user.go +++ b/drone/resource_user.go @@ -2,7 +2,6 @@ package drone import ( "fmt" - "github.com/Lucretius/terraform-provider-drone/drone/utils" "github.com/drone/drone-go/drone" "github.com/hashicorp/terraform/helper/schema" @@ -77,9 +76,8 @@ func resourceUserRead(data *schema.ResourceData, meta interface{}) error { client := meta.(drone.Client) user, err := client.User(data.Id()) - if err != nil { - return fmt.Errorf("Unable to read user %s: %v", user.Login, err) + return fmt.Errorf("failed to read Drone user with id: %s", data.Id()) } return readUser(data, user, err) @@ -97,8 +95,11 @@ func resourceUserExists(data *schema.ResourceData, meta interface{}) (bool, erro login := data.Id() user, err := client.User(login) + if err != nil { + return false, fmt.Errorf("failed to read Drone user with id: %s", data.Id()) + } - exists := (user.Login == login) && (err == nil) + exists := user.Login == login return exists, err }