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 · 2 comments

Comments

@empijei
Copy link
Contributor

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

This comment has been minimized.

Copy link

commented Mar 11, 2019

@alandonovan

This comment has been minimized.

Copy link
Contributor

commented Mar 20, 2019

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
3 participants
You can’t perform that action at this time.