Skip to content

Commit 5e67390

Browse files
committed
fix(parser): Fix error handling when dealing with empty layout or format
1 parent 0684f5a commit 5e67390

File tree

2 files changed

+10
-10
lines changed

2 files changed

+10
-10
lines changed

parser.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ func ParseByLayouts(value string, layouts []string, timezone ...string) Carbon {
133133
return Carbon{isEmpty: true}
134134
}
135135
if len(layouts) == 0 {
136-
return Parse(value, timezone...)
136+
return Carbon{Error: ErrEmptyLayout()}
137137
}
138138
var (
139139
tz string
@@ -169,7 +169,7 @@ func ParseByFormats(value string, formats []string, timezone ...string) Carbon {
169169
return Carbon{isEmpty: true}
170170
}
171171
if len(formats) == 0 {
172-
return Parse(value, timezone...)
172+
return Carbon{Error: ErrEmptyFormat()}
173173
}
174174
var (
175175
tz string

parser_test.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -156,8 +156,8 @@ func TestParseByLayouts(t *testing.T) {
156156
})
157157

158158
t.Run("empty layouts", func(t *testing.T) {
159-
assert.Equal(t, "2020-08-05 13:14:15 +0000 UTC", ParseByLayouts("2020-08-05 13:14:15", []string{}).ToString())
160-
assert.Equal(t, "2006-01-02 15:04:05", ParseByLayouts("2020-08-05 13:14:15", []string{}).CurrentLayout())
159+
assert.Empty(t, ParseByLayouts("2020-08-05 13:14:15", []string{}).ToString())
160+
assert.Empty(t, ParseByLayouts("2020-08-05 13:14:15", []string{}).CurrentLayout())
161161
})
162162

163163
t.Run("invalid timezone", func(t *testing.T) {
@@ -273,8 +273,8 @@ func TestParseByFormats(t *testing.T) {
273273
})
274274

275275
t.Run("empty layouts", func(t *testing.T) {
276-
assert.Equal(t, "2020-08-05 13:14:15 +0000 UTC", ParseByFormats("2020-08-05 13:14:15", []string{}).ToString())
277-
assert.Equal(t, "2006-01-02 15:04:05", ParseByFormats("2020-08-05 13:14:15", []string{}).CurrentLayout())
276+
assert.Empty(t, ParseByFormats("2020-08-05 13:14:15", []string{}).ToString())
277+
assert.Empty(t, ParseByFormats("2020-08-05 13:14:15", []string{}).CurrentLayout())
278278
})
279279

280280
t.Run("invalid timezone", func(t *testing.T) {
@@ -444,8 +444,8 @@ func TestParseWithLayouts(t *testing.T) {
444444
})
445445

446446
t.Run("empty layouts", func(t *testing.T) {
447-
assert.Equal(t, "2020-08-05 13:14:15 +0000 UTC", ParseWithLayouts("2020-08-05 13:14:15", []string{}).ToString())
448-
assert.Equal(t, "2006-01-02 15:04:05", ParseWithLayouts("2020-08-05 13:14:15", []string{}).CurrentLayout())
447+
assert.Empty(t, ParseWithLayouts("2020-08-05 13:14:15", []string{}).ToString())
448+
assert.Empty(t, ParseWithLayouts("2020-08-05 13:14:15", []string{}).CurrentLayout())
449449
})
450450

451451
t.Run("invalid timezone", func(t *testing.T) {
@@ -483,8 +483,8 @@ func TestParseWithFormats(t *testing.T) {
483483
})
484484

485485
t.Run("empty layouts", func(t *testing.T) {
486-
assert.Equal(t, "2020-08-05 13:14:15 +0000 UTC", ParseWithFormats("2020-08-05 13:14:15", []string{}).ToString())
487-
assert.Equal(t, "2006-01-02 15:04:05", ParseWithFormats("2020-08-05 13:14:15", []string{}).CurrentLayout())
486+
assert.Empty(t, ParseWithFormats("2020-08-05 13:14:15", []string{}).ToString())
487+
assert.Empty(t, ParseWithFormats("2020-08-05 13:14:15", []string{}).CurrentLayout())
488488
})
489489

490490
t.Run("invalid timezone", func(t *testing.T) {

0 commit comments

Comments
 (0)