Skip to content

[FEATURE REQUEST] Improve errors.Deduce #906

Open
@awilliams-fastly

Description

@awilliams-fastly

The errors.Deduce function drops important error information in certain cases.

Example

func TestDeduce_FastlyError(t *testing.T) {
	fstHTTPErr := fastly.HTTPError{
		Errors: []*fastly.ErrorObject{
			{Title: "the title of the HTTPError"},
		},
		StatusCode: http.StatusTeapot,
	}

	prefix := "important detail here"
	wrapped := fmt.Errorf("%s: %w", prefix, &fstHTTPErr)

	if got := errors.Deduce(wrapped).Error(); !strings.Contains(got, prefix) {
		t.Fatalf("got: %q\nwant contain: %q", got, prefix)
	} else {
		t.Logf("got: %s", got)
	}
}

=== RUN   TestDeduce_FastlyError
    deduce_test.go:80: got: "the Fastly API returned 418 I'm a teapot: the title of the HTTPError"
        want contain: "important detail here"

Description

The Deduce function will unwrap any errors that wrap a fastly.HTTPError:

cli/pkg/errors/deduce.go

Lines 24 to 33 in 92952d8

var httpError *fastly.HTTPError
if errors.As(err, &httpError) {
remediation := BugRemediation
if httpError.StatusCode == http.StatusUnauthorized {
remediation = AuthRemediation
}
return RemediationError{Inner: SimplifyFastlyError(*httpError), Remediation: remediation}
}

In some cases, like the example above and here, this can remove important error context from the output error message.

Ideas

  • I thought about creating my own RemediationError instead of using fmt.Errorf with wrapping, but that'd mean re-implementing the logic in Deduce (i.e. the Authentication error handling).
  • I think an approach would be extracting this logic into a standalone exported functions that package users can call. They could then add a prefix to the Remediation.Inner field.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions