Skip to content
This repository has been archived by the owner on Aug 29, 2023. It is now read-only.

Commit

Permalink
Improve output of FileRetrievalError source errors
Browse files Browse the repository at this point in the history
And update some names for consistency and spelling
  • Loading branch information
bmaupin committed Sep 28, 2018
1 parent 0e761ec commit ca13b8f
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 36 deletions.
47 changes: 19 additions & 28 deletions epub.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ Basic usage:
package epub

import (
"errors"
"fmt"
"io"
"io/ioutil"
Expand All @@ -40,32 +39,24 @@ import (
)

// FilenameAlreadyUsedError is thrown by AddCSS, AddFont, AddImage, or AddSection
// if the same filename is used more than once
// if the same filename is used more than once.
type FilenameAlreadyUsedError struct {
Filename string // Filename that caused the error
}

func (e *FilenameAlreadyUsedError) Error() string {
return "Filename already used: " + e.Filename
return fmt.Sprintf("Filename already used: %s", e.Filename)
}

// ErrInvalidSource is used inside FileRetrevalError for calls to
// AddCSS, AddFont, AddImage, AddSection with an invalid file source.
var ErrInvalidSource = errors.New("Invalid Source")

// FileRetrevalError is thrown by Write in the case of a media file added with
// AddImage, AddFont, AddCSS being unable to be read.
type FileRetrevalError struct {
// File is the file that we failed to retreive.
File string

// Err is the underlying error that we encountered attempting to retreive File.
Err error
// FileRetrievalError is thrown by AddCSS, AddFont, AddImage, or Write if there was a
// problem retrieving the source file that was provided.
type FileRetrievalError struct {
Source string // The source of the file whose retrieval failed
Err error // The underlying error that was thrown
}

// Error implements the error interface.
func (e *FileRetrevalError) Error() string {
return fmt.Sprintf("Error retrieving %q from source: %+v", e.File, e.Err)
func (e *FileRetrievalError) Error() string {
return fmt.Sprintf("Error retrieving %q from source: %+v", e.Source, e.Err)
}

// Folder names used for resources inside the EPUB
Expand Down Expand Up @@ -403,11 +394,11 @@ func (e *Epub) Title() string {
// Add a media file to the EPUB and return the path relative to the EPUB section
// files
func addMedia(source string, internalFilename string, mediaFileFormat string, mediaFolderName string, mediaMap map[string]string) (string, error) {
// Make sure the source file is valid before proceeding
if isFileSourceValid(source) == false {
return "", &FileRetrevalError{
File: source,
Err: ErrInvalidSource,
err := validateFileSource(source)
if err != nil {
return "", &FileRetrievalError{
Source: source,
Err: err,
}
}

Expand Down Expand Up @@ -437,10 +428,10 @@ func addMedia(source string, internalFilename string, mediaFileFormat string, me
), nil
}

func isFileSourceValid(source string) bool {
func validateFileSource(source string) error {
u, err := url.Parse(source)
if err != nil {
return false
return err
}

var r io.ReadCloser
Expand All @@ -449,7 +440,7 @@ func isFileSourceValid(source string) bool {
if u.Scheme == "http" || u.Scheme == "https" {
resp, err = http.Get(source)
if err != nil {
return false
return err
}
r = resp.Body

Expand All @@ -458,13 +449,13 @@ func isFileSourceValid(source string) bool {
r, err = os.Open(source)
}
if err != nil {
return false
return err
}
defer func() {
if err := r.Close(); err != nil {
panic(err)
}
}()

return true
return nil
}
6 changes: 3 additions & 3 deletions epub_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -567,12 +567,12 @@ func TestFilenameAlreadyUsedError(t *testing.T) {
}
}

func TestFileRetrevalError(t *testing.T) {
func TestFileRetrievalError(t *testing.T) {
e := NewEpub(testEpubTitle)

_, err := e.AddCSS("/sbin/thisShouldFail", testCoverCSSFilename)
if _, ok := err.(*FileRetrevalError); !ok {
t.Errorf("Expected error FileRetrevalError not returned. Returned instead: %+v", err)
if _, ok := err.(*FileRetrievalError); !ok {
t.Errorf("Expected error FileRetrievalError not returned. Returned instead: %+v", err)
}
}

Expand Down
10 changes: 5 additions & 5 deletions write.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ type UnableToCreateEpubError struct {
}

func (e *UnableToCreateEpubError) Error() string {
return fmt.Sprintf("Error creating EPUB at path: %q, error: %+v", e.Path, e.Err)
return fmt.Sprintf("Error creating EPUB at %q: %+v", e.Path, e.Err)
}

var extensionMediaTypes = map[string]string{
Expand Down Expand Up @@ -318,7 +318,7 @@ func (e *Epub) writeMedia(tempDir string, mediaMap map[string]string, mediaFolde
// Get the media file from the source
u, err := url.Parse(mediaSource)
if err != nil {
return &FileRetrevalError{File: mediaSource, Err: err}
return &FileRetrievalError{Source: mediaSource, Err: err}
}

var r io.ReadCloser
Expand All @@ -327,7 +327,7 @@ func (e *Epub) writeMedia(tempDir string, mediaMap map[string]string, mediaFolde
if u.Scheme == "http" || u.Scheme == "https" {
resp, err = http.Get(mediaSource)
if err != nil {
return &FileRetrevalError{File: mediaSource, Err: err}
return &FileRetrievalError{Source: mediaSource, Err: err}
}
r = resp.Body

Expand All @@ -336,7 +336,7 @@ func (e *Epub) writeMedia(tempDir string, mediaMap map[string]string, mediaFolde
r, err = os.Open(mediaSource)
}
if err != nil {
return &FileRetrevalError{File: mediaSource, Err: err}
return &FileRetrievalError{Source: mediaSource, Err: err}
}

mediaFilePath := filepath.Join(
Expand Down Expand Up @@ -366,7 +366,7 @@ func (e *Epub) writeMedia(tempDir string, mediaMap map[string]string, mediaFolde
if err != nil {
// There shouldn't be any problem with the writer, but the reader
// might have an issue
return &FileRetrevalError{File: mediaSource, Err: err}
return &FileRetrievalError{Source: mediaSource, Err: err}
}

mediaType := extensionMediaTypes[strings.ToLower(filepath.Ext(mediaFilename))]
Expand Down

0 comments on commit ca13b8f

Please sign in to comment.