Skip to content

Commit

Permalink
fix nil pointer for xlsx sharedStrings; add log
Browse files Browse the repository at this point in the history
  • Loading branch information
ysj committed Dec 11, 2023
1 parent ef76e71 commit 4a602c8
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 6 deletions.
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.0.2
1.0.3
16 changes: 16 additions & 0 deletions docxtotext/docxtotext.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@ func (dp *DocxParser) ExtractTexts() (string, error) {
// - error: an error if any.
func (dp *DocxParser) extractDocument() (*strings.Builder, error) {
if dp.documentFile == nil {
dp.logWarn(types.ErrNoDocument)
return new(strings.Builder), nil
}

Expand Down Expand Up @@ -342,6 +343,11 @@ NEXT:
// - *strings.Builder: a strings.Builder containing the extracted comments text.
// - error: an error if any.
func (dp *DocxParser) extractComments() (*strings.Builder, error) {
if dp.commentsFile == nil {
dp.logWarn(types.ErrNoComments)
return new(strings.Builder), nil
}

rc, err := dp.commentsFile.Open()
if err != nil {
return nil, err
Expand Down Expand Up @@ -388,6 +394,11 @@ NEXT:
// - *strings.Builder: a strings.Builder containing the extracted endnotes text.
// - error: an error if any.
func (dp *DocxParser) extractEndnotes() (*strings.Builder, error) {
if dp.endnotesFile == nil {
dp.logWarn(types.ErrNoEndnotes)
return new(strings.Builder), nil
}

rc, err := dp.endnotesFile.Open()
if err != nil {
return nil, err
Expand Down Expand Up @@ -441,6 +452,11 @@ NEXT:
// - *strings.Builder: a strings.Builder containing the extracted footnotes text.
// - error: an error if any.
func (dp *DocxParser) extractFootnotes() (*strings.Builder, error) {
if dp.footnotesFile == nil {
dp.logWarn(types.ErrNoFootnotes)
return new(strings.Builder), nil
}

rc, err := dp.footnotesFile.Open()
if err != nil {
return nil, err
Expand Down
15 changes: 10 additions & 5 deletions types/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,14 @@ package types
import "errors"

var (
ErrNilZipFile = errors.New("the input zip file is nil")
ErrEmptyRID = errors.New("the rId is empty")
ErrNonePart = errors.New("the document part resolves failed or not exists")
ErrNoSlide = errors.New("the specified slide is not found")
ErrNoSheet = errors.New("the specified sheet is not found")
ErrNilZipFile = errors.New("the input zip file is nil")
ErrEmptyRID = errors.New("the rId is empty")
ErrNonePart = errors.New("the document part resolves failed or not exists")
ErrNoSlide = errors.New("the specified slide is not found")
ErrNoSheet = errors.New("the specified sheet is not found")
ErrNoSharedStrings = errors.New("the sharedStrings.xml file is not found")
ErrNoDocument = errors.New("the document.xml file is not found")
ErrNoComments = errors.New("the comments.xml file is not found")
ErrNoEndnotes = errors.New("the endnotes.xml file is not found")
ErrNoFootnotes = errors.New("the footnotes.xml file is not found")
)
5 changes: 5 additions & 0 deletions xlsxtotext/xlsxtotext.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,11 @@ func (xp *XlsxParser) ExtractTexts() (string, error) {
// - error: An error if there is any issue with opening the file or parsing
// the XML elements.
func (xp *XlsxParser) parseSharedStrings() error {
if xp.sharedStringsFile == nil {
xp.logWarn(types.ErrNoSharedStrings)
return nil
}

rc, err := xp.sharedStringsFile.Open()
if err != nil {
return err
Expand Down

0 comments on commit 4a602c8

Please sign in to comment.