Thanks to visit codestin.com
Credit goes to github.com

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

Commit 320c906

Browse files
committed
Improve shell disconnect error
1 parent 90dfe59 commit 320c906

File tree

2 files changed

+43
-13
lines changed

2 files changed

+43
-13
lines changed

coder-sdk/env.go

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,16 +76,28 @@ type CreateEnvironmentRequest struct {
7676
Services []string `json:"services"`
7777
}
7878

79+
// EnvironmentByName gets a single environment environment for the authenticted user by name.
80+
func (c Client) EnvironmentByName(ctx context.Context, name string) (*Environment, error) {
81+
var env Environment
82+
err := c.requestBody(
83+
ctx,
84+
http.MethodGet, "/api/environments/"+name,
85+
nil,
86+
&env,
87+
)
88+
return &env, err
89+
}
90+
7991
// CreateEnvironment sends a request to create an environment.
8092
func (c Client) CreateEnvironment(ctx context.Context, orgID string, req CreateEnvironmentRequest) (*Environment, error) {
81-
var env *Environment
93+
var env Environment
8294
err := c.requestBody(
8395
ctx,
8496
http.MethodPost, "/api/orgs/"+orgID+"/environments",
8597
req,
86-
env,
98+
&env,
8799
)
88-
return env, err
100+
return &env, err
89101
}
90102

91103
// EnvironmentsByOrganization gets the list of environments owned by the given user.

internal/cmd/shell.go

Lines changed: 28 additions & 10 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"
@@ -142,11 +143,7 @@ func runCommand(ctx context.Context, envName string, command string, args []stri
142143
Env: cmdEnv,
143144
})
144145
if err != nil {
145-
var closeErr websocket.CloseError
146-
if xerrors.As(err, &closeErr) {
147-
return xerrors.Errorf("network error, is %q online?", envName)
148-
}
149-
return err
146+
return prettyShellExitError(ctx, err, entClient, env.Name)
150147
}
151148

152149
if tty {
@@ -178,15 +175,36 @@ func runCommand(ctx context.Context, envName string, command string, args []stri
178175
}()
179176
err = process.Wait()
180177
if err != nil {
181-
var closeErr websocket.CloseError
182-
if xerrors.Is(err, ctx.Err()) || xerrors.As(err, &closeErr) {
183-
return xerrors.Errorf("network error, is %q online?", envName)
184-
}
185-
return err
178+
return prettyShellExitError(ctx, err, entClient, env.Name)
186179
}
187180
return nil
188181
}
189182

183+
func prettyShellExitError(ctx context.Context, err error, client *coder.Client, envName string) error {
184+
var closeErr websocket.CloseError
185+
if xerrors.Is(err, ctx.Err()) || xerrors.As(err, &closeErr) {
186+
if err != nil {
187+
return xerrors.Errorf("network error, %s", envErrorMessage(ctx, client, envName))
188+
}
189+
}
190+
return err
191+
}
192+
193+
func envErrorMessage(ctx context.Context, client *coder.Client, envName string) string {
194+
env, err := client.EnvironmentByName(ctx, envName)
195+
if err != nil {
196+
return fmt.Sprintf("failed to fetch environment status: %v", err)
197+
}
198+
if env.LatestStat.ContainerStatus == "" {
199+
return "environment status not found"
200+
}
201+
if env.LatestStat.ContainerStatus == coder.EnvironmentOn {
202+
return fmt.Sprintf(`environment status is "%s", please try again`, env.LatestStat.ContainerStatus)
203+
}
204+
205+
return fmt.Sprintf(`environment "%s" has a status of "%s"`, envName, env.LatestStat.ContainerStatus)
206+
}
207+
190208
func heartbeat(ctx context.Context, c *websocket.Conn, interval time.Duration) {
191209
ticker := time.NewTicker(interval)
192210
defer ticker.Stop()

0 commit comments

Comments
 (0)