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

Ensure ParseError.Message is always set #411

Merged
merged 1 commit into from
May 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -542,10 +542,11 @@ func (md *MetaData) parseErr(err error) error {
k := md.context.String()
d := string(md.data)
return ParseError{
Message: err.Error(),
err: err,
LastKey: k,
Position: md.keyInfo[k].pos.withCol(d),
Line: md.keyInfo[k].pos.Line,
err: err,
input: d,
}
}
Expand Down
25 changes: 7 additions & 18 deletions error.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,16 +92,11 @@ func (p Position) withCol(tomlFile string) Position {
}

func (pe ParseError) Error() string {
msg := pe.Message
if msg == "" { // Error from errorf()
msg = pe.err.Error()
}

if pe.LastKey == "" {
return fmt.Sprintf("toml: line %d: %s", pe.Position.Line, msg)
return fmt.Sprintf("toml: line %d: %s", pe.Position.Line, pe.Message)
}
return fmt.Sprintf("toml: line %d (last key %q): %s",
pe.Position.Line, pe.LastKey, msg)
pe.Position.Line, pe.LastKey, pe.Message)
}

// ErrorWithPosition returns the error with detailed location context.
Expand All @@ -112,25 +107,19 @@ func (pe ParseError) ErrorWithPosition() string {
return pe.Error()
}

// TODO: don't show control characters as literals? This may not show up
// well everywhere.

var (
lines = strings.Split(pe.input, "\n")
b = new(strings.Builder)
)

msg := pe.Message
if msg == "" {
msg = pe.err.Error()
}

// TODO: don't show control characters as literals? This may not show up
// well everywhere.

if pe.Position.Len == 1 {
fmt.Fprintf(b, "toml: error: %s\n\nAt line %d, column %d:\n\n",
msg, pe.Position.Line, pe.Position.Col)
pe.Message, pe.Position.Line, pe.Position.Col)
} else {
fmt.Fprintf(b, "toml: error: %s\n\nAt line %d, column %d-%d:\n\n",
msg, pe.Position.Line, pe.Position.Col, pe.Position.Col+pe.Position.Len-1)
pe.Message, pe.Position.Line, pe.Position.Col, pe.Position.Col+pe.Position.Len-1)
}
if pe.Position.Line > 2 {
fmt.Fprintf(b, "% 7d | %s\n", pe.Position.Line-2, expandTab(lines[pe.Position.Line-3]))
Expand Down
4 changes: 3 additions & 1 deletion parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ func parse(data string) (p *parser, err error) {

func (p *parser) panicErr(it item, err error) {
panic(ParseError{
Message: err.Error(),
err: err,
Position: it.pos.withCol(p.lx.input),
Line: it.pos.Len,
Expand Down Expand Up @@ -123,10 +124,11 @@ func (p *parser) next() item {
if it.typ == itemError {
if it.err != nil {
panic(ParseError{
Message: it.err.Error(),
err: it.err,
Position: it.pos.withCol(p.lx.input),
Line: it.pos.Line,
LastKey: p.current(),
err: it.err,
})
}

Expand Down
Loading