Skip to content

Commit 870702b

Browse files
authored
Improve parse constructor exception message (#193)
1 parent 1e73635 commit 870702b

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

src/parse.jl

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,19 @@ end
8181
# Note: ISOZonedDateTimeFormat is defined in the module __init__ which means that this
8282
# function can not be called from within this module. TODO: Ignore linting for this line
8383
function ZonedDateTime(str::AbstractString, df::DateFormat=ISOZonedDateTimeFormat)
84-
parse(ZonedDateTime, str, df)
84+
try
85+
parse(ZonedDateTime, str, df)
86+
catch e
87+
if e isa ArgumentError
88+
rethrow(ArgumentError(
89+
"Unable to parse string \"$str\" using format $df. $(e.msg)"
90+
))
91+
else
92+
rethrow()
93+
end
94+
end
8595
end
96+
8697
function ZonedDateTime(str::AbstractString, format::AbstractString; locale::AbstractString="english")
8798
ZonedDateTime(str, DateFormat(format, locale))
8899
end

test/parse.jl

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,24 @@ end
5151
@testset "default format" begin
5252
@test default_format(ZonedDateTime) === TimeZones.ISOZonedDateTimeFormat
5353
end
54+
55+
@testset "parse constructor" begin
56+
@test isequal(
57+
ZonedDateTime("2000-01-02T03:04:05.006+0700"),
58+
ZonedDateTime(2000, 1, 2, 3, 4, 5, 6, tz"UTC+07")
59+
)
60+
@test isequal(
61+
ZonedDateTime("2018-11-01-0600", dateformat"yyyy-mm-ddzzzz"),
62+
ZonedDateTime(2018, 11, 1, tz"UTC-06"),
63+
)
64+
65+
# Validate that error message contains the original string and the format used
66+
str = "2018-11-01"
67+
try
68+
ZonedDateTime(str)
69+
catch e
70+
@test e isa ArgumentError
71+
@test occursin(str, e.msg)
72+
@test occursin(string(TimeZones.ISOZonedDateTimeFormat), e.msg)
73+
end
74+
end

0 commit comments

Comments
 (0)