Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

html/template: improve context representation for user-facing error messages #30635

Open
empijei opened this issue Mar 6, 2019 · 5 comments
Open
Labels
NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone

Comments

@empijei
Copy link
Contributor

empijei commented Mar 6, 2019

What version of Go are you using (go version)?

$ go version
go version go1.12 linux/amd64

Does this issue reproduce with the latest release?

Yes

What operating system and processor architecture are you using (go env)?

go env Output
$ go env
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/clap/.cache/go-build"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/home/clap/go"
GOPROXY=""
GORACE=""
GOROOT="/home/clap/goroot"
GOTMPDIR=""
GOTOOLDIR="/home/clap/goroot/pkg/tool/linux_amd64"
GCCGO="gccgo"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD=""
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build564528553=/tmp/go-build -gno-record-gcc-switches"

What did you do?

https://play.golang.org/p/t7FfQjWKEB6

package main

import (
	"html/template"
	"os"
)

func main() {
	t := template.Must(template.New("test").Parse(`<style>{{.}}`))
	err := t.Execute(os.Stdout, "test")
	if err != nil {
		panic(err)
	}
}

What did you expect to see?

An error message stating that there is an unclosed <style> tag, or some similarly helpful message

What did you see instead?

html/template:test: ends in a non-text context: {stateCSS delimNone urlPartNone jsCtxRegexp attrNone elementStyle <nil>}

Proposed fix

This is due to the fact that escape.go uses the "%v" representation of context to format the returned error.

It would probably be better to show an error based on c.state value, providing a String representation of states (it's not necessary to have it too fine-grained, returning one of [CSS HTML JS RCDATA Text URL Error] would probably suffice to debug the issue).

@julieqiu julieqiu changed the title html/template: Improve context representation for user-facing error messages. html/template: improve context representation for user-facing error messages Mar 11, 2019
@julieqiu julieqiu added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Mar 11, 2019
@julieqiu
Copy link
Member

cc: @mikesamuel @alandonovan

@alandonovan
Copy link
Contributor

I'm not familiar with this package. Mike, any thoughts?

@mariojackson
Copy link

I just had the same problem and it took me a while to find the error in the correct child template at the correct line.

@candlerb
Copy link

Another example: writing <script src="foo" /> instead of <script src="foo"></script> gives the same cryptic error message.

Suggestion: something along the lines of element <script> opened on line XXX was not closed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Projects
None yet
Development

No branches or pull requests

7 participants
@candlerb @empijei @julieqiu @alandonovan @seankhliao @mariojackson and others