Skip to content

Commit 7df585d

Browse files
agnivadeianlancetaylor
authored andcommitted
time: improve error message for LoadLocation
Currently, when a tz file was being checked inside a zoneInfo dir, a syscall.ENOENT error was being returned, which caused it to look in the zoneinfo.zip file and return an error for that case. We return a syscall.ENOENT error for the zip file case too, so that it falls through to the end of the loop and returns an uniform error for both cases. Fixes #20969 Change-Id: If1de068022ac7693caabb5cffd1c929878460140 Reviewed-on: https://go-review.googlesource.com/121877 Run-TryBot: Ian Lance Taylor <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]>
1 parent 0e21cc2 commit 7df585d

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

src/time/zoneinfo_read.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,7 @@ func loadTzinfoFromZip(zipfile, name string) ([]byte, error) {
364364
return buf, nil
365365
}
366366

367-
return nil, errors.New("cannot find " + name + " in zip file " + zipfile)
367+
return nil, syscall.ENOENT
368368
}
369369

370370
// loadTzinfoFromTzdata returns the time zone information of the time zone

src/time/zoneinfo_test.go

+11
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
package time_test
66

77
import (
8+
"errors"
89
"fmt"
910
"os"
1011
"reflect"
@@ -36,6 +37,16 @@ func TestEnvVarUsage(t *testing.T) {
3637
}
3738
}
3839

40+
func TestBadLocationErrMsg(t *testing.T) {
41+
time.ResetZoneinfoForTesting()
42+
loc := "Asia/SomethingNotExist"
43+
want := errors.New("unknown time zone " + loc)
44+
_, err := time.LoadLocation(loc)
45+
if err.Error() != want.Error() {
46+
t.Errorf("LoadLocation(%q) error = %v; want %v", loc, err, want)
47+
}
48+
}
49+
3950
func TestLoadLocationValidatesNames(t *testing.T) {
4051
time.ResetZoneinfoForTesting()
4152
const env = "ZONEINFO"

0 commit comments

Comments
 (0)