diff --git a/internal/provider/dashboard_resource.go b/internal/provider/dashboard_resource.go index 2e532ea..7520b58 100644 --- a/internal/provider/dashboard_resource.go +++ b/internal/provider/dashboard_resource.go @@ -3,6 +3,7 @@ package provider import ( "context" "encoding/json" + "errors" "fmt" "github.com/google/go-cmp/cmp" @@ -19,8 +20,19 @@ var ( _ resource.Resource = &dashboardResource{} _ resource.ResourceWithConfigure = &dashboardResource{} _ resource.ResourceWithImportState = &dashboardResource{} + + errLayoutMissing = errors.New("layout missing for widget id") + errWidgetProperties = errors.New("widget properties error") ) +func newLayoutError(id string) error { + return fmt.Errorf("%w: %s", errLayoutMissing, id) +} + +func newWidgetPropertiesError(msg string, id string) error { + return fmt.Errorf("%w: %s id:%s", errWidgetProperties, msg, id) +} + func NewDashboardResource() resource.Resource { return &dashboardResource{} } @@ -80,7 +92,7 @@ func setDashboardValuesFromCreate(dashboard *swoClient.CreateDashboardResult, pl for _, w := range dashboard.Widgets { lIdx := slices.IndexFunc(dashboard.Layout, func(l swoClient.CreateDashboardLayout) bool { return l.Id == w.Id }) if lIdx <= -1 { - return fmt.Errorf("layout missing for widget id: %s", w.Id) + return newLayoutError(w.Id) } // The layout that will give us the widget coordinates for comparison to the plan. @@ -120,7 +132,7 @@ func setDashboardValuesFromRead(dashboard *swoClient.ReadDashboardResult, state for _, w := range dashboard.Widgets { lIdx := slices.IndexFunc(dashboard.Layout, func(l swoClient.ReadDashboardLayout) bool { return l.Id == w.Id }) if lIdx <= -1 { - return fmt.Errorf("layout missing for widget id: %s", w.Id) + return newLayoutError(w.Id) } // We found the layout that will give us the widget coordinates for comparison to the plan. @@ -128,8 +140,7 @@ func setDashboardValuesFromRead(dashboard *swoClient.ReadDashboardResult, state isInState := false props, err := json.Marshal(w.Properties) if err != nil { - return fmt.Errorf("widget properties error: %s, id: %s", - err, w.Id) + return newWidgetPropertiesError(err.Error(), w.Id) } for wIdx := range state.Widgets { @@ -146,7 +157,7 @@ func setDashboardValuesFromRead(dashboard *swoClient.ReadDashboardResult, state var stateProps any err = json.Unmarshal([]byte(stateW.Properties.ValueString()), &stateProps) if err != nil { - return fmt.Errorf("widget properties error: %s, id: %s", err, w.Id) + return newWidgetPropertiesError(err.Error(), w.Id) } // The json string can be marshalled differently than what is specified in the terraform diff --git a/internal/provider/notificationSettings.go b/internal/provider/notificationSettings.go index 3e35e9f..d4555b4 100644 --- a/internal/provider/notificationSettings.go +++ b/internal/provider/notificationSettings.go @@ -2,10 +2,19 @@ package provider import ( "encoding/json" + "errors" "fmt" "log" ) +var ( + errUnsupportedNotificationType = errors.New("unsupported notification type") +) + +func newUnsupportedNotificationTypeError(notificationType string) error { + return fmt.Errorf("%w: %s", errUnsupportedNotificationType, notificationType) +} + type notificationSettings struct { Email *notificationSettingsEmail `tfsdk:"email"` Slack *notificationSettingsSlack `tfsdk:"slack"` @@ -261,7 +270,7 @@ func (m *notificationResourceModel) SetSettings(settings any) error { return err } } else { - return fmt.Errorf("unsupported notification type: %s", m.Type.ValueString()) + return newUnsupportedNotificationTypeError(m.Type.ValueString()) } return nil diff --git a/internal/provider/notification_schema.go b/internal/provider/notification_schema.go index 13b122c..a69ec96 100644 --- a/internal/provider/notification_schema.go +++ b/internal/provider/notification_schema.go @@ -2,6 +2,7 @@ package provider import ( "context" + "errors" "fmt" "regexp" "strings" @@ -25,6 +26,14 @@ const ( httpSchemeRegex = `^(http|https)` ) +var ( + errParse = errors.New("parser error") +) + +func newParseError(msg string) error { + return fmt.Errorf("%w: %s", errParse, msg) +} + // The main Notification Resource model that is derived from the schema. type notificationResourceModel struct { Id types.String `tfsdk:"id"` @@ -38,7 +47,7 @@ func (m *notificationResourceModel) ParseId() (id string, notificationType strin idParts := strings.Split(m.Id.ValueString(), ":") if len(idParts) != 2 || idParts[0] == "" || idParts[1] == "" { - err = fmt.Errorf("expected identifier with format: id:type. got: %q", m.Id) + err = newParseError(fmt.Sprintf("expected identifier with format id:type. got: %q", m.Id)) } else { id = idParts[0] notificationType = idParts[1]