已验证 提交 63adac25 编写于 作者: xurime's avatar xurime

make workbook open filed exception message clear

- New exported constant `ErrWorkbookPassword`
- Rename exported constant `ErrWorkbookExt` to `ErrWorkbookFileFormat`
上级 8f16a767
......@@ -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())
......
......@@ -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")
)
......@@ -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)
......
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) {
......
......@@ -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)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册