Skip to content

Commit 5137433

Browse files
authored
chore: add validation errors to the cli output (coder#12814)
* chore: add validation errors to the cli output
1 parent 94e82f9 commit 5137433

File tree

3 files changed

+19
-2
lines changed

3 files changed

+19
-2
lines changed

cli/root.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1084,10 +1084,23 @@ func formatCoderSDKError(from string, err *codersdk.Error, opts *formatOpts) str
10841084
_, _ = str.WriteString("\n")
10851085
}
10861086

1087+
// The main error message
10871088
_, _ = str.WriteString(pretty.Sprint(headLineStyle(), err.Message))
1089+
1090+
// Validation errors.
1091+
if len(err.Validations) > 0 {
1092+
_, _ = str.WriteString("\n")
1093+
_, _ = str.WriteString(pretty.Sprint(tailLineStyle(), fmt.Sprintf("%d validation error(s) found", len(err.Validations))))
1094+
for _, e := range err.Validations {
1095+
_, _ = str.WriteString("\n\t")
1096+
_, _ = str.WriteString(pretty.Sprint(cliui.DefaultStyles.Field, e.Field))
1097+
_, _ = str.WriteString(pretty.Sprintf(cliui.DefaultStyles.Warn, ": %s", e.Detail))
1098+
}
1099+
}
1100+
10881101
if err.Helper != "" {
10891102
_, _ = str.WriteString("\n")
1090-
_, _ = str.WriteString(pretty.Sprint(tailLineStyle(), err.Helper))
1103+
_, _ = str.WriteString(pretty.Sprintf(tailLineStyle(), "Suggestion: %s", err.Helper))
10911104
}
10921105
// By default we do not show the Detail with the helper.
10931106
if opts.Verbose || (err.Helper == "" && err.Detail != "") {
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
Encountered an error running "coder exp example-error api", see "coder exp example-error api --help" for more information
22
error: Top level sdk error message.
3-
Have you tried turning it off and on again?
3+
1 validation error(s) found
4+
region : magic dust is not available in your region
5+
Suggestion: Have you tried turning it off and on again?

cli/testdata/coder_exp_example-error_multi-error.golden

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,6 @@ error: 3 errors encountered: Trace=[wrapped: ])
44
2. second error: function decided not to work, and it never will
55
3. Trace=[wrapped api error: ]
66
Top level sdk error message.
7+
1 validation error(s) found
8+
region : magic dust is not available in your region
79
magic dust unavailable, please try again later

0 commit comments

Comments
 (0)