https://github.com/ttab/flerr
flerr makes it easy to capture errors from deferred functions and allows something close to a block level defer
https://github.com/ttab/flerr
Last synced: 11 months ago
JSON representation
flerr makes it easy to capture errors from deferred functions and allows something close to a block level defer
- Host: GitHub
- URL: https://github.com/ttab/flerr
- Owner: ttab
- License: mit
- Created: 2025-01-17T14:56:27.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2025-01-19T11:29:52.000Z (over 1 year ago)
- Last Synced: 2025-03-04T19:44:01.311Z (over 1 year ago)
- Language: Go
- Size: 6.84 KB
- Stars: 0
- Watchers: 5
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Flush errors
Helper library that can be used to capture errors from deferred functions and allows something close to a block level defer.
Example usage:
``` go
func createScriptDir(sourceDir embed.FS, scriptDir string) (outErr error) {
err := os.MkdirAll(scriptDir, 0o700)
if err != nil {
return fmt.Errorf("create script directory: %w", err)
}
scripts, err := sourceDir.ReadDir(".")
if err != nil {
return fmt.Errorf("list files in source directory: %w", err)
}
var clean flerr.Cleaner
// Clean up all files when the function returns, joining in any errors
// that might result from the cleanup.
defer clean.FlushTo(&outErr)
for _, f := range scripts {
if f.IsDir() || !strings.HasSuffix(f.Name(), ".py") {
continue
}
src, err := pysrc.Scripts.Open(f.Name())
if err != nil {
return fmt.Errorf("open %q for reading: %w", f.Name(), err)
}
clean.Addf(src.Close, "close %q", f.Name())
dst, err := os.Create(filepath.Join(scriptDir, f.Name()))
if err != nil {
return fmt.Errorf("create destination file for %q: %w", f.Name(), err)
}
clean.Addf(dst.Close, "close %q destination", f.Name())
_, err = io.Copy(dst, src)
if err != nil {
return fmt.Errorf("copy %q: %w", f.Name(), err)
}
// Clean up all files at the end of the loop.
err = clean.Flush()
if err != nil {
return err
}
}
return nil
}
```