Skip to content

Improve error handling in HTMLTemplRenderer for better debugging #41

@coderabbitai

Description

@coderabbitai

Problem

The current implementation of HTMLTemplRenderer.Instance in internal/delivery/http/renderer/templ_renderer.go has a silent failure mode that makes debugging difficult.

When the data parameter is not a templ.Component and no fallback renderer is configured, the method returns a Renderer with a nil Component. This results in a 200 HTTP response with an empty body, which is hard to debug in production.

Current Behavior

func (r *HTMLTemplRenderer) Instance(s string, d any) render.Render {
    templData, ok := d.(templ.Component)
    if !ok {
        if r.FallbackHtmlRenderer != nil {
            return r.FallbackHtmlRenderer.Instance(s, d)
        }
    }

    return &Renderer{
        Ctx:       context.Background(),
        Status:    -1,
        Component: templData, // This is nil when type assertion fails
    }
}

Expected Behavior

The renderer should fail fast and provide clear error information when:

  1. The data is not a templ.Component
  2. No fallback renderer is available

Suggested Solution

Return an error render instance or nil to surface the programming error early, instead of silently creating a renderer with a nil component.

References

Metadata

Metadata

Assignees

Labels

No labels
No labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions