Skip to content

Commit cf0f5e4

Browse files
ref: Use error wrapping for returned errors (#319)
* Use error wrapping for returned errors * Oops
1 parent cdc1e36 commit cf0f5e4

File tree

7 files changed

+35
-35
lines changed

7 files changed

+35
-35
lines changed

client.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ func (c *Client) InvalidateCache() {
294294
func (c *Client) InvalidateCacheEndpoint(endpoint string) error {
295295
u, err := url.Parse(endpoint)
296296
if err != nil {
297-
return fmt.Errorf("failed to parse URL for caching: %s", err)
297+
return fmt.Errorf("failed to parse URL for caching: %w", err)
298298
}
299299

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

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

445445
err = client.preLoadConfig(configPath)

config.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ func (c *Client) LoadConfig(options *LoadConfigOptions) error {
6565
if cfg.HasSection("default") {
6666
err := cfg.Section("default").MapTo(&defaultConfig)
6767
if err != nil {
68-
return fmt.Errorf("failed to map default profile: %s", err)
68+
return fmt.Errorf("failed to map default profile: %w", err)
6969
}
7070
}
7171

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

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

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

8787
if !options.SkipLoadProfile {
8888
if err := c.UseProfile(profileOption); err != nil {
89-
return fmt.Errorf("unable to use profile %s: %s", profileOption, err)
89+
return fmt.Errorf("unable to use profile %s: %w", profileOption, err)
9090
}
9191
}
9292

k8s/clientset.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,12 @@ func BuildClientsetFromConfig(
2020
) (kubernetes.Interface, error) {
2121
kubeConfigBytes, err := base64.StdEncoding.DecodeString(lkeKubeconfig.KubeConfig)
2222
if err != nil {
23-
return nil, fmt.Errorf("failed to decode kubeconfig: %s", err)
23+
return nil, fmt.Errorf("failed to decode kubeconfig: %w", err)
2424
}
2525

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

3131
if transportWrapper != nil {
@@ -34,7 +34,7 @@ func BuildClientsetFromConfig(
3434

3535
clientset, err := kubernetes.NewForConfig(restClientConfig)
3636
if err != nil {
37-
return nil, fmt.Errorf("failed to build k8s client from LKE cluster kubeconfig: %s", err)
37+
return nil, fmt.Errorf("failed to build k8s client from LKE cluster kubeconfig: %w", err)
3838
}
3939
return clientset, nil
4040
}

k8s/pkg/condition/lke.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ func ClusterHasReadyNode(ctx context.Context, options linodego.ClusterConditionO
2020

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

2626
for _, node := range nodes.Items {

pagination.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ func NewListOptions(page int, filter string) *ListOptions {
3939
func (l ListOptions) Hash() (string, error) {
4040
data, err := json.Marshal(l)
4141
if err != nil {
42-
return "", fmt.Errorf("failed to cache ListOptions: %s", err)
42+
return "", fmt.Errorf("failed to cache ListOptions: %w", err)
4343
}
4444

4545
h := sha256.New()

vlans.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,12 +74,12 @@ func (c *Client) GetVLANIPAMAddress(ctx context.Context, linodeID int, vlanLabel
7474
f.AddField(Eq, "interfaces", vlanLabel)
7575
vlanFilter, err := f.MarshalJSON()
7676
if err != nil {
77-
return "", fmt.Errorf("Unable to convert VLAN label: %s to a filterable object: %s", vlanLabel, err)
77+
return "", fmt.Errorf("Unable to convert VLAN label: %s to a filterable object: %w", vlanLabel, err)
7878
}
7979

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

8585
interfaces := cfgs[0].Interfaces

waitfor.go

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ func (client Client) WaitForInstanceStatus(ctx context.Context, instanceID int,
4141
return instance, nil
4242
}
4343
case <-ctx.Done():
44-
return nil, fmt.Errorf("Error waiting for Instance %d status %s: %s", instanceID, status, ctx.Err())
44+
return nil, fmt.Errorf("Error waiting for Instance %d status %s: %w", instanceID, status, ctx.Err())
4545
}
4646
}
4747
}
@@ -77,7 +77,7 @@ func (client Client) WaitForInstanceDiskStatus(ctx context.Context, instanceID i
7777
}
7878
}
7979
case <-ctx.Done():
80-
return nil, fmt.Errorf("Error waiting for Instance %d Disk %d status %s: %s", instanceID, diskID, status, ctx.Err())
80+
return nil, fmt.Errorf("Error waiting for Instance %d Disk %d status %s: %w", instanceID, diskID, status, ctx.Err())
8181
}
8282
}
8383
}
@@ -104,7 +104,7 @@ func (client Client) WaitForVolumeStatus(ctx context.Context, volumeID int, stat
104104
return volume, nil
105105
}
106106
case <-ctx.Done():
107-
return nil, fmt.Errorf("Error waiting for Volume %d status %s: %s", volumeID, status, ctx.Err())
107+
return nil, fmt.Errorf("Error waiting for Volume %d status %s: %w", volumeID, status, ctx.Err())
108108
}
109109
}
110110
}
@@ -131,7 +131,7 @@ func (client Client) WaitForSnapshotStatus(ctx context.Context, instanceID int,
131131
return snapshot, nil
132132
}
133133
case <-ctx.Done():
134-
return nil, fmt.Errorf("Error waiting for Instance %d Snapshot %d status %s: %s", instanceID, snapshotID, status, ctx.Err())
134+
return nil, fmt.Errorf("Error waiting for Instance %d Snapshot %d status %s: %w", instanceID, snapshotID, status, ctx.Err())
135135
}
136136
}
137137
}
@@ -164,7 +164,7 @@ func (client Client) WaitForVolumeLinodeID(ctx context.Context, volumeID int, li
164164
return volume, nil
165165
}
166166
case <-ctx.Done():
167-
return nil, fmt.Errorf("Error waiting for Volume %d to have Instance %v: %s", volumeID, linodeID, ctx.Err())
167+
return nil, fmt.Errorf("Error waiting for Volume %d to have Instance %v: %w", volumeID, linodeID, ctx.Err())
168168
}
169169
}
170170
}
@@ -191,7 +191,7 @@ func (client Client) WaitForLKEClusterStatus(ctx context.Context, clusterID int,
191191
return cluster, nil
192192
}
193193
case <-ctx.Done():
194-
return nil, fmt.Errorf("Error waiting for Cluster %d status %s: %s", clusterID, status, ctx.Err())
194+
return nil, fmt.Errorf("Error waiting for Cluster %d status %s: %w", clusterID, status, ctx.Err())
195195
}
196196
}
197197
}
@@ -234,7 +234,7 @@ func (client Client) WaitForLKEClusterConditions(
234234

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

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

262262
case <-ctx.Done():
263-
return fmt.Errorf("Error waiting for cluster %d conditions: %s", clusterID, ctx.Err())
263+
return fmt.Errorf("Error waiting for cluster %d conditions: %w", clusterID, ctx.Err())
264264
}
265265
}
266266
}
@@ -291,7 +291,7 @@ func (client Client) WaitForEventFinished(ctx context.Context, id any, entityTyp
291291
// All of the filter supported types have int ids
292292
filterableEntityID, err := strconv.Atoi(fmt.Sprintf("%v", id))
293293
if err != nil {
294-
return nil, fmt.Errorf("Error parsing Entity ID %q for optimized WaitForEventFinished EventType %q: %s", id, entityType, err)
294+
return nil, fmt.Errorf("Error parsing Entity ID %q for optimized WaitForEventFinished EventType %q: %w", id, entityType, err)
295295
}
296296
filter.AddField(Eq, "entity.id", filterableEntityID)
297297
filter.AddField(Eq, "entity.type", entityType)
@@ -397,7 +397,7 @@ func (client Client) WaitForEventFinished(ctx context.Context, id any, entityTyp
397397
lastLog = nextLog
398398
}
399399
case <-ctx.Done():
400-
return nil, fmt.Errorf("Error waiting for Event Status '%s' of %s %v action '%s': %s", EventFinished, titledEntityType, id, action, ctx.Err())
400+
return nil, fmt.Errorf("Error waiting for Event Status '%s' of %s %v action '%s': %w", EventFinished, titledEntityType, id, action, ctx.Err())
401401
}
402402
}
403403
}
@@ -424,7 +424,7 @@ func (client Client) WaitForImageStatus(ctx context.Context, imageID string, sta
424424
return image, nil
425425
}
426426
case <-ctx.Done():
427-
return nil, fmt.Errorf("failed to wait for Image %s status %s: %s", imageID, status, ctx.Err())
427+
return nil, fmt.Errorf("failed to wait for Image %s status %s: %w", imageID, status, ctx.Err())
428428
}
429429
}
430430
}
@@ -451,7 +451,7 @@ func (client Client) WaitForMySQLDatabaseBackup(ctx context.Context, dbID int, l
451451
}
452452
}
453453
case <-ctx.Done():
454-
return nil, fmt.Errorf("failed to wait for backup %s: %s", label, ctx.Err())
454+
return nil, fmt.Errorf("failed to wait for backup %s: %w", label, ctx.Err())
455455
}
456456
}
457457
}
@@ -478,7 +478,7 @@ func (client Client) WaitForMongoDatabaseBackup(ctx context.Context, dbID int, l
478478
}
479479
}
480480
case <-ctx.Done():
481-
return nil, fmt.Errorf("failed to wait for backup %s: %s", label, ctx.Err())
481+
return nil, fmt.Errorf("failed to wait for backup %s: %w", label, ctx.Err())
482482
}
483483
}
484484
}
@@ -505,7 +505,7 @@ func (client Client) WaitForPostgresDatabaseBackup(ctx context.Context, dbID int
505505
}
506506
}
507507
case <-ctx.Done():
508-
return nil, fmt.Errorf("failed to wait for backup %s: %s", label, ctx.Err())
508+
return nil, fmt.Errorf("failed to wait for backup %s: %w", label, ctx.Err())
509509
}
510510
}
511511
}
@@ -559,14 +559,14 @@ func (client Client) WaitForDatabaseStatus(
559559

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

565565
if currentStatus == status {
566566
return nil
567567
}
568568
case <-ctx.Done():
569-
return fmt.Errorf("failed to wait for database %d status: %s", dbID, ctx.Err())
569+
return fmt.Errorf("failed to wait for database %d status: %w", dbID, ctx.Err())
570570
}
571571
}
572572
}
@@ -585,7 +585,7 @@ func (client Client) NewEventPoller(
585585
}
586586

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

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

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

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

703703
event, err := p.WaitForLatestUnknownEvent(ctx)
704704
if err != nil {
705-
return nil, fmt.Errorf("failed to wait for event: %s", err)
705+
return nil, fmt.Errorf("failed to wait for event: %w", err)
706706
}
707707

708708
for {
709709
select {
710710
case <-ticker.C:
711711
event, err := p.client.GetEvent(ctx, event.ID)
712712
if err != nil {
713-
return nil, fmt.Errorf("failed to get event: %s", err)
713+
return nil, fmt.Errorf("failed to get event: %w", err)
714714
}
715715

716716
switch event.Status {
@@ -722,7 +722,7 @@ func (p *EventPoller) WaitForFinished(
722722
continue
723723
}
724724
case <-ctx.Done():
725-
return nil, fmt.Errorf("failed to wait for event: %s", ctx.Err())
725+
return nil, fmt.Errorf("failed to wait for event: %w", ctx.Err())
726726
}
727727
}
728728
}

0 commit comments

Comments
 (0)