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

Commit e8f622d

Browse files
committed
Add rich error message for sh into off environment
1 parent 6ef3b24 commit e8f622d

File tree

1 file changed

+17
-3
lines changed

1 file changed

+17
-3
lines changed

internal/cmd/shell.go

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package cmd
22

33
import (
44
"context"
5+
"fmt"
56
"io"
67
"os"
78
"strings"
@@ -15,6 +16,7 @@ import (
1516

1617
"cdr.dev/coder-cli/coder-sdk"
1718
"cdr.dev/coder-cli/internal/activity"
19+
"cdr.dev/coder-cli/internal/clog"
1820
"cdr.dev/coder-cli/internal/x/xterminal"
1921
"cdr.dev/wsep"
2022

@@ -153,7 +155,7 @@ func runCommand(ctx context.Context, envName, command string, args []string) err
153155
if err != nil {
154156
var closeErr websocket.CloseError
155157
if xerrors.As(err, &closeErr) {
156-
return xerrors.Errorf("network error, is %q online?", envName)
158+
return networkErr(client, env)
157159
}
158160
return xerrors.Errorf("start remote command: %w", err)
159161
}
@@ -179,7 +181,6 @@ func runCommand(ctx context.Context, envName, command string, args []string) err
179181
}
180182
}()
181183
go func() {
182-
183184
if _, err := io.Copy(os.Stderr, process.Stderr()); err != nil {
184185
cancel()
185186
}
@@ -188,13 +189,26 @@ func runCommand(ctx context.Context, envName, command string, args []string) err
188189
if err := process.Wait(); err != nil {
189190
var closeErr websocket.CloseError
190191
if xerrors.Is(err, ctx.Err()) || xerrors.As(err, &closeErr) {
191-
return xerrors.Errorf("network error, is %q online?", envName)
192+
return networkErr(client, env)
192193
}
193194
return err
194195
}
195196
return nil
196197
}
197198

199+
func networkErr(client *coder.Client, env *coder.Environment) error {
200+
if env.LatestStat.ContainerStatus != coder.EnvironmentOn {
201+
return clog.Fatal(
202+
"environment is not running",
203+
fmt.Sprintf("environment %q is not running", env.Name),
204+
fmt.Sprintf("its current status is %q",env.LatestStat.ContainerStatus),
205+
"",
206+
clog.Bold("tip: ")+fmt.Sprintf("run \"coder envs rebuild %s --follow\" to start the environment", env.Name),
207+
)
208+
}
209+
return xerrors.Errorf("network error, is %q online?", env.Name)
210+
}
211+
198212
func heartbeat(ctx context.Context, conn *websocket.Conn, interval time.Duration) {
199213
ticker := time.NewTicker(interval)
200214
defer ticker.Stop()

0 commit comments

Comments
 (0)