Repository: helm/helm
File: pkg/engine/engine.go (Go)

Code before:
	}

	// The first token is "template"
	// The second token is either "filename:lineno" or "filename:lineNo:columnNo"
	location := tokens[1]

	parts := warnRegex.FindStringSubmatch(tokens[2])
	if len(parts) >= 2 {
		return fmt.Errorf("execution error at (%s): %s", string(location), parts[1])
	}
	current := err
	fileLocations := []TraceableError{}
	maxIterations := 100
	for i := 0; i < maxIterations && current != nil; i++ {
		if current == nil {
			break
		}
		if i == maxIterations-1 {
			return err
		}

		var traceable TraceableError
		if execErrFmt.MatchString(current.Error()) {
			matches := execErrFmt.FindStringSubmatch(current.Error())
			templateIndex := execErrFmt.SubexpIndex("templateName")
			templateName := matches[templateIndex]
			functionNameIndex := execErrFmt.SubexpIndex("functionName")
			functionName := matches[functionNameIndex]
			locationNameIndex := execErrFmt.SubexpIndex("location")
			locationName := matches[locationNameIndex]
			errMsgIndex := execErrFmt.SubexpIndex("errMsg")
			errMsg := matches[errMsgIndex]
			traceable = TraceableError{
				location:         templateName,
				message:          errMsg,
				executedFunction: "executing " + functionName + " at " + locationName + ":",
			}
		} else if execErrFmtWithoutTemplate.MatchString(current.Error()) {
			matches := execErrFmt.FindStringSubmatch(current.Error())
			templateIndex := execErrFmt.SubexpIndex("templateName")
			templateName := matches[templateIndex]
			errMsgIndex := execErrFmt.SubexpIndex("errMsg")
			errMsg := matches[errMsgIndex]
			traceable = TraceableError{
				location: templateName,
				message:  errMsg,
			}
		} else {
			return err
		}
		if len(fileLocations) > 0 {

Review comment (suggestion):
Personally, I would shorten the code and write:

```suggestion
			templateName := matches[execErrFmt.SubexpIndex("templateName")]
```

Code after:
	if t.executedFunction != "" {
		fmt.Fprintf(&errorString, "%s\n    ", t.executedFunction)
	}
	if t.message != "" {
		fmt.Fprintf(&errorString, "%s\n", t.message)
	}
	return errorString.String()
}

// reformatExecErrorMsg takes an error message for template rendering and formats it into a formatted
// multi-line error string
func reformatExecErrorMsg(filename string, err error) error {
	// This function matches the error message against regex's for the text/template package.
	// If the regex's can parse out details from that error message such as the line number, template it failed on,
	// and error description, then it will construct a new error that displays these details in a structured way.
	// If there are issues with parsing the error message, the err passed into the function should return instead.
	if _, isExecError := err.(template.ExecError); !isExecError {
		return err
	}

	tokens := strings.SplitN(err.Error(), ": ", 3)
	if len(tokens) != 3 {
		// This might happen if a non-templating error occurs
		return fmt.Errorf("execution error in (%s): %s", filename, err)
	}

	// The first token is "template"
	// The second token is either "filename:lineno" or "filename:lineNo:columnNo"
	location := tokens[1]

	parts := warnRegex.FindStringSubmatch(tokens[2])
	if len(parts) >= 2 {
		return fmt.Errorf("execution error at (%s): %s", string(location), parts[1])
	}
	current := err
	fileLocations := []TraceableError{}
	for current != nil {
		var traceable TraceableError
		if matches := execErrFmt.FindStringSubmatch(current.Error()); matches != nil {
			templateName := matches[execErrFmt.SubexpIndex("templateName")]
			functionName := matches[execErrFmt.SubexpIndex("functionName")]
			locationName := matches[execErrFmt.SubexpIndex("location")]
			errMsg := matches[execErrFmt.SubexpIndex("errMsg")]
			traceable = TraceableError{
				location:         templateName,
				message:          errMsg,
				executedFunction: "executing " + functionName + " at " + locationName + ":",
			}
		} else if matches := execErrFmtWithoutTemplate.FindStringSubmatch(current.Error()); matches != nil {
			templateName := matches[execErrFmt.SubexpIndex("templateName")]
			errMsg := matches[execErrFmt.SubexpIndex("errMsg")]