Skip to content

Commit

Permalink
refactor: Early return in case of a connection error (#389)
Browse files Browse the repository at this point in the history
#### Summary

The usual pattern in go is to do `if err != nil { // do something }`.
The current code is a bit confusing to read as it does `if err == nil { return err }`.
A smaller change can be to do `if err == nil { return nil }`

---
  • Loading branch information
erezrokah authored Aug 8, 2024
1 parent e32084d commit e4619ac
Showing 1 changed file with 10 additions and 14 deletions.
24 changes: 10 additions & 14 deletions managedplugin/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -484,23 +484,19 @@ func (c *Client) startLocalUnixSocket(ctx context.Context, path string) error {
c.wg.Add(1)
go c.readLogLines(reader)

err = c.connectToUnixSocket(ctx)
// N.B. we exit early if connecting succeeds here!
if err == nil {
return nil
}

// Error scenarios:
if err := c.connectToUnixSocket(ctx); err != nil {
if killErr := cmd.Process.Kill(); killErr != nil {
c.logger.Error().Err(killErr).Msg("failed to kill plugin process")
}

if killErr := cmd.Process.Kill(); killErr != nil {
c.logger.Error().Err(killErr).Msg("failed to kill plugin process")
waitErr := cmd.Wait()
if waitErr != nil && errors.Is(err, context.DeadlineExceeded) {
return fmt.Errorf("failed to run plugin %s: %w", path, waitErr)
}
return fmt.Errorf("failed connecting to plugin %s: %w", path, err)
}

waitErr := cmd.Wait()
if waitErr != nil && errors.Is(err, context.DeadlineExceeded) {
return fmt.Errorf("failed to run plugin %s: %w", path, waitErr)
}
return fmt.Errorf("failed connecting to plugin %s: %w", path, err)
return nil
}

func (c *Client) getPluginArgs() []string {
Expand Down

0 comments on commit e4619ac

Please sign in to comment.