From 8c6181db9fc91a9f3edbda4b3ece89739b9a3ebc Mon Sep 17 00:00:00 2001 From: Igor Postelnik Date: Mon, 9 Nov 2020 16:53:51 -0600 Subject: [PATCH] TUN-3524: Don't ignore errors from app-level action handler (#248) --- cmd/cloudflared/cliutil/errors.go | 10 +++++----- cmd/cloudflared/main.go | 14 +++++--------- 2 files changed, 10 insertions(+), 14 deletions(-) diff --git a/cmd/cloudflared/cliutil/errors.go b/cmd/cloudflared/cliutil/errors.go index 71976156..902ad2ff 100644 --- a/cmd/cloudflared/cliutil/errors.go +++ b/cmd/cloudflared/cliutil/errors.go @@ -27,17 +27,17 @@ func UsageError(format string, args ...interface{}) error { // Ensures exit with error code if actionFunc returns an error func ErrorHandler(actionFunc cli.ActionFunc) cli.ActionFunc { return func(ctx *cli.Context) error { + defer logger.SharedWriteManager.Shutdown() + err := actionFunc(ctx) if err != nil { if _, ok := err.(usageError); ok { msg := fmt.Sprintf("%s\nSee 'cloudflared %s --help'.", err.Error(), ctx.Command.FullName()) - return cli.Exit(msg, -1) + err = cli.Exit(msg, -1) + } else if _, ok := err.(cli.ExitCoder); !ok { + err = cli.Exit(err.Error(), 1) } - // os.Exits with error code if err is cli.ExitCoder or cli.MultiError - cli.HandleExitCoder(err) - err = cli.Exit(err.Error(), 1) } - logger.SharedWriteManager.Shutdown() return err } } diff --git a/cmd/cloudflared/main.go b/cmd/cloudflared/main.go index 833b3090..44b1028e 100644 --- a/cmd/cloudflared/main.go +++ b/cmd/cloudflared/main.go @@ -145,7 +145,7 @@ func isEmptyInvocation(c *cli.Context) bool { } func action(version string, shutdownC, graceShutdownC chan struct{}) cli.ActionFunc { - return func(c *cli.Context) (err error) { + return cliutil.ErrorHandler(func(c *cli.Context) (err error) { if isEmptyInvocation(c) { return handleServiceMode(shutdownC) } @@ -153,15 +153,11 @@ func action(version string, shutdownC, graceShutdownC chan struct{}) cli.ActionF tags["hostname"] = c.String("hostname") raven.SetTagsContext(tags) raven.CapturePanic(func() { err = tunnel.TunnelCommand(c) }, nil) - exitCode := 0 if err != nil { - handleError(err) - exitCode = 1 + captureError(err) } - // we already handle error printing, so we pass an empty string so we - // don't have to print again. - return cli.Exit("", exitCode) - } + return err + }) } func userHomeDir() (string, error) { @@ -177,7 +173,7 @@ func userHomeDir() (string, error) { } // In order to keep the amount of noise sent to Sentry low, typical network errors can be filtered out here by a substring match. -func handleError(err error) { +func captureError(err error) { errorMessage := err.Error() for _, ignoredErrorMessage := range ignoredErrors { if strings.Contains(errorMessage, ignoredErrorMessage) {