Commit 451a1fa4 authored by Russ Cox's avatar Russ Cox

exec: introduce ExitError

The existing code uses *os.Waitmsg as an os.Error,
but *os.Waitmsg is really just a stringer.

Introduce an explicit error type for the real error.

Not to be submitted until just before error goes in;
the gofix for error updates type assertions
        err.(*os.Waitmsg)
to
        err.(*exec.ExitError)

The seemingly redundant String method will become
an Error method when error goes in, and will no longer
be redundant.

R=golang-dev, bradfitz
CC=golang-dev
https://golang.org/cl/5331044
parent c93b6a17
...@@ -203,7 +203,7 @@ func (c *Cmd) writerDescriptor(w io.Writer) (f *os.File, err os.Error) { ...@@ -203,7 +203,7 @@ func (c *Cmd) writerDescriptor(w io.Writer) (f *os.File, err os.Error) {
// status. // status.
// //
// If the command fails to run or doesn't complete successfully, the // If the command fails to run or doesn't complete successfully, the
// error is of type *os.Waitmsg. Other error types may be // error is of type *ExitError. Other error types may be
// returned for I/O problems. // returned for I/O problems.
func (c *Cmd) Run() os.Error { func (c *Cmd) Run() os.Error {
if err := c.Start(); err != nil { if err := c.Start(); err != nil {
...@@ -256,6 +256,15 @@ func (c *Cmd) Start() os.Error { ...@@ -256,6 +256,15 @@ func (c *Cmd) Start() os.Error {
return nil return nil
} }
// An ExitError reports an unsuccessful exit by a command.
type ExitError struct {
*os.Waitmsg
}
func (e *ExitError) String() string {
return e.Waitmsg.String()
}
// Wait waits for the command to exit. // Wait waits for the command to exit.
// It must have been started by Start. // It must have been started by Start.
// //
...@@ -264,7 +273,7 @@ func (c *Cmd) Start() os.Error { ...@@ -264,7 +273,7 @@ func (c *Cmd) Start() os.Error {
// status. // status.
// //
// If the command fails to run or doesn't complete successfully, the // If the command fails to run or doesn't complete successfully, the
// error is of type *os.Waitmsg. Other error types may be // error is of type *ExitError. Other error types may be
// returned for I/O problems. // returned for I/O problems.
func (c *Cmd) Wait() os.Error { func (c *Cmd) Wait() os.Error {
if c.Process == nil { if c.Process == nil {
...@@ -290,7 +299,7 @@ func (c *Cmd) Wait() os.Error { ...@@ -290,7 +299,7 @@ func (c *Cmd) Wait() os.Error {
if err != nil { if err != nil {
return err return err
} else if !msg.Exited() || msg.ExitStatus() != 0 { } else if !msg.Exited() || msg.ExitStatus() != 0 {
return msg return &ExitError{msg}
} }
return copyError return copyError
......
...@@ -53,8 +53,8 @@ func TestCatStdin(t *testing.T) { ...@@ -53,8 +53,8 @@ func TestCatStdin(t *testing.T) {
func TestCatGoodAndBadFile(t *testing.T) { func TestCatGoodAndBadFile(t *testing.T) {
// Testing combined output and error values. // Testing combined output and error values.
bs, err := helperCommand("cat", "/bogus/file.foo", "exec_test.go").CombinedOutput() bs, err := helperCommand("cat", "/bogus/file.foo", "exec_test.go").CombinedOutput()
if _, ok := err.(*os.Waitmsg); !ok { if _, ok := err.(*ExitError); !ok {
t.Errorf("expected Waitmsg from cat combined; got %T: %v", err, err) t.Errorf("expected *ExitError from cat combined; got %T: %v", err, err)
} }
s := string(bs) s := string(bs)
sp := strings.SplitN(s, "\n", 2) sp := strings.SplitN(s, "\n", 2)
...@@ -81,12 +81,12 @@ func TestNoExistBinary(t *testing.T) { ...@@ -81,12 +81,12 @@ func TestNoExistBinary(t *testing.T) {
func TestExitStatus(t *testing.T) { func TestExitStatus(t *testing.T) {
// Test that exit values are returned correctly // Test that exit values are returned correctly
err := helperCommand("exit", "42").Run() err := helperCommand("exit", "42").Run()
if werr, ok := err.(*os.Waitmsg); ok { if werr, ok := err.(*ExitError); ok {
if s, e := werr.String(), "exit status 42"; s != e { if s, e := werr.String(), "exit status 42"; s != e {
t.Errorf("from exit 42 got exit %q, want %q", s, e) t.Errorf("from exit 42 got exit %q, want %q", s, e)
} }
} else { } else {
t.Fatalf("expected Waitmsg from exit 42; got %T: %v", err, err) t.Fatalf("expected *ExitError from exit 42; got %T: %v", err, err)
} }
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment