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

Commit 649b468

Browse files
committed
Handle verbose errors
1 parent 417cdbd commit 649b468

File tree

2 files changed

+54
-46
lines changed

2 files changed

+54
-46
lines changed

internal/cmd/envs.go

Lines changed: 2 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,7 @@ coder envs create-from-config -f coder.yaml`,
355355

356356
tpl, err := client.ParseTemplate(ctx, req)
357357
if err != nil {
358-
return handleTemplateError(err)
358+
return handleAPIError(err)
359359
}
360360

361361
provider, err := coderutil.DefaultWorkspaceProvider(ctx, client)
@@ -370,7 +370,7 @@ coder envs create-from-config -f coder.yaml`,
370370
Namespace: provider.DefaultNamespace,
371371
})
372372
if err != nil {
373-
return xerrors.Errorf("create environment: %w", err)
373+
return handleAPIError(err)
374374
}
375375

376376
if follow {
@@ -397,50 +397,6 @@ coder envs create-from-config -f coder.yaml`,
397397
return cmd
398398
}
399399

400-
// handleTemplateError attempts to convert the basic error into a more detailed clog error.
401-
func handleTemplateError(origError error) error {
402-
var httpError *coder.HTTPError
403-
if !xerrors.As(origError, &httpError) {
404-
return origError // Return the original
405-
}
406-
407-
ae, err := httpError.Payload()
408-
if err != nil {
409-
return origError // Return the original
410-
}
411-
412-
// TODO: Handle verbose case here too?
413-
switch ae.Err.Code {
414-
case "wac_template":
415-
type templatePayload struct {
416-
ErrorType string `json:"error_type"`
417-
Msgs []string `json:"messages"`
418-
}
419-
420-
var p templatePayload
421-
err := json.Unmarshal(ae.Err.Details, &p)
422-
if err != nil {
423-
return origError
424-
}
425-
426-
return clog.Error(p.ErrorType, p.Msgs...)
427-
case "verbose":
428-
// TODO: We should move this to some general spot to decode this
429-
type verbosePayload struct {
430-
Verbose string `json:"verbose"`
431-
}
432-
var p verbosePayload
433-
err := json.Unmarshal(ae.Err.Details, &p)
434-
if err != nil {
435-
return origError
436-
}
437-
438-
return clog.Error(origError.Error(), p.Verbose)
439-
}
440-
441-
return origError // Return the original
442-
}
443-
444400
func editEnvCmd() *cobra.Command {
445401
var (
446402
org string

internal/cmd/errors.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package cmd
2+
3+
import (
4+
"encoding/json"
5+
6+
"cdr.dev/coder-cli/coder-sdk"
7+
"cdr.dev/coder-cli/pkg/clog"
8+
"golang.org/x/xerrors"
9+
)
10+
11+
// handleAPIError attempts to convert an api error into a more detailed clog error.
12+
// If it cannot, it will return the original error
13+
func handleAPIError(origError error) error {
14+
var httpError *coder.HTTPError
15+
if !xerrors.As(origError, &httpError) {
16+
return origError // Return the original
17+
}
18+
19+
ae, err := httpError.Payload()
20+
if err != nil {
21+
return origError // Return the original
22+
}
23+
24+
switch ae.Err.Code {
25+
case "wac_template": // template parse errors
26+
type templatePayload struct {
27+
ErrorType string `json:"error_type"`
28+
Msgs []string `json:"messages"`
29+
}
30+
31+
var p templatePayload
32+
err := json.Unmarshal(ae.Err.Details, &p)
33+
if err != nil {
34+
return origError
35+
}
36+
37+
return clog.Error(p.ErrorType, p.Msgs...)
38+
case "verbose":
39+
type verbosePayload struct {
40+
Verbose string `json:"verbose"`
41+
}
42+
var p verbosePayload
43+
err := json.Unmarshal(ae.Err.Details, &p)
44+
if err != nil {
45+
return origError
46+
}
47+
48+
return clog.Error(origError.Error(), p.Verbose)
49+
}
50+
51+
return origError // Return the original
52+
}

0 commit comments

Comments
 (0)