Skip to content

Commit

Permalink
Merge pull request #343 from soraxas/clean-error-handling
Browse files Browse the repository at this point in the history
feat: Cleaner error handling for looping over gateways
  • Loading branch information
moul authored Jun 7, 2020
2 parents 93c2d86 + f02261c commit ee3bc4d
Showing 1 changed file with 21 additions and 6 deletions.
27 changes: 21 additions & 6 deletions pkg/commands/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ import (

type contextKey string

type gatewayErrorMsg struct {
gateway string
err zap.Field
}

var syncContextKey contextKey = "sync"

var proxyCommand = &cobra.Command{
Expand Down Expand Up @@ -228,11 +233,13 @@ func proxy(host *config.Host, conf *config.Config, dryRun bool) error {

if len(host.Gateways) > 0 {
logger().Debug("Trying gateways", zap.String("gateways", strings.Join(host.Gateways, ", ")))
var gatewayErrors []gatewayErrorMsg
for _, gateway := range host.Gateways {
log.Println(gateway)
if gateway == "direct" {
if err := proxyDirect(host, dryRun); err != nil {
logger().Error("Failed to use 'direct' connection", zap.Error(err))
gatewayErrors = append(gatewayErrors, gatewayErrorMsg{
gateway: "direct", err: zap.Error(err)})
} else {
return nil
}
Expand Down Expand Up @@ -267,16 +274,24 @@ func proxy(host *config.Host, conf *config.Config, dryRun bool) error {
zap.String("command", command),
)
if err := runProxy(gatewayHost, command, dryRun); err != nil {
logger().Error(
"Cannot use gateway",
zap.String("gateway", gateway),
zap.Error(err),
)
gatewayErrors = append(gatewayErrors, gatewayErrorMsg{
gateway: gateway, err: zap.Error(err)})
} else {
return nil
}
}
}
if len(gatewayErrors) > 0 {
for _, errMsg := range gatewayErrors{
conType := "gateway"
if errMsg.gateway == "direct" {
conType = "connection"
}
logger().Error(
fmt.Sprintf("Failed to use '%s' %s with error:",
errMsg.gateway, conType), errMsg.err)
}
}
return errors.New("no such available gateway")
}

Expand Down

0 comments on commit ee3bc4d

Please sign in to comment.