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

Fix: Better error message for Time.parse! when end of input is reached #12124

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
6 changes: 6 additions & 0 deletions spec/std/time/format_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,12 @@ describe Time::Format do
Time.parse!("2017-12-01 20:15:13 +01:00", "%F %T %:z").to_s("%F %T %:z").should eq "2017-12-01 20:15:13 +01:00"
end

it "gives nice error message when end of input is reached (#12047)" do
expect_raises(Time::Format::Error, "Expected '-' but the end of the input was reached") do
Time.parse!("2021-01", "%F")
end
end

it "parses" do
parse_time("2014", "%Y").year.should eq(2014)
parse_time("19", "%C").year.should eq(1900)
Expand Down
8 changes: 8 additions & 0 deletions src/time/format/parser.cr
Original file line number Diff line number Diff line change
Expand Up @@ -502,6 +502,14 @@ struct Time::Format
end

def char(char, *alternatives)
unless @reader.has_next?
if alternatives.empty?
raise "Expected #{char.inspect} but the end of the input was reached"
else
raise "Expected one of #{({char} + alternatives).join(", ", &.inspect)} but reached the input end"
asterite marked this conversation as resolved.
Show resolved Hide resolved
end
end

unless char?(char, *alternatives)
raise "Unexpected char: #{current_char.inspect}"
end
Expand Down