Skip to content

Commit

Permalink
Fix panic on nil pointer in state. Add panic recover code.
Browse files Browse the repository at this point in the history
  • Loading branch information
brikis98 committed Jun 13, 2016
1 parent 2e81f16 commit d9de426
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 11 deletions.
4 changes: 3 additions & 1 deletion cli/cli_app.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,9 @@ func CreateTerragruntCli(version string) *cli.App {

// The sole action for the app. It forwards all commands directly to Terraform, enforcing a few best practices along
// the way, such as configuring remote state or acquiring a lock.
func runApp(cliContext *cli.Context) error {
func runApp(cliContext *cli.Context) (finalErr error) {
defer errors.Recover(func(cause error) { finalErr = cause })

terragruntConfig, err := config.ReadTerragruntConfig()
if err != nil {
return err
Expand Down
12 changes: 12 additions & 0 deletions errors/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,16 @@ func PrintErrorWithStackTrace(err error) string {
case *goerrors.Error: return underlyingErr.ErrorStack()
default: return err.Error()
}
}

// A method that tries to recover from panics, and if it succeeds, calls the given onPanic function with an error that
// explains the cause of the panic. This function should only be called from a defer statement.
func Recover(onPanic func(cause error)) {
if rec := recover(); rec != nil {
err, isError := rec.(error)
if !isError {
err = fmt.Errorf("%v", rec)
}
onPanic(WithStackTrace(err))
}
}
23 changes: 14 additions & 9 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,25 @@ var VERSION string

// The main entrypoint for Terragrunt
func main() {
defer errors.Recover(checkForErrorsAndExit)

app := cli.CreateTerragruntCli(VERSION)
err := app.Run(os.Args)

if err != nil {
printError(err)
os.Exit(1)
}
checkForErrorsAndExit(err)
}

// Display the given error in the console
func printError(err error) {
if os.Getenv("TERRAGRUNT_DEBUG") != "" {
util.Logger.Println(errors.PrintErrorWithStackTrace(err))
// If there is an error, display it in the console and exit with a non-zero exit code. Otherwise, exit 0.
func checkForErrorsAndExit(err error) {
if err == nil {
os.Exit(0)
} else {
util.Logger.Println(err)
if os.Getenv("TERRAGRUNT_DEBUG") != "" {
util.Logger.Println(errors.PrintErrorWithStackTrace(err))
} else {
util.Logger.Println(err)
}
os.Exit(1)
}

}
2 changes: 1 addition & 1 deletion remote/remote_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func shouldConfigureRemoteState(remoteStateFromTerragruntConfig RemoteState) (bo
return false, err
}

if state.IsRemote() {
if state != nil && state.IsRemote() {
return shouldOverrideExistingRemoteState(state.Remote, remoteStateFromTerragruntConfig)
} else {
return true, nil
Expand Down

0 comments on commit d9de426

Please sign in to comment.