Changed: DB Params
This commit is contained in:
5
templ/generator/test-attribute-errors/expected.html
Normal file
5
templ/generator/test-attribute-errors/expected.html
Normal file
@@ -0,0 +1,5 @@
|
||||
<ul>
|
||||
<li data-attr="raw"></li>
|
||||
<li data-attr="OK"></li>
|
||||
<li data-attr="OK2"></li>
|
||||
</ul>
|
57
templ/generator/test-attribute-errors/render_test.go
Normal file
57
templ/generator/test-attribute-errors/render_test.go
Normal file
@@ -0,0 +1,57 @@
|
||||
package testattrerrs
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
_ "embed"
|
||||
"errors"
|
||||
"testing"
|
||||
|
||||
"github.com/a-h/templ"
|
||||
"github.com/a-h/templ/generator/htmldiff"
|
||||
)
|
||||
|
||||
//go:embed expected.html
|
||||
var expected string
|
||||
|
||||
func Test(t *testing.T) {
|
||||
t.Run("can render without error", func(t *testing.T) {
|
||||
component := TestComponent(nil)
|
||||
|
||||
_, err := htmldiff.Diff(component, expected)
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
})
|
||||
t.Run("attribute expressions can return errors", func(t *testing.T) {
|
||||
errSomethingBad := errors.New("bad error")
|
||||
|
||||
err := TestComponent(errSomethingBad).Render(context.Background(), &bytes.Buffer{})
|
||||
if err == nil {
|
||||
t.Fatalf("expected error, but got nil")
|
||||
}
|
||||
|
||||
t.Run("the errors are templ errors", func(t *testing.T) {
|
||||
var templateErr templ.Error
|
||||
if !errors.As(err, &templateErr) {
|
||||
t.Fatalf("expected error to be templ.Error, but got %T", err)
|
||||
}
|
||||
if templateErr.FileName != `generator/test-attribute-errors/template.templ` {
|
||||
t.Errorf("expected error in `generator/test-attribute-errors/template.templ`, but got %v", templateErr.FileName)
|
||||
}
|
||||
if templateErr.Line != 18 {
|
||||
t.Errorf("expected error on line 18, but got %v", templateErr.Line)
|
||||
}
|
||||
if templateErr.Col != 36 {
|
||||
t.Errorf("expected error on column 26, but got %v", templateErr.Col)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("the underlying error can be unwrapped", func(t *testing.T) {
|
||||
if !errors.Is(err, errSomethingBad) {
|
||||
t.Errorf("expected error: %v, but got %v", errSomethingBad, err)
|
||||
}
|
||||
})
|
||||
|
||||
})
|
||||
}
|
20
templ/generator/test-attribute-errors/template.templ
Normal file
20
templ/generator/test-attribute-errors/template.templ
Normal file
@@ -0,0 +1,20 @@
|
||||
package testattrerrs
|
||||
|
||||
func funcWithNoError() (s string) {
|
||||
return "OK"
|
||||
}
|
||||
|
||||
func funcWithError(in error) (s string, err error) {
|
||||
if in != nil {
|
||||
return "", in
|
||||
}
|
||||
return "OK2", nil
|
||||
}
|
||||
|
||||
templ TestComponent(err error) {
|
||||
<ul>
|
||||
<li data-attr={ "raw" }></li>
|
||||
<li data-attr={ funcWithNoError() }></li>
|
||||
<li data-attr={ funcWithError(err) }></li>
|
||||
</ul>
|
||||
}
|
90
templ/generator/test-attribute-errors/template_templ.go
Normal file
90
templ/generator/test-attribute-errors/template_templ.go
Normal file
@@ -0,0 +1,90 @@
|
||||
// Code generated by templ - DO NOT EDIT.
|
||||
|
||||
// templ: version: v0.3.833
|
||||
package testattrerrs
|
||||
|
||||
//lint:file-ignore SA4006 This context is only used if a nested component is present.
|
||||
|
||||
import "github.com/a-h/templ"
|
||||
import templruntime "github.com/a-h/templ/runtime"
|
||||
|
||||
func funcWithNoError() (s string) {
|
||||
return "OK"
|
||||
}
|
||||
|
||||
func funcWithError(in error) (s string, err error) {
|
||||
if in != nil {
|
||||
return "", in
|
||||
}
|
||||
return "OK2", nil
|
||||
}
|
||||
|
||||
func TestComponent(err error) templ.Component {
|
||||
return templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) {
|
||||
templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context
|
||||
if templ_7745c5c3_CtxErr := ctx.Err(); templ_7745c5c3_CtxErr != nil {
|
||||
return templ_7745c5c3_CtxErr
|
||||
}
|
||||
templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W)
|
||||
if !templ_7745c5c3_IsBuffer {
|
||||
defer func() {
|
||||
templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer)
|
||||
if templ_7745c5c3_Err == nil {
|
||||
templ_7745c5c3_Err = templ_7745c5c3_BufErr
|
||||
}
|
||||
}()
|
||||
}
|
||||
ctx = templ.InitializeContext(ctx)
|
||||
templ_7745c5c3_Var1 := templ.GetChildren(ctx)
|
||||
if templ_7745c5c3_Var1 == nil {
|
||||
templ_7745c5c3_Var1 = templ.NopComponent
|
||||
}
|
||||
ctx = templ.ClearChildren(ctx)
|
||||
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 1, "<ul><li data-attr=\"")
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
var templ_7745c5c3_Var2 string
|
||||
templ_7745c5c3_Var2, templ_7745c5c3_Err = templ.JoinStringErrs("raw")
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ.Error{Err: templ_7745c5c3_Err, FileName: `templ/generator/test-attribute-errors/template.templ`, Line: 16, Col: 23}
|
||||
}
|
||||
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var2))
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 2, "\"></li><li data-attr=\"")
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
var templ_7745c5c3_Var3 string
|
||||
templ_7745c5c3_Var3, templ_7745c5c3_Err = templ.JoinStringErrs(funcWithNoError())
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ.Error{Err: templ_7745c5c3_Err, FileName: `templ/generator/test-attribute-errors/template.templ`, Line: 17, Col: 35}
|
||||
}
|
||||
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var3))
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 3, "\"></li><li data-attr=\"")
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
var templ_7745c5c3_Var4 string
|
||||
templ_7745c5c3_Var4, templ_7745c5c3_Err = templ.JoinStringErrs(funcWithError(err))
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ.Error{Err: templ_7745c5c3_Err, FileName: `templ/generator/test-attribute-errors/template.templ`, Line: 18, Col: 36}
|
||||
}
|
||||
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var4))
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 4, "\"></li></ul>")
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
var _ = templruntime.GeneratedTemplate
|
Reference in New Issue
Block a user