Update error handling

This commit is contained in:
Alex Yatskov 2020-08-12 21:03:49 -07:00
parent f410ad444d
commit 9aba24e892
3 changed files with 12 additions and 25 deletions

View File

@ -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())
}

View File

@ -93,6 +93,11 @@ func (file *File) Seek(offset int64, whence int) (int64, error) {
return file.reader.Seek(offset, whence) 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 { func (file *File) export(targetDir string) error {
targetPath := filepath.Join(targetDir, file.sourcePath) targetPath := filepath.Join(targetDir, file.sourcePath)

View File

@ -2,6 +2,7 @@
package goldsmith package goldsmith
import ( import (
"fmt"
"hash" "hash"
"hash/crc32" "hash/crc32"
"os" "os"
@ -164,13 +165,15 @@ func (goldsmith *Goldsmith) exportFile(file *File) error {
return nil 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() goldsmith.mutex.Lock()
defer goldsmith.mutex.Unlock() defer goldsmith.mutex.Unlock()
faultError := &Error{Name: pluginName, Err: err} var faultError error
if file != nil { if file == nil {
faultError.Path = file.sourcePath faultError = fmt.Errorf("[%s]: %w", name, err)
} else {
faultError = fmt.Errorf("[%s@%v]: %w", name, file, err)
} }
goldsmith.errors = append(goldsmith.errors, faultError) goldsmith.errors = append(goldsmith.errors, faultError)