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)
}
// 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)

View File

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