keep the friendly error message whenever possible

This commit is contained in:
CJ Horton 2023-02-15 20:56:38 -08:00
parent 727e22e762
commit 30f8b014f8

View File

@ -244,14 +244,28 @@ func (c *InitCommand) Run(args []string) int {
return 1 return 1
} }
// If the core version is OK, show any underlying config errors uncovered when initializing // If we pass the core version check, we want to show any errors from initializing the backend next,
// the backend. // which will include syntax errors from loading the configuration. However, there's a special case
diags = diags.Append(backDiags) // where we are unable to load the backend from configuration or state _and_ the configuration has
if backDiags.HasErrors() { // errors. In that case, we want to show a slightly friendlier error message for newcomers.
c.showDiagnostics(diags) showBackendDiags := back != nil || rootModEarly.Backend != nil || rootModEarly.CloudConfig != nil
return 1 if showBackendDiags {
diags = diags.Append(backDiags)
if backDiags.HasErrors() {
c.showDiagnostics(diags)
return 1
}
} else {
diags = diags.Append(earlyConfDiags)
if earlyConfDiags.HasErrors() {
c.Ui.Error(strings.TrimSpace(errInitConfigError))
c.showDiagnostics(diags)
return 1
}
} }
// If everything is ok with the core version check and backend initialization,
// show other errors from loading the full configuration tree.
diags = diags.Append(confDiags) diags = diags.Append(confDiags)
if confDiags.HasErrors() { if confDiags.HasErrors() {
c.Ui.Error(strings.TrimSpace(errInitConfigError)) c.Ui.Error(strings.TrimSpace(errInitConfigError))