Skip to content

Commit

Permalink
avoid failing on wrapped errors (#351)
Browse files Browse the repository at this point in the history
* avoid failing on wrapped errors from external libraries
  • Loading branch information
uturunku1 committed Mar 7, 2022
1 parent dac29bf commit c29a9f8
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 8 deletions.
9 changes: 8 additions & 1 deletion configuration_version.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package tfe
import (
"bytes"
"context"
"errors"
"fmt"
"io/fs"
"net/url"
"os"
"time"
Expand Down Expand Up @@ -245,9 +247,14 @@ func (s *configurationVersions) ReadWithOptions(ctx context.Context, cvID string
// files on disk.
func (s *configurationVersions) Upload(ctx context.Context, u, path string) error {
file, err := os.Stat(path)

if err != nil {
return err
if errors.Is(err, fs.ErrNotExist) {
return fmt.Errorf(`failed to find terraform configuration files under the path "%v": %w`, path, err)
}
return fmt.Errorf(`unable to upload terraform configuration files from the path "%v": %w`, path, err)
}

if !file.Mode().IsDir() {
return ErrMissingDirectory
}
Expand Down
9 changes: 5 additions & 4 deletions logreader.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package tfe

import (
"context"
"errors"
"fmt"
"io"
"math"
Expand All @@ -23,19 +24,19 @@ type LogReader struct {
}

func (r *LogReader) Read(l []byte) (int, error) {
if written, err := r.read(l); err != io.ErrNoProgress {
if written, err := r.read(l); !errors.Is(err, io.ErrNoProgress) {
return written, err
}

// Loop until we can any data, the context is canceled or the
// run is finsished. If we would return right away without any
// data, we could and up causing a io.ErrNoProgress error.
// data, we could end up causing a io.ErrNoProgress error.
for r.reads = 1; ; r.reads++ {
select {
case <-r.ctx.Done():
return 0, r.ctx.Err()
case <-time.After(backoff(500, 2000, r.reads)):
if written, err := r.read(l); err != io.ErrNoProgress {
if written, err := r.read(l); !errors.Is(err, io.ErrNoProgress) {
return written, err
}
}
Expand Down Expand Up @@ -72,7 +73,7 @@ func (r *LogReader) read(l []byte) (int, error) {

// Read the retrieved chunk.
written, err := resp.Body.Read(l)
if err != nil && err != io.EOF {
if err != nil && !errors.Is(err, io.EOF) {
// Ignore io.EOF errors returned when reading from the response
// body as this indicates the end of the chunk and not the end
// of the logfile.
Expand Down
12 changes: 9 additions & 3 deletions tfe.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package tfe

import (
"errors"
"io/fs"
"log"
"sort"

Expand Down Expand Up @@ -870,8 +872,8 @@ func decodeErrorPayload(r *http.Response) ([]string, error) {
return errs, nil
}

func errorPayloadContains(errors []string, match string) bool {
for _, e := range errors {
func errorPayloadContains(payloadErrors []string, match string) bool {
for _, e := range payloadErrors {
if strings.Contains(e, match) {
return true
}
Expand All @@ -884,8 +886,12 @@ func packContents(path string) (*bytes.Buffer, error) {

file, err := os.Stat(path)
if err != nil {
return body, err
if errors.Is(err, fs.ErrNotExist) {
return body, fmt.Errorf(`failed to find files under the path "%v": %w`, path, err)
}
return body, fmt.Errorf(`unable to upload files from the path "%v": %w`, path, err)
}

if !file.Mode().IsDir() {
return body, ErrMissingDirectory
}
Expand Down

0 comments on commit c29a9f8

Please sign in to comment.