Improve generator error handling

This commit is contained in:
go-jet 2023-07-21 13:20:44 +02:00
parent b38b63d804
commit 06ecd73f67
12 changed files with 386 additions and 176 deletions

View file

@ -0,0 +1,10 @@
package errfmt
import (
"strings"
)
// Trace returns well formatted wrapped error trace string
func Trace(err error) string {
return "Error trace:\n" + " - " + strings.Replace(err.Error(), ": ", ":\n - ", -1)
}

View file

@ -39,14 +39,16 @@ func SaveGoFile(dirPath, fileName string, text []byte) error {
defer file.Close()
p, err := format.Source(text)
// if there is a format error we will write unformulated text for debug purposes
if err != nil {
return err
file.Write(text)
return fmt.Errorf("failed to format '%s', check '%s' for syntax errors: %w", fileName, newGoFilePath, err)
}
_, err = file.Write(p)
if err != nil {
return err
return fmt.Errorf("failed to save '%s' file: %w", newGoFilePath, err)
}
return nil
@ -58,7 +60,7 @@ func EnsureDirPath(dirPath string) error {
err := os.MkdirAll(dirPath, os.ModePerm)
if err != nil {
return err
return fmt.Errorf("can't create directory - %s: %w", dirPath, err)
}
}