Skip to content

Configurable stack traces #15

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Nov 28, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion entrypoint/entrypoint.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package entrypoint

import (
"fmt"
"os"

"github.com/urfave/cli"
Expand All @@ -11,6 +12,7 @@ import (

const defaultSuccessExitCode = 0
const defaultErrorExitCode = 1
const debugEnvironmentVarName = "GRUNTWORK_DEBUG"

// Wrapper around cli.NewApp that sets the help text printer.
func NewApp() *cli.App {
Expand All @@ -34,11 +36,17 @@ func RunApp(app *cli.App) {
}

// If there is an error, display it in the console and exit with a non-zero exit code. Otherwise, exit 0.
// Note that if the GRUNTWORK_DEBUG environment variable is set, this will print out the stack trace.
func checkForErrorsAndExit(err error) {
exitCode := defaultSuccessExitCode
isDebugMode := os.Getenv(debugEnvironmentVarName) != ""

if err != nil {
logging.GetLogger("").WithError(err).Error(errors.PrintErrorWithStackTrace(err))
if isDebugMode {
logging.GetLogger("").WithError(err).Error(errors.PrintErrorWithStackTrace(err))
} else {
fmt.Fprintf(os.Stderr, "ERROR: %s\n", errors.Unwrap(err))
}

errorWithExitCode, isErrorWithExitCode := err.(errors.ErrorWithExitCode)
if isErrorWithExitCode {
Expand Down
10 changes: 6 additions & 4 deletions errors/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func WithStackTrace(err error) error {
// Wrap the given error in an Error type that contains the stack trace and has the given message prepended as part of
// the error message. If the given error already has a stack trace, it is used directly. If the given error is nil,
// return nil.
func WithStackTraceAndPrefix(err error, message string, args ... interface{}) error {
func WithStackTraceAndPrefix(err error, message string, args ...interface{}) error {
if err == nil {
return nil
}
Expand Down Expand Up @@ -65,8 +65,10 @@ func PrintErrorWithStackTrace(err error) string {
}

switch underlyingErr := err.(type) {
case *goerrors.Error: return underlyingErr.ErrorStack()
default: return err.Error()
case *goerrors.Error:
return underlyingErr.ErrorStack()
default:
return err.Error()
}
}

Expand All @@ -92,4 +94,4 @@ func WithPanicHandling(action func(*cli.Context) error) func(*cli.Context) error

return action(context)
}
}
}