github.com/fastly/cli@v1.7.2-0.20240304164155-9d0f1d77c3bf/pkg/errors/deduce_test.go (about)

     1  package errors_test
     2  
     3  import (
     4  	"fmt"
     5  	"net/http"
     6  	"os"
     7  	"testing"
     8  
     9  	"github.com/fastly/cli/pkg/errors"
    10  	"github.com/fastly/cli/pkg/testutil"
    11  	"github.com/fastly/go-fastly/v9/fastly"
    12  )
    13  
    14  func TestDeduce(t *testing.T) {
    15  	var (
    16  		re1             = errors.RemediationError{Inner: fmt.Errorf("foo")}
    17  		re2             = errors.RemediationError{Inner: fmt.Errorf("bar"), Remediation: "Reticulate your splines."}
    18  		http503         = &fastly.HTTPError{StatusCode: http.StatusInternalServerError}
    19  		http401         = &fastly.HTTPError{StatusCode: http.StatusUnauthorized}
    20  		wrappedNotExist = fmt.Errorf("couldn't do the thing: %w", os.ErrNotExist)
    21  	)
    22  
    23  	for _, testcase := range []struct {
    24  		name  string
    25  		input error
    26  		want  errors.RemediationError
    27  	}{
    28  		{
    29  			name:  "RemediationError with no remediation",
    30  			input: re1,
    31  			want:  re1,
    32  		},
    33  		{
    34  			name:  "RemediationError with remediation",
    35  			input: re2,
    36  			want:  re2,
    37  		},
    38  		{
    39  			name:  "fastly.HTTPError 503",
    40  			input: http503,
    41  			want:  errors.RemediationError{Inner: errors.SimplifyFastlyError(*http503), Remediation: errors.BugRemediation},
    42  		},
    43  		{
    44  			name:  "fastly.HTTPError 401",
    45  			input: http401,
    46  			want:  errors.RemediationError{Inner: errors.SimplifyFastlyError(*http401), Remediation: errors.AuthRemediation},
    47  		},
    48  		{
    49  			name:  "wrapped os.ErrNotExist",
    50  			input: wrappedNotExist,
    51  			want:  errors.RemediationError{Inner: wrappedNotExist, Remediation: errors.HostRemediation},
    52  		},
    53  		{
    54  			name:  "temporary network error",
    55  			input: isTemporary{fmt.Errorf("baz")},
    56  			want:  errors.RemediationError{Inner: fmt.Errorf("baz"), Remediation: errors.NetworkRemediation},
    57  		},
    58  	} {
    59  		t.Run(testcase.name, func(t *testing.T) {
    60  			have := errors.Deduce(testcase.input)
    61  			testutil.AssertString(t, testcase.want.Error(), have.Error())
    62  			testutil.AssertString(t, testcase.want.Remediation, have.Remediation)
    63  		})
    64  	}
    65  }
    66  
    67  type isTemporary struct{ error }
    68  
    69  func (isTemporary) Temporary() bool { return true }