Skip to content

Commit

Permalink
ref: Use error wrapping for returned errors (#319)
Browse files Browse the repository at this point in the history
* Use error wrapping for returned errors

* Oops
  • Loading branch information
lgarber-akamai authored Apr 12, 2023
1 parent cdc1e36 commit cf0f5e4
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 35 deletions.
4 changes: 2 additions & 2 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ func (c *Client) InvalidateCache() {
func (c *Client) InvalidateCacheEndpoint(endpoint string) error {
u, err := url.Parse(endpoint)
if err != nil {
return fmt.Errorf("failed to parse URL for caching: %s", err)
return fmt.Errorf("failed to parse URL for caching: %w", err)
}

c.cachedEntryLock.Lock()
Expand Down Expand Up @@ -439,7 +439,7 @@ func NewClientFromEnv(hc *http.Client) (*Client, error) {

// We should only load the config if the config file exists
if _, err := os.Stat(configPath); err != nil {
return nil, fmt.Errorf("error loading config file %s: %s", configPath, err)
return nil, fmt.Errorf("error loading config file %s: %w", configPath, err)
}

err = client.preLoadConfig(configPath)
Expand Down
6 changes: 3 additions & 3 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func (c *Client) LoadConfig(options *LoadConfigOptions) error {
if cfg.HasSection("default") {
err := cfg.Section("default").MapTo(&defaultConfig)
if err != nil {
return fmt.Errorf("failed to map default profile: %s", err)
return fmt.Errorf("failed to map default profile: %w", err)
}
}

Expand All @@ -76,7 +76,7 @@ func (c *Client) LoadConfig(options *LoadConfigOptions) error {

f := defaultConfig
if err := profile.MapTo(&f); err != nil {
return fmt.Errorf("failed to map values: %s", err)
return fmt.Errorf("failed to map values: %w", err)
}

result[name] = f
Expand All @@ -86,7 +86,7 @@ func (c *Client) LoadConfig(options *LoadConfigOptions) error {

if !options.SkipLoadProfile {
if err := c.UseProfile(profileOption); err != nil {
return fmt.Errorf("unable to use profile %s: %s", profileOption, err)
return fmt.Errorf("unable to use profile %s: %w", profileOption, err)
}
}

Expand Down
6 changes: 3 additions & 3 deletions k8s/clientset.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ func BuildClientsetFromConfig(
) (kubernetes.Interface, error) {
kubeConfigBytes, err := base64.StdEncoding.DecodeString(lkeKubeconfig.KubeConfig)
if err != nil {
return nil, fmt.Errorf("failed to decode kubeconfig: %s", err)
return nil, fmt.Errorf("failed to decode kubeconfig: %w", err)
}

restClientConfig, err := clientcmd.RESTConfigFromKubeConfig(kubeConfigBytes)
if err != nil {
return nil, fmt.Errorf("failed to parse LKE cluster kubeconfig: %s", err)
return nil, fmt.Errorf("failed to parse LKE cluster kubeconfig: %w", err)
}

if transportWrapper != nil {
Expand All @@ -34,7 +34,7 @@ func BuildClientsetFromConfig(

clientset, err := kubernetes.NewForConfig(restClientConfig)
if err != nil {
return nil, fmt.Errorf("failed to build k8s client from LKE cluster kubeconfig: %s", err)
return nil, fmt.Errorf("failed to build k8s client from LKE cluster kubeconfig: %w", err)
}
return clientset, nil
}
2 changes: 1 addition & 1 deletion k8s/pkg/condition/lke.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func ClusterHasReadyNode(ctx context.Context, options linodego.ClusterConditionO

nodes, err := clientset.CoreV1().Nodes().List(ctx, v1.ListOptions{})
if err != nil {
return false, fmt.Errorf("failed to get nodes for cluster: %s", err)
return false, fmt.Errorf("failed to get nodes for cluster: %w", err)
}

for _, node := range nodes.Items {
Expand Down
2 changes: 1 addition & 1 deletion pagination.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func NewListOptions(page int, filter string) *ListOptions {
func (l ListOptions) Hash() (string, error) {
data, err := json.Marshal(l)
if err != nil {
return "", fmt.Errorf("failed to cache ListOptions: %s", err)
return "", fmt.Errorf("failed to cache ListOptions: %w", err)
}

h := sha256.New()
Expand Down
4 changes: 2 additions & 2 deletions vlans.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,12 @@ func (c *Client) GetVLANIPAMAddress(ctx context.Context, linodeID int, vlanLabel
f.AddField(Eq, "interfaces", vlanLabel)
vlanFilter, err := f.MarshalJSON()
if err != nil {
return "", fmt.Errorf("Unable to convert VLAN label: %s to a filterable object: %s", vlanLabel, err)
return "", fmt.Errorf("Unable to convert VLAN label: %s to a filterable object: %w", vlanLabel, err)
}

cfgs, err := c.ListInstanceConfigs(ctx, linodeID, &ListOptions{Filter: string(vlanFilter)})
if err != nil {
return "", fmt.Errorf("Fetching configs for instance %v failed: %s", linodeID, err)
return "", fmt.Errorf("Fetching configs for instance %v failed: %w", linodeID, err)
}

interfaces := cfgs[0].Interfaces
Expand Down
46 changes: 23 additions & 23 deletions waitfor.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func (client Client) WaitForInstanceStatus(ctx context.Context, instanceID int,
return instance, nil
}
case <-ctx.Done():
return nil, fmt.Errorf("Error waiting for Instance %d status %s: %s", instanceID, status, ctx.Err())
return nil, fmt.Errorf("Error waiting for Instance %d status %s: %w", instanceID, status, ctx.Err())
}
}
}
Expand Down Expand Up @@ -77,7 +77,7 @@ func (client Client) WaitForInstanceDiskStatus(ctx context.Context, instanceID i
}
}
case <-ctx.Done():
return nil, fmt.Errorf("Error waiting for Instance %d Disk %d status %s: %s", instanceID, diskID, status, ctx.Err())
return nil, fmt.Errorf("Error waiting for Instance %d Disk %d status %s: %w", instanceID, diskID, status, ctx.Err())
}
}
}
Expand All @@ -104,7 +104,7 @@ func (client Client) WaitForVolumeStatus(ctx context.Context, volumeID int, stat
return volume, nil
}
case <-ctx.Done():
return nil, fmt.Errorf("Error waiting for Volume %d status %s: %s", volumeID, status, ctx.Err())
return nil, fmt.Errorf("Error waiting for Volume %d status %s: %w", volumeID, status, ctx.Err())
}
}
}
Expand All @@ -131,7 +131,7 @@ func (client Client) WaitForSnapshotStatus(ctx context.Context, instanceID int,
return snapshot, nil
}
case <-ctx.Done():
return nil, fmt.Errorf("Error waiting for Instance %d Snapshot %d status %s: %s", instanceID, snapshotID, status, ctx.Err())
return nil, fmt.Errorf("Error waiting for Instance %d Snapshot %d status %s: %w", instanceID, snapshotID, status, ctx.Err())
}
}
}
Expand Down Expand Up @@ -164,7 +164,7 @@ func (client Client) WaitForVolumeLinodeID(ctx context.Context, volumeID int, li
return volume, nil
}
case <-ctx.Done():
return nil, fmt.Errorf("Error waiting for Volume %d to have Instance %v: %s", volumeID, linodeID, ctx.Err())
return nil, fmt.Errorf("Error waiting for Volume %d to have Instance %v: %w", volumeID, linodeID, ctx.Err())
}
}
}
Expand All @@ -191,7 +191,7 @@ func (client Client) WaitForLKEClusterStatus(ctx context.Context, clusterID int,
return cluster, nil
}
case <-ctx.Done():
return nil, fmt.Errorf("Error waiting for Cluster %d status %s: %s", clusterID, status, ctx.Err())
return nil, fmt.Errorf("Error waiting for Cluster %d status %s: %w", clusterID, status, ctx.Err())
}
}
}
Expand Down Expand Up @@ -234,7 +234,7 @@ func (client Client) WaitForLKEClusterConditions(

lkeKubeConfig, err := client.GetLKEClusterKubeconfig(ctx, clusterID)
if err != nil {
return fmt.Errorf("failed to get Kubeconfig for LKE cluster %d: %s", clusterID, err)
return fmt.Errorf("failed to get Kubeconfig for LKE cluster %d: %w", clusterID, err)
}

ticker := time.NewTicker(client.millisecondsPerPoll * time.Millisecond)
Expand All @@ -260,7 +260,7 @@ func (client Client) WaitForLKEClusterConditions(
}

case <-ctx.Done():
return fmt.Errorf("Error waiting for cluster %d conditions: %s", clusterID, ctx.Err())
return fmt.Errorf("Error waiting for cluster %d conditions: %w", clusterID, ctx.Err())
}
}
}
Expand Down Expand Up @@ -291,7 +291,7 @@ func (client Client) WaitForEventFinished(ctx context.Context, id any, entityTyp
// All of the filter supported types have int ids
filterableEntityID, err := strconv.Atoi(fmt.Sprintf("%v", id))
if err != nil {
return nil, fmt.Errorf("Error parsing Entity ID %q for optimized WaitForEventFinished EventType %q: %s", id, entityType, err)
return nil, fmt.Errorf("Error parsing Entity ID %q for optimized WaitForEventFinished EventType %q: %w", id, entityType, err)
}
filter.AddField(Eq, "entity.id", filterableEntityID)
filter.AddField(Eq, "entity.type", entityType)
Expand Down Expand Up @@ -397,7 +397,7 @@ func (client Client) WaitForEventFinished(ctx context.Context, id any, entityTyp
lastLog = nextLog
}
case <-ctx.Done():
return nil, fmt.Errorf("Error waiting for Event Status '%s' of %s %v action '%s': %s", EventFinished, titledEntityType, id, action, ctx.Err())
return nil, fmt.Errorf("Error waiting for Event Status '%s' of %s %v action '%s': %w", EventFinished, titledEntityType, id, action, ctx.Err())
}
}
}
Expand All @@ -424,7 +424,7 @@ func (client Client) WaitForImageStatus(ctx context.Context, imageID string, sta
return image, nil
}
case <-ctx.Done():
return nil, fmt.Errorf("failed to wait for Image %s status %s: %s", imageID, status, ctx.Err())
return nil, fmt.Errorf("failed to wait for Image %s status %s: %w", imageID, status, ctx.Err())
}
}
}
Expand All @@ -451,7 +451,7 @@ func (client Client) WaitForMySQLDatabaseBackup(ctx context.Context, dbID int, l
}
}
case <-ctx.Done():
return nil, fmt.Errorf("failed to wait for backup %s: %s", label, ctx.Err())
return nil, fmt.Errorf("failed to wait for backup %s: %w", label, ctx.Err())
}
}
}
Expand All @@ -478,7 +478,7 @@ func (client Client) WaitForMongoDatabaseBackup(ctx context.Context, dbID int, l
}
}
case <-ctx.Done():
return nil, fmt.Errorf("failed to wait for backup %s: %s", label, ctx.Err())
return nil, fmt.Errorf("failed to wait for backup %s: %w", label, ctx.Err())
}
}
}
Expand All @@ -505,7 +505,7 @@ func (client Client) WaitForPostgresDatabaseBackup(ctx context.Context, dbID int
}
}
case <-ctx.Done():
return nil, fmt.Errorf("failed to wait for backup %s: %s", label, ctx.Err())
return nil, fmt.Errorf("failed to wait for backup %s: %w", label, ctx.Err())
}
}
}
Expand Down Expand Up @@ -559,14 +559,14 @@ func (client Client) WaitForDatabaseStatus(

currentStatus, err := statusHandler(ctx, client, dbID)
if err != nil {
return fmt.Errorf("failed to get db status: %s", err)
return fmt.Errorf("failed to get db status: %w", err)
}

if currentStatus == status {
return nil
}
case <-ctx.Done():
return fmt.Errorf("failed to wait for database %d status: %s", dbID, ctx.Err())
return fmt.Errorf("failed to wait for database %d status: %w", dbID, ctx.Err())
}
}
}
Expand All @@ -585,7 +585,7 @@ func (client Client) NewEventPoller(
}

if err := result.PreTask(ctx); err != nil {
return nil, fmt.Errorf("failed to run pretask: %s", err)
return nil, fmt.Errorf("failed to run pretask: %w", err)
}

return &result, nil
Expand Down Expand Up @@ -632,7 +632,7 @@ func (p *EventPoller) PreTask(ctx context.Context) error {
PageOptions: &PageOptions{Page: 1},
})
if err != nil {
return fmt.Errorf("failed to list events: %s", err)
return fmt.Errorf("failed to list events: %w", err)
}

eventIDs := make(map[int]bool, len(events))
Expand Down Expand Up @@ -672,7 +672,7 @@ func (p *EventPoller) WaitForLatestUnknownEvent(ctx context.Context) (*Event, er
case <-ticker.C:
events, err := p.client.ListEvents(ctx, &listOpts)
if err != nil {
return nil, fmt.Errorf("failed to list events: %s", err)
return nil, fmt.Errorf("failed to list events: %w", err)
}

for _, event := range events {
Expand All @@ -685,7 +685,7 @@ func (p *EventPoller) WaitForLatestUnknownEvent(ctx context.Context) (*Event, er
}
}
case <-ctx.Done():
return nil, fmt.Errorf("failed to wait for event: %s", ctx.Err())
return nil, fmt.Errorf("failed to wait for event: %w", ctx.Err())
}
}
}
Expand All @@ -702,15 +702,15 @@ func (p *EventPoller) WaitForFinished(

event, err := p.WaitForLatestUnknownEvent(ctx)
if err != nil {
return nil, fmt.Errorf("failed to wait for event: %s", err)
return nil, fmt.Errorf("failed to wait for event: %w", err)
}

for {
select {
case <-ticker.C:
event, err := p.client.GetEvent(ctx, event.ID)
if err != nil {
return nil, fmt.Errorf("failed to get event: %s", err)
return nil, fmt.Errorf("failed to get event: %w", err)
}

switch event.Status {
Expand All @@ -722,7 +722,7 @@ func (p *EventPoller) WaitForFinished(
continue
}
case <-ctx.Done():
return nil, fmt.Errorf("failed to wait for event: %s", ctx.Err())
return nil, fmt.Errorf("failed to wait for event: %w", ctx.Err())
}
}
}

0 comments on commit cf0f5e4

Please sign in to comment.