-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
command: capture and report panics in command handlers
Add a sentinel error that is injected when a panic reaches the root of Run. Update the example to show it at work. Add a test to make sure it does.
- Loading branch information
1 parent
a755450
commit 02b10e5
Showing
5 changed files
with
57 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
// Copyright (C) 2020 Michael J. Fromberger. All Rights Reserved. | ||
|
||
package command_test | ||
|
||
import ( | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// Copyright (C) 2020 Michael J. Fromberger. All Rights Reserved. | ||
|
||
package command_test | ||
|
||
import ( | ||
"errors" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/creachadair/command" | ||
) | ||
|
||
func TestRun_panic(t *testing.T) { | ||
const message = "omg the sky is falling" | ||
cmd := &command.C{ | ||
Name: "freak-out", | ||
Run: func(*command.Env) error { | ||
panic(message) | ||
}, | ||
} | ||
err := command.Run(cmd.NewEnv(nil), []string{"freak-out"}) | ||
if !errors.Is(err, command.ErrRunPanicked) { | ||
t.Fatalf("Run: should panic, got error: %v", err) | ||
} | ||
if !strings.Contains(err.Error(), message) { | ||
t.Error("Panic error does not contain the probe string") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
// Copyright (C) 2020 Michael J. Fromberger. All Rights Reserved. | ||
|
||
package command_test | ||
|
||
import ( | ||
|