This repository was archived by the owner on Aug 30, 2024. It is now read-only.
- Notifications
You must be signed in to change notification settings - Fork18
Add clog.ErrGroup to handle errgroup logging#157
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes fromall commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
7 changes: 7 additions & 0 deletionsinternal/clog/doc.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
// package clog provides rich error types and logging helpers for coder-cli. | ||
// | ||
// clog encourages returning error types rather than | ||
// logging them and failing with os.Exit as they happen. | ||
// Error, Fatal, and Warn allow downstream functions to return errors with rich formatting information | ||
// while preserving the original, single-line error chain. | ||
package clog |
58 changes: 58 additions & 0 deletionsinternal/clog/errgroup.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package clog | ||
import ( | ||
"fmt" | ||
"sync/atomic" | ||
"golang.org/x/sync/errgroup" | ||
"golang.org/x/xerrors" | ||
) | ||
// ErrGroup wraps the /x/sync/errgroup.(Group) and adds clog logging and rich error propagation. | ||
// | ||
// Take for example, a case in which we are concurrently stopping a slice of environments. | ||
// In this case, we want to log errors as they happen, not pass them through the callstack as errors. | ||
// When the operations complete, we want to log how many, if any, failed. The caller is still expected | ||
// to handle success and info logging. | ||
type ErrGroup interface { | ||
Go(f func() error) | ||
Wait() error | ||
} | ||
type group struct { | ||
egroup errgroup.Group | ||
failures int32 | ||
} | ||
// LoggedErrGroup gives an error group with error logging and error propagation handled automatically. | ||
func LoggedErrGroup() ErrGroup { | ||
return &group{ | ||
egroup: errgroup.Group{}, | ||
failures: 0, | ||
} | ||
} | ||
func (g *group) Go(f func() error) { | ||
g.egroup.Go(func() error { | ||
if err := f(); err != nil { | ||
atomic.AddInt32(&g.failures, 1) | ||
Log(err) | ||
// this error does not matter because we discard it in Wait. | ||
return xerrors.New("") | ||
} | ||
return nil | ||
}) | ||
} | ||
func (g *group) Wait() error { | ||
_ = g.egroup.Wait() // ignore this error because we are already tracking failures manually | ||
if g.failures == 0 { | ||
return nil | ||
} | ||
failureWord := "failure" | ||
if g.failures > 1 { | ||
failureWord += "s" | ||
} | ||
return Fatal(fmt.Sprintf("%d %s emitted", g.failures, failureWord)) | ||
} |
35 changes: 7 additions & 28 deletionsinternal/cmd/envs.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.