Skip to content

Commit

Permalink
feat: show log message if generation error is cleared
Browse files Browse the repository at this point in the history
  • Loading branch information
a-h committed Feb 11, 2024
1 parent 5821706 commit e78dbc1
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.2.567
0.2.568
2 changes: 1 addition & 1 deletion cmd/templ/generatecmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ func (cmd Generate) Run(ctx context.Context) (err error) {
cmd.Log.Debug("Fatal error, exiting")
return err
}
cmd.Log.Error("Error received", slog.Any("error", err))
cmd.Log.Error("Error", slog.Any("error", err))
errorCount.Add(1)
}

Expand Down
19 changes: 19 additions & 0 deletions cmd/templ/generatecmd/eventhandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ func NewFSEventHandler(log *slog.Logger, dir string, devMode bool, genOpts []gen
dir: dir,
fileNameToLastModTime: make(map[string]time.Time),
fileNameToLastModTimeMutex: &sync.Mutex{},
fileNameToError: make(map[string]struct{}),
fileNameToErrorMutex: &sync.Mutex{},
hashes: make(map[string][sha256.Size]byte),
hashesMutex: &sync.Mutex{},
genOpts: genOpts,
Expand All @@ -49,6 +51,8 @@ type FSEventHandler struct {
dir string
fileNameToLastModTime map[string]time.Time
fileNameToLastModTimeMutex *sync.Mutex
fileNameToError map[string]struct{}
fileNameToErrorMutex *sync.Mutex
hashes map[string][sha256.Size]byte
hashesMutex *sync.Mutex
genOpts []generator.GenerateOpt
Expand Down Expand Up @@ -105,6 +109,7 @@ func (h *FSEventHandler) HandleEvent(ctx context.Context, event fsnotify.Event)
goUpdated, textUpdated, diag, err := h.generate(ctx, event.Name)
if err != nil {
h.Log.Error("Error generating code", slog.String("file", event.Name), slog.Any("error", err))
h.SetError(event.Name, true)
return goUpdated, textUpdated, fmt.Errorf("failed to generate code for %q: %w", event.Name, err)
}
if len(diag) > 0 {
Expand All @@ -113,11 +118,25 @@ func (h *FSEventHandler) HandleEvent(ctx context.Context, event fsnotify.Event)
}
return
}
if errorCleared, errorCount := h.SetError(event.Name, false); errorCleared {
h.Log.Info("Error cleared", slog.String("file", event.Name), slog.Int("errors", errorCount))
}
h.Log.Debug("Generated code", slog.String("file", event.Name), slog.Duration("in", time.Since(start)))

return goUpdated, textUpdated, nil
}

func (h *FSEventHandler) SetError(fileName string, hasError bool) (previouslyHadError bool, errorCount int) {
h.fileNameToErrorMutex.Lock()
defer h.fileNameToErrorMutex.Unlock()
_, previouslyHadError = h.fileNameToError[fileName]
delete(h.fileNameToError, fileName)
if hasError {
h.fileNameToError[fileName] = struct{}{}
}
return previouslyHadError, len(h.fileNameToError)
}

func (h *FSEventHandler) UpsertLastModTime(fileName string) (updated bool) {
fileInfo, err := os.Stat(fileName)
if err != nil {
Expand Down

0 comments on commit e78dbc1

Please sign in to comment.