Skip to content

Decode: improve errors on integers and strings#891

Merged
pelletier merged 3 commits intov2from
ints-err-loc
Aug 28, 2023
Merged

Decode: improve errors on integers and strings#891
pelletier merged 3 commits intov2from
ints-err-loc

Conversation

@pelletier
Copy link
Copy Markdown
Owner

Add struct field location and context to errors reported when trying to unmarshal a string or integer into an inappropriate Go type.

Given this program:

type Server struct {
        Path string
        Port int
}

type Cfg struct {
        Server Server
}

var cfg Cfg
data := `[server]
path = "/my/path"
port = "bad"`

file := strings.NewReader(data)
err := toml.NewDecoder(file).Decode(&cfg)
fmt.Println(err)
fmt.Println(err.(*toml.DecodeError).String()) // crashes on main

Output before patch:

toml: cannot store TOML string into a Go int

Output after patch:

toml: cannot decode TOML string into struct field toml_test.Server.Port of type int
1| [server]
2| path = "/my/path"
3| port = "bad"
 |        ~~~~~ cannot decode TOML string into struct field toml_test.Server.Port of type int

Based on #890

@pelletier pelletier added the bug Issues describing a bug in go-toml. label Aug 28, 2023
Base automatically changed from fix-raw-integers to v2 August 28, 2023 15:02
@pelletier pelletier changed the title Decode: add location to errors of ints and strings Decode: improve errors on integers and strings Aug 28, 2023
@pelletier pelletier merged commit 4835627 into v2 Aug 28, 2023
@pelletier pelletier deleted the ints-err-loc branch August 28, 2023 15:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Issues describing a bug in go-toml.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant