github.com/turbot/steampipe@v1.7.0-rc.0.0.20240517123944-7cef272d4458/pkg/error_helpers/diags.go (about) 1 package error_helpers 2 3 import ( 4 "errors" 5 "fmt" 6 "strings" 7 8 "github.com/turbot/go-kit/helpers" 9 "github.com/turbot/terraform-components/tfdiags" 10 ) 11 12 // DiagsToError converts tfdiags diags into an error 13 func DiagsToError(prefix string, diags tfdiags.Diagnostics) error { 14 // convert the first diag into an error 15 if !diags.HasErrors() { 16 return nil 17 } 18 errorStrings := []string{fmt.Sprintf("%s", prefix)} 19 // store list of messages (without the range) and use for deduping (we may get the same message for multiple ranges) 20 errorMessages := []string{} 21 for _, diag := range diags { 22 if diag.Severity() == tfdiags.Error { 23 errorString := fmt.Sprintf("%s", diag.Description().Summary) 24 if diag.Description().Detail != "" { 25 errorString += fmt.Sprintf(": %s", diag.Description().Detail) 26 } 27 28 if !helpers.StringSliceContains(errorMessages, errorString) { 29 errorMessages = append(errorMessages, errorString) 30 // now add in the subject and add to the output array 31 if diag.Source().Subject != nil && len(diag.Source().Subject.Filename) > 0 { 32 errorString += fmt.Sprintf("\n(%s)", diag.Source().Subject.StartString()) 33 } 34 errorStrings = append(errorStrings, errorString) 35 36 } 37 } 38 } 39 if len(errorStrings) > 0 { 40 errorString := strings.Join(errorStrings, "\n") 41 if len(errorStrings) > 1 { 42 errorString += "\n" 43 } 44 return errors.New(errorString) 45 } 46 return diags.Err() 47 }