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

time: unbalanced quotation in Parse error message #45391

Closed
adonovan opened this issue Apr 5, 2021 · 4 comments
Closed

time: unbalanced quotation in Parse error message #45391

adonovan opened this issue Apr 5, 2021 · 4 comments
Labels
FrozenDueToAge help wanted NeedsFix The path to resolution is known, but the work has not been done.
Milestone

Comments

@adonovan
Copy link
Member

adonovan commented Apr 5, 2021

See https://play.golang.org/p/z4GVpb8RQCY:

	// time (underlying problem)
	_, err := time.Parse(`"`+time.RFC3339+`"`, "0")
	fmt.Println(err) // parsing time "0" as ""2006-01-02T15:04:05Z07:00"": cannot parse "0" as """

	// encoding/json (how I encountered the error)
	err = json.Unmarshal([]byte(`0`), new(time.Time))
	fmt.Println(err) // parsing time "0" as ""2006-01-02T15:04:05Z07:00"": cannot parse "0" as """

The error message emitted by time.Parse contains unbalanced quotation marks, presumably because it uses its own trivial quote function to avoid a cycle-forming dependency on strconv. Ideally its quote function would be a little smarter. (Perhaps time.Parse could also avoid redundantly mentioning "parse" and "0" twice in the error message.)

@seankhliao
Copy link
Member

seankhliao commented Apr 5, 2021

The error message looks like it makes sense, it's trying to parse the input as "2006-01-02T15:04:05Z07:00" (with the double quotes) and it chokes on missing the leading ".

I guess it could use fmt.Sprintf("%q", s) instead to get "\""? Though I think that would actually make the parse error less clear as you now have to undo the quoting to get the original input.

@adonovan
Copy link
Member Author

adonovan commented Apr 5, 2021

The time package cannot use %q (strconv.Quote), but a simple improvement would be to use backticks to quote strings that contain double-quotes.

@ianlancetaylor ianlancetaylor added help wanted NeedsFix The path to resolution is known, but the work has not been done. labels Apr 5, 2021
@ianlancetaylor ianlancetaylor added this to the Backlog milestone Apr 5, 2021
@gopherbot
Copy link

Change https://golang.org/cl/307192 mentions this issue: time: use backticks to quote strings that contain double-quotes

@SimonAlling
Copy link

For the record, the fix in 1271e9a caused some of our tests that passed with Go 1.16 to fail with Go 1.17 because they were written like this:

{
	description: "message with invalid timestamp",
	message:     plainMessage(`{"Value":42,"Timestamp":"HALLOJ"}`),
	expectationOnError: errtest.And(
		errtest.HasMessageContaining(`Unknown message format: parsing time ""HALLOJ""`),
		errtest.HasMessageContaining(`cannot parse "HALLOJ""`),
	),
},

Test output:

Error message should contain

  Unknown message format: parsing time ""HALLOJ""

Actual error:

  Unknown message format: parsing time "\"HALLOJ\"" as "\"2006-01-02T15:04:05Z07:00\"": cannot parse "HALLOJ\"" as "2006"

Not meant as a criticism, just a side note that perhaps someone may find interesting. 🙂

@golang golang locked and limited conversation to collaborators Dec 22, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge help wanted NeedsFix The path to resolution is known, but the work has not been done.
Projects
None yet
Development

No branches or pull requests

5 participants