Skip to content
This repository was archived by the owner on Jul 22, 2024. It is now read-only.

Commit 2a8b68f

Browse files
committed
Unambiguous return values. Fixes #18
1 parent b3f3dc3 commit 2a8b68f

2 files changed

Lines changed: 21 additions & 21 deletions

File tree

panicwrap.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ type WrapConfig struct {
7878
// BasicWrap calls Wrap with the given handler function, using defaults
7979
// for everything else. See Wrap and WrapConfig for more information on
8080
// functionality and return values.
81-
func BasicWrap(f HandlerFunc) (int, error) {
81+
func BasicWrap(f HandlerFunc) (done bool, statusCode int, err error) {
8282
return Wrap(&WrapConfig{
8383
Handler: f,
8484
})
@@ -87,7 +87,7 @@ func BasicWrap(f HandlerFunc) (int, error) {
8787
// Wrap wraps the current executable in a handler to catch panics. It
8888
// returns an error if there was an error during the wrapping process.
8989
// If the error is nil, then the int result indicates the exit status of the
90-
// child process. If the exit status is -1, then this is the child process,
90+
// child process. If the done flag is false, then this is the child process,
9191
// and execution should continue as normal. Otherwise, this is the parent
9292
// process and the child successfully ran already, and you should exit the
9393
// process with the returned exit status.
@@ -97,9 +97,9 @@ func BasicWrap(f HandlerFunc) (int, error) {
9797
//
9898
// Once this is called, the given WrapConfig shouldn't be modified or used
9999
// any further.
100-
func Wrap(c *WrapConfig) (int, error) {
100+
func Wrap(c *WrapConfig) (done bool, statusCode int, err error) {
101101
if c.Handler == nil {
102-
return -1, errors.New("Handler must be set")
102+
return false, -1, errors.New("Handler must be set")
103103
}
104104

105105
if c.DetectDuration == 0 {
@@ -112,13 +112,13 @@ func Wrap(c *WrapConfig) (int, error) {
112112

113113
// If we're already wrapped, exit out.
114114
if Wrapped(c) {
115-
return -1, nil
115+
return false, -1, nil
116116
}
117117

118118
// Get the path to our current executable
119119
exePath, err := os.Executable()
120120
if err != nil {
121-
return -1, err
121+
return false, -1, err
122122
}
123123

124124
// Pipe the stderr so we can read all the data as we look for panics
@@ -165,7 +165,7 @@ func Wrap(c *WrapConfig) (int, error) {
165165
}
166166

167167
if err := cmd.Start(); err != nil {
168-
return 1, err
168+
return true, 1, err
169169
}
170170

171171
// Listen to signals and capture them forever. We allow the child
@@ -197,7 +197,7 @@ func Wrap(c *WrapConfig) (int, error) {
197197
exitErr, ok := err.(*exec.ExitError)
198198
if !ok {
199199
// This is some other kind of subprocessing error.
200-
return 1, err
200+
return true, 1, err
201201
}
202202

203203
exitStatus := 1
@@ -218,10 +218,10 @@ func Wrap(c *WrapConfig) (int, error) {
218218
c.Handler(panicTxt)
219219
}
220220

221-
return exitStatus, nil
221+
return true, exitStatus, nil
222222
}
223223

224-
return 0, nil
224+
return true, 0, nil
225225
}
226226

227227
// Wrapped checks if we're already wrapped according to the configuration

panicwrap_test.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,13 @@ func TestHelperProcess(*testing.T) {
6060
cmd, args := args[0], args[1:]
6161
switch cmd {
6262
case "no-panic-ordered-output":
63-
exitStatus, err := BasicWrap(panicHandler)
63+
done, exitStatus, err := BasicWrap(panicHandler)
6464
if err != nil {
6565
fmt.Fprintf(os.Stderr, "wrap error: %s", err)
6666
os.Exit(1)
6767
}
6868

69-
if exitStatus < 0 {
69+
if !done {
7070
for i := 0; i < 1000; i++ {
7171
os.Stdout.Write([]byte("a"))
7272
os.Stderr.Write([]byte("b"))
@@ -80,14 +80,14 @@ func TestHelperProcess(*testing.T) {
8080
fmt.Fprint(os.Stderr, "stderr out")
8181
os.Exit(0)
8282
case "panic-boundary":
83-
exitStatus, err := BasicWrap(panicHandler)
83+
done, exitStatus, err := BasicWrap(panicHandler)
8484

8585
if err != nil {
8686
fmt.Fprintf(os.Stderr, "wrap error: %s", err)
8787
os.Exit(1)
8888
}
8989

90-
if exitStatus < 0 {
90+
if !done {
9191
// Simulate a panic but on two boundaries...
9292
fmt.Fprint(os.Stderr, "pan")
9393
os.Stderr.Sync()
@@ -98,14 +98,14 @@ func TestHelperProcess(*testing.T) {
9898

9999
os.Exit(exitStatus)
100100
case "panic-long":
101-
exitStatus, err := BasicWrap(panicHandler)
101+
done, exitStatus, err := BasicWrap(panicHandler)
102102

103103
if err != nil {
104104
fmt.Fprintf(os.Stderr, "wrap error: %s", err)
105105
os.Exit(1)
106106
}
107107

108-
if exitStatus < 0 {
108+
if !done {
109109
// Make a fake panic by faking the header and adding a
110110
// bunch of garbage.
111111
fmt.Fprint(os.Stderr, "panic: foo\n\n")
@@ -133,14 +133,14 @@ func TestHelperProcess(*testing.T) {
133133
HidePanic: hidePanic,
134134
}
135135

136-
exitStatus, err := Wrap(config)
136+
done, exitStatus, err := Wrap(config)
137137

138138
if err != nil {
139139
fmt.Fprintf(os.Stderr, "wrap error: %s", err)
140140
os.Exit(1)
141141
}
142142

143-
if exitStatus < 0 {
143+
if !done {
144144
panic("uh oh")
145145
}
146146

@@ -154,13 +154,13 @@ func TestHelperProcess(*testing.T) {
154154
Handler: panicHandler,
155155
}
156156

157-
exitStatus, err := Wrap(config)
157+
done, exitStatus, err := Wrap(config)
158158
if err != nil {
159159
fmt.Fprintf(os.Stderr, "wrap error: %s", err)
160160
os.Exit(1)
161161
}
162162

163-
if exitStatus < 0 {
163+
if !done {
164164
if child {
165165
fmt.Printf("%v", Wrapped(nil))
166166
}
@@ -181,7 +181,7 @@ func TestHelperProcess(*testing.T) {
181181
os.Exit(0)
182182
}
183183

184-
exitCode, err := Wrap(config)
184+
_, exitCode, err := Wrap(config)
185185
if err != nil {
186186
fmt.Fprintf(os.Stderr, "wrap error: %s", err)
187187
os.Exit(1)

0 commit comments

Comments
 (0)