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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wrap error from go/format as it is scanner.ErrorList #104

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

veggiemonk
Copy link

Hi,

Using jen a lot lately (great project, btw). It was difficult to debug as the error is just a string with the resulting code which can be quite hard to figure out without doing properly formatting the resulting error.

Since go/format returns a scanner.ErrorList error type, it would be nice to be able to errors.As to interact with the errors. This allow the code to be more like:

err := f.Render(customJenCode)
if err != nil {
    var se scanner.ErrorList
    if errors.As(err, &se) {
	for _, e := range se {
		fmt.Printf("position: %+v", e.Pos)
		fmt.Println("message:", e.Msg)
	}
    }
    t.Fatal(err)
}

Currently, I'm using my fork and this is the best I could come up with:

func jenDebug(err error) {
	es := err.Error()
	var se scanner.ErrorList
	if errors.As(err, &se) {
		for _, e := range se {
			// fmt.Println("line:", e.Pos.Line)
			// fmt.Println("column:", e.Pos.Column)
			// fmt.Println("message:", e.Msg)
			_, a, found := strings.Cut(es, e.Msg)
			if found {
				ss := strings.Split(a, "\n")
				for i, l := range ss {
					if i == e.Pos.Line {
						fmt.Printf("%d: %s  <---- ERROR: %s\n", i, l, e.Msg)
					} else {
						fmt.Printf("%d: %s\n", i, l)
					}
				}
				// fmt.Println("after:", a)
			}
		}
	}
}

This would help the debugging experience, not perfect by any means.
Let me know what you think.

Not an expert on the matter so I would appreciate to know how others debug generated code 馃槃

Signed-off-by: Julien Bisconti <veggiemonk@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant