From 41f37aaec7ab625487a2be16a468b2a77e4a67d2 Mon Sep 17 00:00:00 2001 From: Integralist Date: Wed, 19 Apr 2023 09:49:12 +0100 Subject: [PATCH] fix(manifest): re-raise remediation error to avoid go-toml wrapping issue --- pkg/errors/errors.go | 2 +- pkg/errors/remediation_error.go | 10 ++++++---- pkg/manifest/file.go | 13 ++++++++++++- pkg/manifest/manifest_test.go | 12 ++++++------ 4 files changed, 25 insertions(+), 12 deletions(-) diff --git a/pkg/errors/errors.go b/pkg/errors/errors.go index 61c14ad74..2fed04e6a 100644 --- a/pkg/errors/errors.go +++ b/pkg/errors/errors.go @@ -54,7 +54,7 @@ var ErrMissingManifestVersion = RemediationError{ // version has been specified. var ErrUnrecognisedManifestVersion = RemediationError{ Inner: fmt.Errorf("unrecognised manifest_version found in the fastly.toml"), - Remediation: CLIUpdateRemediation, + Remediation: UnrecognisedManifestVersionRemediation, } // ErrIncompatibleManifestVersion means the manifest_version defined is no diff --git a/pkg/errors/remediation_error.go b/pkg/errors/remediation_error.go index 3d530886e..f3321a0fd 100644 --- a/pkg/errors/remediation_error.go +++ b/pkg/errors/remediation_error.go @@ -123,10 +123,12 @@ var PackageSizeRemediation = strings.Join([]string{ "https://developer.fastly.com/learning/compute/#limitations-and-constraints", }, " ") -// CLIUpdateRemediation suggests updating the installed CLI version. -var CLIUpdateRemediation = strings.Join([]string{ - "Please try updating the installed CLI version using:", - "`fastly update`.", +// UnrecognisedManifestVersionRemediation suggests steps to resolve an issue +// where the project contains a manifest_version that is larger than what the +// current CLI version supports. +var UnrecognisedManifestVersionRemediation = strings.Join([]string{ + "Please try updating the installed CLI version using: `fastly update`.", + "See also https://developer.fastly.com/reference/fastly-toml/ to check your fastly.toml manifest is up-to-date with the latest data model.", BugRemediation, }, " ") diff --git a/pkg/manifest/file.go b/pkg/manifest/file.go index cb601733d..fdbafdb5f 100644 --- a/pkg/manifest/file.go +++ b/pkg/manifest/file.go @@ -4,10 +4,12 @@ import ( "fmt" "io" "os" + "strings" + + toml "github.com/pelletier/go-toml" fsterr "github.com/fastly/cli/pkg/errors" "github.com/fastly/cli/pkg/text" - toml "github.com/pelletier/go-toml" ) // File represents all of the configuration parameters in the fastly.toml @@ -78,6 +80,15 @@ func (f *File) Read(path string) (err error) { err = tree.Unmarshal(f) if err != nil { + // IMPORTANT: go-toml consumes our error type within its own. + // + // This means we need to manually parse the return error to see if it + // contains our specific error message. If we don't do this, then the + // remediation information we pass back will be lost and a generic 'bug' + // remediation (which is set by logic in main.go) is used instead. + if strings.Contains(err.Error(), fsterr.ErrUnrecognisedManifestVersion.Inner.Error()) { + err = fsterr.ErrUnrecognisedManifestVersion + } f.logErr(err) return err } diff --git a/pkg/manifest/manifest_test.go b/pkg/manifest/manifest_test.go index 88d035e3d..47e4edfdd 100644 --- a/pkg/manifest/manifest_test.go +++ b/pkg/manifest/manifest_test.go @@ -7,13 +7,14 @@ import ( "strings" "testing" + "github.com/google/go-cmp/cmp" + toml "github.com/pelletier/go-toml" + "github.com/fastly/cli/pkg/env" fsterr "github.com/fastly/cli/pkg/errors" "github.com/fastly/cli/pkg/manifest" "github.com/fastly/cli/pkg/testutil" "github.com/fastly/cli/pkg/threadsafe" - "github.com/google/go-cmp/cmp" - toml "github.com/pelletier/go-toml" ) func TestManifest(t *testing.T) { @@ -101,6 +102,9 @@ func TestManifest(t *testing.T) { err = m.Read(path) + output := stdout.String() + t.Log(output) + // If we expect an invalid config, then assert we get the right error. if !tc.valid { testutil.AssertErrorContains(t, err, tc.expectedError.Error()) @@ -117,10 +121,6 @@ func TestManifest(t *testing.T) { t.Fatalf("manifest_version '%d' doesn't match latest '%d'", m.ManifestVersion, manifest.ManifestLatestVersion) } - output := stdout.String() - - t.Log(output) - if tc.expectedOutput != "" && !strings.Contains(output, tc.expectedOutput) { t.Fatalf("got: %s, want: %s", output, tc.expectedOutput) }