From c69ceb12a0c8c51cf52ea25b6116b46cc04cfb5f Mon Sep 17 00:00:00 2001 From: Curtis Vogt Date: Thu, 28 Mar 2019 16:47:37 -0500 Subject: [PATCH] Improve parse constructor exception message (#193) --- src/parse.jl | 13 ++++++++++++- test/parse.jl | 21 +++++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/src/parse.jl b/src/parse.jl index 10abf0807..8e12c05cf 100644 --- a/src/parse.jl +++ b/src/parse.jl @@ -81,8 +81,19 @@ end # Note: ISOZonedDateTimeFormat is defined in the module __init__ which means that this # function can not be called from within this module. TODO: Ignore linting for this line function ZonedDateTime(str::AbstractString, df::DateFormat=ISOZonedDateTimeFormat) - parse(ZonedDateTime, str, df) + try + parse(ZonedDateTime, str, df) + catch e + if e isa ArgumentError + rethrow(ArgumentError( + "Unable to parse string \"$str\" using format $df. $(e.msg)" + )) + else + rethrow() + end + end end + function ZonedDateTime(str::AbstractString, format::AbstractString; locale::AbstractString="english") ZonedDateTime(str, DateFormat(format, locale)) end diff --git a/test/parse.jl b/test/parse.jl index 87604d518..7667c88a9 100644 --- a/test/parse.jl +++ b/test/parse.jl @@ -51,3 +51,24 @@ end @testset "default format" begin @test default_format(ZonedDateTime) === TimeZones.ISOZonedDateTimeFormat end + +@testset "parse constructor" begin + @test isequal( + ZonedDateTime("2000-01-02T03:04:05.006+0700"), + ZonedDateTime(2000, 1, 2, 3, 4, 5, 6, tz"UTC+07") + ) + @test isequal( + ZonedDateTime("2018-11-01-0600", dateformat"yyyy-mm-ddzzzz"), + ZonedDateTime(2018, 11, 1, tz"UTC-06"), + ) + + # Validate that error message contains the original string and the format used + str = "2018-11-01" + try + ZonedDateTime(str) + catch e + @test e isa ArgumentError + @test occursin(str, e.msg) + @test occursin(string(TimeZones.ISOZonedDateTimeFormat), e.msg) + end +end