github.com/fastly/cli@v1.7.2-0.20240304164155-9d0f1d77c3bf/pkg/errors/remediation_error.go (about) 1 package errors 2 3 import ( 4 "fmt" 5 "io" 6 "strings" 7 8 "github.com/fastly/cli/pkg/env" 9 "github.com/fastly/cli/pkg/text" 10 ) 11 12 // RemediationError wraps a normal error with a suggested remediation. 13 type RemediationError struct { 14 // Prefix is a custom message displayed without modification. 15 Prefix string 16 // Inner is the root error. 17 Inner error 18 // Remediation provides more context and helpful references. 19 Remediation string 20 } 21 22 // Unwrap returns the inner error. 23 func (re RemediationError) Unwrap() error { 24 return re.Inner 25 } 26 27 // Error prints the inner error string without any remediation suggestion. 28 func (re RemediationError) Error() string { 29 if re.Inner == nil { 30 return "" 31 } 32 return re.Inner.Error() 33 } 34 35 // Print the error to the io.Writer for human consumption. If a prefix is 36 // provided, it will be written without modification. The inner error is always 37 // printed via text.Output with an "Error: " prefix and a "." suffix. If a 38 // remediation is provided, it's printed via text.Output. 39 func (re RemediationError) Print(w io.Writer) { 40 if re.Prefix != "" { 41 fmt.Fprintf(w, "%s\n\n", strings.TrimRight(re.Prefix, "\r\n")) 42 } 43 if re.Inner != nil { 44 text.Error(w, "%s.\n\n", re.Inner.Error()) // single "\n" ensured by text.Error 45 } 46 if re.Remediation != "" { 47 fmt.Fprintf(w, "%s\n", strings.TrimRight(re.Remediation, "\r\n")) 48 } 49 } 50 51 // FormatTemplate represents a generic error message prefix. 52 var FormatTemplate = "To fix this error, run the following command:\n\n\t$ %s" 53 54 // AuthRemediation suggests checking the provided --token. 55 var AuthRemediation = fmt.Sprintf(strings.Join([]string{ 56 "This error may be caused by a missing, incorrect, or expired Fastly API token.", 57 "Check that you're supplying a valid token, either via --token,", 58 "through the environment variable %s, or through the config file via `fastly profile`.", 59 "Verify that the token is still valid via `fastly whoami`.", 60 }, " "), env.APIToken) 61 62 // NetworkRemediation suggests, somewhat unhelpfully, to try again later. 63 var NetworkRemediation = strings.Join([]string{ 64 "This error may be caused by transient network issues.", 65 "Please verify your network connection and DNS configuration, and try again.", 66 }, " ") 67 68 // HostRemediation suggests there might be an issue with the local host. 69 var HostRemediation = strings.Join([]string{ 70 "This error may be caused by a problem with your host environment, for example", 71 "too-restrictive file permissions, files that already exist, or a full disk.", 72 }, " ") 73 74 // BugRemediation suggests filing a bug on the GitHub repo. It's good to include 75 // as the final suggested remediation in many errors. 76 var BugRemediation = strings.Join([]string{ 77 "If you believe this error is the result of a bug, please file an issue:", 78 "https://github.com/fastly/cli/issues/new?labels=bug&template=bug_report.md", 79 }, " ") 80 81 // ConfigRemediation informs the user that an error with loading the config 82 // isn't a breaking error and the CLI can still be used. 83 var ConfigRemediation = strings.Join([]string{ 84 "There is a fallback version of the configuration provided with the CLI install", 85 "(run `fastly config` to view the config) which enables the CLI to continue to be usable even though the config couldn't be updated.", 86 }, " ") 87 88 // ServiceIDRemediation suggests provide a service ID via --service-id flag or 89 // fastly.toml. 90 var ServiceIDRemediation = strings.Join([]string{ 91 "Please provide one via the --service-id or --service-name flag, or by setting the FASTLY_SERVICE_ID environment variable, or within your fastly.toml", 92 }, " ") 93 94 // CustomerIDRemediation suggests provide a customer ID via --customer-id flag 95 // or via environment variable. 96 var CustomerIDRemediation = strings.Join([]string{ 97 "Please provide one via the --customer-id flag, or by setting the FASTLY_CUSTOMER_ID environment variable", 98 }, " ") 99 100 // ExistingDirRemediation suggests moving to another directory and retrying. 101 var ExistingDirRemediation = strings.Join([]string{ 102 "Please create a new directory and initialize a new project using:", 103 "`fastly compute init`.", 104 }, " ") 105 106 // AutoCloneRemediation suggests provide an --autoclone flag. 107 var AutoCloneRemediation = strings.Join([]string{ 108 "Repeat the command with the --autoclone flag to allow the version to be cloned", 109 }, " ") 110 111 // IDRemediation suggests an ID via --id flag should be provided. 112 var IDRemediation = strings.Join([]string{ 113 "Please provide one via the --id flag", 114 }, " ") 115 116 // PackageSizeRemediation suggests checking the resources documentation for the 117 // current package size limit. 118 var PackageSizeRemediation = strings.Join([]string{ 119 "Please check our Compute resource limits:", 120 "https://developer.fastly.com/learning/compute/#limitations-and-constraints", 121 }, " ") 122 123 // UnrecognisedManifestVersionRemediation suggests steps to resolve an issue 124 // where the project contains a manifest_version that is larger than what the 125 // current CLI version supports. 126 var UnrecognisedManifestVersionRemediation = strings.Join([]string{ 127 "Please try updating the installed CLI version using: `fastly update`.", 128 "See also https://developer.fastly.com/reference/fastly-toml/ to check your fastly.toml manifest is up-to-date with the latest data model.", 129 BugRemediation, 130 }, " ") 131 132 // ComputeInitRemediation suggests re-running `compute init` to resolve 133 // manifest issue. 134 var ComputeInitRemediation = strings.Join([]string{ 135 "Run `fastly compute init` to ensure a correctly configured manifest.", 136 "See more at https://developer.fastly.com/reference/fastly-toml/", 137 }, " ") 138 139 // ComputeServeRemediation suggests re-running `compute serve` with one of the 140 // incompatible flags removed. 141 var ComputeServeRemediation = strings.Join([]string{ 142 "The --watch flag enables hot reloading of your project to support a faster feedback loop during local development, and subsequently conflicts with the --skip-build flag which avoids rebuilding your project altogether.", 143 "Remove one of the flags based on the outcome you require.", 144 }, " ") 145 146 // ComputeBuildRemediation suggests configuring a `[scripts.build]` setting in 147 // the fastly.toml manifest. 148 var ComputeBuildRemediation = strings.Join([]string{ 149 "Add a [scripts] section with `build = \"%s\"`.", 150 "See more at https://developer.fastly.com/reference/fastly-toml/", 151 }, " ") 152 153 // ComputeTrialRemediation suggests contacting customer manager to enable the 154 // free trial feature flag. 155 var ComputeTrialRemediation = "For more help with this error see fastly.help/cli/ecp-feature" 156 157 // ProfileRemediation suggests no profiles exist. 158 var ProfileRemediation = "Run `fastly profile create <NAME>` to create a profile, or `fastly profile list` to view available profiles (at least one profile should be set as 'default')." 159 160 // InvalidStaticConfigRemediation indicates an unexpected error occurred when 161 // deserialising the CLI's internal configuration. 162 var InvalidStaticConfigRemediation = strings.Join([]string{ 163 "The Fastly CLI attempted to parse an internal configuration file but failed.", 164 "Run `fastly update` to upgrade your current CLI version.", 165 "If this does not resolve the issue, then please file an issue:", 166 "https://github.com/fastly/cli/issues/new?labels=bug&template=bug_report.md", 167 }, " ")