From 63adac25897f295ef4493e060d917650f03ebd3b Mon Sep 17 00:00:00 2001 From: xuri Date: Fri, 20 May 2022 20:46:29 +0800 Subject: [PATCH] make workbook open filed exception message clear - New exported constant `ErrWorkbookPassword` - Rename exported constant `ErrWorkbookExt` to `ErrWorkbookFileFormat` --- crypt_test.go | 3 +++ errors.go | 9 ++++++--- excelize.go | 8 +++++--- excelize_test.go | 9 +++++---- file.go | 2 +- 5 files changed, 20 insertions(+), 11 deletions(-) diff --git a/crypt_test.go b/crypt_test.go index 80f6cc4..d095176 100644 --- a/crypt_test.go +++ b/crypt_test.go @@ -19,6 +19,9 @@ import ( ) func TestEncrypt(t *testing.T) { + _, err := OpenFile(filepath.Join("test", "encryptSHA1.xlsx"), Options{Password: "passwd"}) + assert.EqualError(t, err, ErrWorkbookPassword.Error()) + f, err := OpenFile(filepath.Join("test", "encryptSHA1.xlsx"), Options{Password: "password"}) assert.NoError(t, err) assert.EqualError(t, f.SaveAs(filepath.Join("test", "BadEncrypt.xlsx"), Options{Password: "password"}), ErrEncrypt.Error()) diff --git a/errors.go b/errors.go index 64fc711..61a7456 100644 --- a/errors.go +++ b/errors.go @@ -106,9 +106,9 @@ var ( // ErrImgExt defined the error message on receive an unsupported image // extension. ErrImgExt = errors.New("unsupported image extension") - // ErrWorkbookExt defined the error message on receive an unsupported - // workbook extension. - ErrWorkbookExt = errors.New("unsupported workbook extension") + // ErrWorkbookFileFormat defined the error message on receive an + // unsupported workbook file format. + ErrWorkbookFileFormat = errors.New("unsupported workbook file format") // ErrMaxFilePathLength defined the error message on receive the file path // length overflow. ErrMaxFilePathLength = errors.New("file path length exceeds maximum limit") @@ -191,4 +191,7 @@ var ( // ErrSparklineStyle defined the error message on receive the invalid // sparkline Style parameters. ErrSparklineStyle = errors.New("parameter 'Style' must between 0-35") + // ErrWorkbookPassword defined the error message on receiving the incorrect + // workbook password. + ErrWorkbookPassword = errors.New("the supplied open workbook password is not correct") ) diff --git a/excelize.go b/excelize.go index d78d2b1..0e2f440 100644 --- a/excelize.go +++ b/excelize.go @@ -158,13 +158,15 @@ func OpenReader(r io.Reader, opt ...Options) (*File, error) { return nil, ErrOptionsUnzipSizeLimit } if bytes.Contains(b, oleIdentifier) { - b, err = Decrypt(b, f.options) - if err != nil { - return nil, fmt.Errorf("decrypted file failed") + if b, err = Decrypt(b, f.options); err != nil { + return nil, ErrWorkbookFileFormat } } zr, err := zip.NewReader(bytes.NewReader(b), int64(len(b))) if err != nil { + if len(f.options.Password) > 0 { + return nil, ErrWorkbookPassword + } return nil, err } file, sheetCount, err := f.ReadZipReader(zr) diff --git a/excelize_test.go b/excelize_test.go index 27badc6..f1b9903 100644 --- a/excelize_test.go +++ b/excelize_test.go @@ -1,6 +1,7 @@ package excelize import ( + "archive/zip" "bytes" "compress/gzip" "encoding/xml" @@ -179,7 +180,7 @@ func TestSaveFile(t *testing.T) { if !assert.NoError(t, err) { t.FailNow() } - assert.EqualError(t, f.SaveAs(filepath.Join("test", "TestSaveFile.xlsb")), ErrWorkbookExt.Error()) + assert.EqualError(t, f.SaveAs(filepath.Join("test", "TestSaveFile.xlsb")), ErrWorkbookFileFormat.Error()) for _, ext := range []string{".xlam", ".xlsm", ".xlsx", ".xltm", ".xltx"} { assert.NoError(t, f.SaveAs(filepath.Join("test", fmt.Sprintf("TestSaveFile%s", ext)))) } @@ -207,9 +208,9 @@ func TestCharsetTranscoder(t *testing.T) { func TestOpenReader(t *testing.T) { _, err := OpenReader(strings.NewReader("")) - assert.EqualError(t, err, "zip: not a valid zip file") + assert.EqualError(t, err, zip.ErrFormat.Error()) _, err = OpenReader(bytes.NewReader(oleIdentifier), Options{Password: "password", UnzipXMLSizeLimit: UnzipSizeLimit + 1}) - assert.EqualError(t, err, "decrypted file failed") + assert.EqualError(t, err, ErrWorkbookFileFormat.Error()) // Test open spreadsheet with unzip size limit. _, err = OpenFile(filepath.Join("test", "Book1.xlsx"), Options{UnzipSizeLimit: 100}) @@ -261,7 +262,7 @@ func TestOpenReader(t *testing.T) { 0x05, 0x06, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x41, 0x00, 0x00, 0x00, 0x5d, 0x00, 0x00, 0x00, 0x00, 0x00, })) - assert.EqualError(t, err, "zip: unsupported compression algorithm") + assert.EqualError(t, err, zip.ErrAlgorithm.Error()) } func TestBrokenFile(t *testing.T) { diff --git a/file.go b/file.go index 1d3360e..ecdadf4 100644 --- a/file.go +++ b/file.go @@ -78,7 +78,7 @@ func (f *File) SaveAs(name string, opt ...Options) error { ".xltx": ContentTypeTemplate, }[filepath.Ext(f.Path)] if !ok { - return ErrWorkbookExt + return ErrWorkbookFileFormat } f.setContentTypePartProjectExtensions(contentType) file, err := os.OpenFile(filepath.Clean(name), os.O_WRONLY|os.O_TRUNC|os.O_CREATE, os.ModePerm) -- GitLab