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

Commit 6a97c3b

Browse files
committed
Properly handle wush error messages
1 parent 7744252 commit 6a97c3b

File tree

2 files changed

+38
-11
lines changed

2 files changed

+38
-11
lines changed

Diff for: cmd/coder/shell.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ func (cmd *shellCmd) Run(fl *pflag.FlagSet) {
9393

9494
exitCode, err := runCommand(envName, command, args)
9595
if err != nil {
96-
flog.Fatal("run command: %v", err)
96+
flog.Fatal("run command: %v Is it online?", err)
9797
}
9898
os.Exit(exitCode)
9999
}

Diff for: wush/client.go

+37-10
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@ package wush
33
import (
44
"context"
55
"encoding/base64"
6-
"fmt"
6+
"encoding/json"
77
"io"
8+
"io/ioutil"
89

910
"golang.org/x/sync/errgroup"
1011
"golang.org/x/xerrors"
@@ -15,11 +16,11 @@ import (
1516
// Client converts a Wush connection into OS streams.
1617
type Client struct {
1718
statusPromise promise
18-
exitCode uint8
19-
err error
19+
exitCode uint8
20+
err error
2021

2122
conn *websocket.Conn
22-
ctx context.Context
23+
ctx context.Context
2324

2425
Stdin io.WriteCloser
2526
Stdout io.Reader
@@ -89,17 +90,16 @@ func NewClient(ctx context.Context, conn *websocket.Conn) *Client {
8990
eg, ctx := errgroup.WithContext(ctx)
9091

9192
c := &Client{
92-
Stdout: stdoutReader,
93-
Stderr: stderrReader,
94-
conn: conn,
95-
ctx: ctx,
93+
Stdout: stdoutReader,
94+
Stderr: stderrReader,
95+
conn: conn,
96+
ctx: ctx,
9697
statusPromise: newPromise(),
9798
}
9899
c.Stdin = &stdinWriter{
99100
Client: c,
100101
}
101102

102-
103103
// We expect massive reads from some commands. Because we're streaming it's no big deal.
104104
conn.SetReadLimit(1 << 40)
105105

@@ -141,7 +141,17 @@ func NewClient(ctx context.Context, conn *websocket.Conn) *Client {
141141
exitCode <- uint8(exitCodeBuf[0])
142142
return nil
143143
default:
144-
return fmt.Errorf("unexpected id %x", streamID[0])
144+
// This probably means an error was returned.
145+
errResp, err := ioutil.ReadAll(rdr)
146+
if err != nil {
147+
return xerrors.Errorf("read error msg: %w", err)
148+
}
149+
150+
// Since we read the first byte to check the
151+
// stream id, prepend it to the rest of the
152+
// data.
153+
errResp = append([]byte{streamID[0]}, errResp...)
154+
return handleStreamError(errResp)
145155
}
146156
}
147157
})
@@ -161,6 +171,23 @@ func NewClient(ctx context.Context, conn *websocket.Conn) *Client {
161171
return c
162172
}
163173

174+
func handleStreamError(body []byte) error {
175+
res := struct {
176+
Error struct {
177+
Msg string `json:"msg"`
178+
Verbose string `json:"verbose"`
179+
} `json:"error"`
180+
}{}
181+
182+
err := json.Unmarshal(body, &res)
183+
if err != nil {
184+
// If it's not a JSON error just print response verbatim.
185+
return xerrors.Errorf("unknown stream error: %s", string(body))
186+
}
187+
188+
return xerrors.Errorf(res.Error.Msg)
189+
}
190+
164191
// Wait returns the status code of the command, along
165192
// with any error.
166193
func (c *Client) Wait() (uint8, error) {

0 commit comments

Comments
 (0)