Skip to content

Commit

Permalink
fix: add ability to log broken templates in debug mode
Browse files Browse the repository at this point in the history
  • Loading branch information
TristanSpeakEasy committed Feb 13, 2024
1 parent 4f4c062 commit dd47a15
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
7 changes: 7 additions & 0 deletions engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,13 @@ func WithJSFiles(files map[string]string) Opt {
}
}

// WithDebug enables debug mode for the engine, which will log additional information when errors occur.
func WithDebug() Opt {
return func(e *Engine) {
e.templator.Debug = true
}
}

// Engine provides the templating engine.
type Engine struct {
searchLocations []string
Expand Down
4 changes: 4 additions & 0 deletions internal/template/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ type Templator struct {
WriteFunc WriteFunc
ReadFunc ReadFunc
TmplFuncs map[string]any
Debug bool
contextData any
globalComputed goja.Value
}
Expand Down Expand Up @@ -260,6 +261,9 @@ func getRecursiveComputedContext(vm VM) goja.Value {
func (t *Templator) execTemplate(name string, tmplContent string, data any, replacedLines int) (string, error) {
tmp, err := template.New(name).Funcs(t.TmplFuncs).Parse(tmplContent)
if err != nil {
if t.Debug {
fmt.Println(tmplContent)

Check failure on line 265 in internal/template/template.go

View workflow job for this annotation

GitHub Actions / lint

use of `fmt.Println` forbidden by pattern `^(fmt\.Print(|f|ln)|print|println)$` (forbidigo)
}
return "", fmt.Errorf("failed to parse template: %w", err)
}

Expand Down

0 comments on commit dd47a15

Please sign in to comment.