Skip to content

Commit

Permalink
Improve the error message associated with a bad character error.
Browse files Browse the repository at this point in the history
A little web search showed this is a common problem for other template
tools also, but templ can now take advantage of having text sections
to give a very specific error.
  • Loading branch information
gwynforthewyn committed Jan 15, 2024
1 parent 29e01ae commit 54fe136
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion templates/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,9 +210,15 @@ func renderFromString(templateText string, templateVariableDefinitions map[strin
if strings.Contains(err.Error(), "not defined") {
reformedTemplate.WriteString(section)
continue
} else if strings.Contains(err.Error(), "bad character") {
_, file, line, _ := runtime.Caller(0)
return "", fmt.Errorf("\nThe 'bad character' error from the go template engine normally means that you have a disallowed "+
"character inside your template variable name. \nHere's what templ was working on:\n"+
"%s:%d: Failed on section:\n--- %s\n---\n Error is: %v", file, line, section, err)
} else {
_, file, line, _ := runtime.Caller(0)
return "", fmt.Errorf("%s:%d: %v", file, line, err)
return "", fmt.Errorf("%s:%d: Failed on section:\n--- %s\n---\n Error is: %v", file, line, section, err)

}
}

Expand Down

0 comments on commit 54fe136

Please sign in to comment.