Skip to content

Commit

Permalink
fix error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
zl0ty committed Nov 19, 2024
1 parent 14ae04a commit 895d0fe
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 7 deletions.
14 changes: 11 additions & 3 deletions cloudconnexa/data_source_application.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cloudconnexa

import (
"context"
"strings"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
Expand Down Expand Up @@ -60,7 +61,11 @@ func dataSourceApplicationRead(ctx context.Context, data *schema.ResourceData, i
if applicationId != "" {
application, err = c.Applications.Get(applicationId)
if err != nil {
return diag.FromErr(err)
if strings.Contains(err.Error(), "status code: 404") {
return append(diags, diag.Errorf("Application with id %s was not found", applicationId)...)
} else {
return append(diags, diag.FromErr(err)...)
}
}
if application == nil {
return append(diags, diag.Errorf("Application with id %s was not found", applicationId)...)
Expand Down Expand Up @@ -91,6 +96,9 @@ func dataSourceApplicationRead(ctx context.Context, data *schema.ResourceData, i
} else {
return append(diags, diag.Errorf("Application name or id is missing")...)
}
setApplicationData(data, application)
return nil
// setApplicationData(data, application)
data.SetId(application.Id)
data.Set("name", application.Name)
return diags
// return nil
}
2 changes: 1 addition & 1 deletion cloudconnexa/data_source_network.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ func dataSourceNetworkRead(ctx context.Context, d *schema.ResourceData, m interf
return append(diags, diag.Errorf("Network with name %s was not found", networkName)...)
}
} else {
return append(diags, diag.Errorf("Network name or id is missing")...)
return append(diags, diag.Errorf("Network name or id is missing")...)
}
d.SetId(network.Id)
d.Set("name", network.Name)
Expand Down
15 changes: 12 additions & 3 deletions cloudconnexa/data_source_user_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cloudconnexa

import (
"context"
"strings"

"github.com/openvpn/cloudconnexa-go-client/v2/cloudconnexa"

Expand Down Expand Up @@ -76,21 +77,29 @@ func dataSourceUserGroupRead(ctx context.Context, d *schema.ResourceData, m inte
if userGroupId != "" {
userGroup, err = c.UserGroups.Get(userGroupId)
if err != nil {
return append(diags, diag.FromErr(err)...)
if strings.Contains(err.Error(), "user group not found") {
return append(diags, diag.Errorf("User group with id %s was not found", userGroupId)...)
} else {
return append(diags, diag.FromErr(err)...)
}
}
if userGroup == nil {
return append(diags, diag.Errorf("User group with id %s was not found", userGroupId)...)
}
} else if userGroupName != "" {
userGroup, err = c.UserGroups.GetByName(userGroupName)
if err != nil {
return append(diags, diag.FromErr(err)...)
if strings.Contains(err.Error(), "user group not found") {
return append(diags, diag.Errorf("User group with name %s was not found", userGroupName)...)
} else {
return append(diags, diag.FromErr(err)...)
}
}
if userGroup == nil {
return append(diags, diag.Errorf("User group with name %s was not found", userGroupName)...)
}
} else {
return append(diags, diag.Errorf("User group name or group id is missing")...)
return append(diags, diag.Errorf("User group name or id is missing")...)
}
d.SetId(userGroup.ID)
d.Set("name", userGroup.Name)
Expand Down

0 comments on commit 895d0fe

Please sign in to comment.