From 9aba24e892e6da2c444dd60f12e4258b2967dc1d Mon Sep 17 00:00:00 2001 From: Alex Yatskov Date: Wed, 12 Aug 2020 21:03:49 -0700 Subject: [PATCH] Update error handling --- error.go | 21 --------------------- file.go | 5 +++++ goldsmith.go | 11 +++++++---- 3 files changed, 12 insertions(+), 25 deletions(-) delete mode 100644 error.go diff --git a/error.go b/error.go deleted file mode 100644 index b0ecaaa..0000000 --- a/error.go +++ /dev/null @@ -1,21 +0,0 @@ -package goldsmith - -import "fmt" - -// Error wraps the core error type to provide a plugin or filter name in -// addition to the file path that was being processed at the time. -type Error struct { - Name string - Path string - Err error -} - -// Error returns a string representation of the error. -func (err Error) Error() string { - var path string - if len(err.Path) > 0 { - path = "@" + err.Path - } - - return fmt.Sprintf("[%s%s]: %s", err.Name, path, err.Err.Error()) -} diff --git a/file.go b/file.go index 2e89b93..8e57ed4 100644 --- a/file.go +++ b/file.go @@ -93,6 +93,11 @@ func (file *File) Seek(offset int64, whence int) (int64, error) { return file.reader.Seek(offset, whence) } +// Returns value for string formatting. +func (file *File) GoString() string { + return file.sourcePath +} + func (file *File) export(targetDir string) error { targetPath := filepath.Join(targetDir, file.sourcePath) diff --git a/goldsmith.go b/goldsmith.go index 394c272..9d50663 100644 --- a/goldsmith.go +++ b/goldsmith.go @@ -2,6 +2,7 @@ package goldsmith import ( + "fmt" "hash" "hash/crc32" "os" @@ -164,13 +165,15 @@ func (goldsmith *Goldsmith) exportFile(file *File) error { return nil } -func (goldsmith *Goldsmith) fault(pluginName string, file *File, err error) { +func (goldsmith *Goldsmith) fault(name string, file *File, err error) { goldsmith.mutex.Lock() defer goldsmith.mutex.Unlock() - faultError := &Error{Name: pluginName, Err: err} - if file != nil { - faultError.Path = file.sourcePath + var faultError error + if file == nil { + faultError = fmt.Errorf("[%s]: %w", name, err) + } else { + faultError = fmt.Errorf("[%s@%v]: %w", name, file, err) } goldsmith.errors = append(goldsmith.errors, faultError)