From ca13b8fd8e5c9d76660280e542f2ddd6389db7c0 Mon Sep 17 00:00:00 2001 From: bmaupin Date: Fri, 28 Sep 2018 13:46:14 -0400 Subject: [PATCH] Improve output of FileRetrievalError source errors And update some names for consistency and spelling --- epub.go | 47 +++++++++++++++++++---------------------------- epub_test.go | 6 +++--- write.go | 10 +++++----- 3 files changed, 27 insertions(+), 36 deletions(-) diff --git a/epub.go b/epub.go index 3ecb023..70a37f0 100644 --- a/epub.go +++ b/epub.go @@ -26,7 +26,6 @@ Basic usage: package epub import ( - "errors" "fmt" "io" "io/ioutil" @@ -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 @@ -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, } } @@ -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 @@ -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 @@ -458,7 +449,7 @@ 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 { @@ -466,5 +457,5 @@ func isFileSourceValid(source string) bool { } }() - return true + return nil } diff --git a/epub_test.go b/epub_test.go index c014420..fe59ac2 100644 --- a/epub_test.go +++ b/epub_test.go @@ -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) } } diff --git a/write.go b/write.go index 583f26c..533dbaf 100644 --- a/write.go +++ b/write.go @@ -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{ @@ -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 @@ -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 @@ -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( @@ -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))]