Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: proper error handling #2

Merged
merged 29 commits into from
Sep 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
2f84a09
remove some of panic from code
Monirzadeh Aug 9, 2023
8c7973f
use fmt.Errorf instead of errors package
Monirzadeh Aug 10, 2023
23adf11
remove more panic from code
Monirzadeh Aug 10, 2023
17ea293
remove more panic
Monirzadeh Aug 10, 2023
e720479
error handleing for writeMimetype and writeContainerFile
Monirzadeh Aug 10, 2023
8ab2738
Merge branch 'main' into better-error-handling
Monirzadeh Aug 13, 2023
6e9b5b3
sync with main
Monirzadeh Aug 16, 2023
f352cf5
update unit tests
Monirzadeh Aug 18, 2023
ec38e8f
remove more panic
Monirzadeh Aug 18, 2023
1c5c9c1
remove all panic from code
Monirzadeh Aug 18, 2023
ca5c88f
just log instead of fatal error
Monirzadeh Aug 19, 2023
4ff8d8a
Update epub.go
Monirzadeh Aug 30, 2023
edd3e81
Update epub.go
Monirzadeh Aug 30, 2023
f73cca3
Update epub.go
Monirzadeh Aug 30, 2023
1876b3b
Update epub_test.go
Monirzadeh Aug 30, 2023
e8e45c6
Update toc.go
Monirzadeh Aug 30, 2023
e20c001
Update toc.go
Monirzadeh Aug 30, 2023
fc0debe
Update toc.go
Monirzadeh Aug 30, 2023
ba331c6
Update toc.go
Monirzadeh Aug 30, 2023
1a61571
Update write.go
Monirzadeh Aug 30, 2023
5c46137
add testing.T variable
Monirzadeh Aug 30, 2023
894eb45
fix some missing returen and remove unneeded newline
Monirzadeh Aug 30, 2023
b270c88
Update epub_test.go
Monirzadeh Aug 30, 2023
5180f7c
Update epub_test.go
Monirzadeh Aug 30, 2023
5f18fdd
Merge branch 'main' into better-error-handling
Monirzadeh Aug 30, 2023
81da1f1
remove unneeded import
Monirzadeh Aug 30, 2023
79d39c4
remove github.com/stretchr/testify/require from dependencies
Monirzadeh Aug 30, 2023
4732c93
add error handling for all unit test and update comment
Monirzadeh Sep 2, 2023
a419e77
remove uneeded variable
Monirzadeh Sep 5, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 26 additions & 10 deletions epub.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ contents for maximum compatibility.
Basic usage:

// Create a new EPUB
e := epub.NewEpub("My title")
e, err := epub.NewEpub("My title")
if err != nil {
log.Println(err)
}


// Set the author
e.SetAuthor("Hingle McCringleberry")
Expand Down Expand Up @@ -155,7 +159,8 @@ type epubSection struct {
}

// NewEpub returns a new Epub.
func NewEpub(title string) *Epub {
func NewEpub(title string) (*Epub, error) {
var err error
e := &Epub{}
e.cover = &epubCover{
cssFilename: "",
Expand All @@ -169,14 +174,20 @@ func NewEpub(title string) *Epub {
e.images = make(map[string]string)
e.videos = make(map[string]string)
e.audios = make(map[string]string)
e.pkg = newPackage()
e.toc = newToc()
e.pkg, err = newPackage()
if err != nil {
return nil, fmt.Errorf("can't create NewEpub: %w", err)
}
e.toc, err = newToc()
if err != nil {
return nil, fmt.Errorf("can't create NewEpub: %w", err)
}
// Set minimal required attributes
e.SetIdentifier(urnUUIDPrefix + uuid.Must(uuid.NewV4()).String())
e.SetLang(defaultEpubLang)
e.SetTitle(title)

return e
return e, nil
}

// AddCSS adds a CSS file to the EPUB and returns a relative path to the CSS
Expand Down Expand Up @@ -370,7 +381,11 @@ func (e *Epub) addSection(parentFilename string, body string, sectionTitle strin
return "", &ParentDoesNotExistError{Filename: parentFilename}
}

x := newXhtml(body)
x, err := newXhtml(body)
if err != nil {
//return internalFilename, errors.Wrap(err, "can't add section we cant create xhtml")
return internalFilename, fmt.Errorf("can't add section we cant create xhtml: %w", err)
}
x.setTitle(sectionTitle)
x.setXmlnsEpub(xmlnsEpub)

Expand Down Expand Up @@ -439,7 +454,7 @@ func (e *Epub) SetAuthor(author string) {
// The internal path to an already-added CSS file (as returned by AddCSS) to be
// used for the cover is optional. If the CSS path isn't provided, default CSS
// will be used.
func (e *Epub) SetCover(internalImagePath string, internalCSSPath string) {
func (e *Epub) SetCover(internalImagePath string, internalCSSPath string) error {
e.Lock()
defer e.Unlock()
// If a cover already exists
Expand Down Expand Up @@ -483,12 +498,12 @@ func (e *Epub) SetCover(internalImagePath string, internalCSSPath string) {
internalCSSPath, err = e.addCSS(e.cover.cssTempFile, coverCSSFilename)
if _, ok := err.(*FilenameAlreadyUsedError); ok {
// This shouldn't cause an error
panic(fmt.Sprintf("Error adding default cover CSS file: %s", err))
return fmt.Errorf("Error adding default cover CSS file: %w", err)
}
}
if err != nil {
if _, ok := err.(*FilenameAlreadyUsedError); !ok {
panic(fmt.Sprintf("DEBUG %+v", err))
return err
}
}
}
Expand All @@ -503,10 +518,11 @@ func (e *Epub) SetCover(internalImagePath string, internalCSSPath string) {
coverPath, err = e.addSection("", coverBody, "", "", internalCSSPath)
if _, ok := err.(*FilenameAlreadyUsedError); ok {
// This shouldn't cause an error since we're not specifying a filename
panic(fmt.Sprintf("Error adding default cover XHTML file: %s", err))
return fmt.Errorf("Error adding default cover XHTML file: %w", err)
}
}
e.cover.xhtmlFilename = filepath.Base(coverPath)
return nil
}

// SetIdentifier sets the unique identifier of the EPUB, such as a UUID, DOI,
Expand Down
Loading