-
Notifications
You must be signed in to change notification settings - Fork 928
chore: create interface for pkgs to return codersdk errors #18719
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
package dynamicparameters | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
"sort" | ||
|
||
"github.com/hashicorp/hcl/v2" | ||
|
||
"github.com/coder/coder/v2/codersdk" | ||
) | ||
|
||
func ParameterValidationError(diags hcl.Diagnostics) *DiagnosticError { | ||
return &DiagnosticError{ | ||
Message: "Unable to validate parameters", | ||
Diagnostics: diags, | ||
KeyedDiagnostics: make(map[string]hcl.Diagnostics), | ||
} | ||
} | ||
|
||
func TagValidationError(diags hcl.Diagnostics) *DiagnosticError { | ||
return &DiagnosticError{ | ||
Message: "Failed to parse workspace tags", | ||
Diagnostics: diags, | ||
KeyedDiagnostics: make(map[string]hcl.Diagnostics), | ||
} | ||
} | ||
|
||
type DiagnosticError struct { | ||
Message string | ||
Diagnostics hcl.Diagnostics | ||
KeyedDiagnostics map[string]hcl.Diagnostics | ||
} | ||
|
||
// Error is a pretty bad format for these errors. Try to avoid using this. | ||
func (e *DiagnosticError) Error() string { | ||
var diags hcl.Diagnostics | ||
diags = diags.Extend(e.Diagnostics) | ||
for _, d := range e.KeyedDiagnostics { | ||
diags = diags.Extend(d) | ||
} | ||
|
||
return diags.Error() | ||
} | ||
|
||
func (e *DiagnosticError) HasError() bool { | ||
if e.Diagnostics.HasErrors() { | ||
return true | ||
} | ||
|
||
for _, diags := range e.KeyedDiagnostics { | ||
if diags.HasErrors() { | ||
return true | ||
} | ||
} | ||
return false | ||
} | ||
|
||
func (e *DiagnosticError) Append(key string, diag *hcl.Diagnostic) { | ||
e.Extend(key, hcl.Diagnostics{diag}) | ||
} | ||
|
||
func (e *DiagnosticError) Extend(key string, diag hcl.Diagnostics) { | ||
if e.KeyedDiagnostics == nil { | ||
e.KeyedDiagnostics = make(map[string]hcl.Diagnostics) | ||
} | ||
if _, ok := e.KeyedDiagnostics[key]; !ok { | ||
e.KeyedDiagnostics[key] = hcl.Diagnostics{} | ||
} | ||
e.KeyedDiagnostics[key] = e.KeyedDiagnostics[key].Extend(diag) | ||
} | ||
Comment on lines
+59
to
+71
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit/quibble: Similarly here, when should you use
|
||
|
||
func (e *DiagnosticError) Response() (int, codersdk.Response) { | ||
resp := codersdk.Response{ | ||
Message: e.Message, | ||
Validations: nil, | ||
} | ||
|
||
// Sort the parameter names so that the order is consistent. | ||
sortedNames := make([]string, 0, len(e.KeyedDiagnostics)) | ||
for name := range e.KeyedDiagnostics { | ||
sortedNames = append(sortedNames, name) | ||
} | ||
sort.Strings(sortedNames) | ||
|
||
for _, name := range sortedNames { | ||
diag := e.KeyedDiagnostics[name] | ||
resp.Validations = append(resp.Validations, codersdk.ValidationError{ | ||
Field: name, | ||
Detail: DiagnosticsErrorString(diag), | ||
}) | ||
} | ||
|
||
if e.Diagnostics.HasErrors() { | ||
resp.Detail = DiagnosticsErrorString(e.Diagnostics) | ||
} | ||
|
||
return http.StatusBadRequest, resp | ||
} | ||
|
||
func DiagnosticErrorString(d *hcl.Diagnostic) string { | ||
return fmt.Sprintf("%s; %s", d.Summary, d.Detail) | ||
} | ||
|
||
func DiagnosticsErrorString(d hcl.Diagnostics) string { | ||
count := len(d) | ||
switch { | ||
case count == 0: | ||
return "no diagnostics" | ||
case count == 1: | ||
return DiagnosticErrorString(d[0]) | ||
default: | ||
for _, d := range d { | ||
// Render the first error diag. | ||
// If there are warnings, do not priority them over errors. | ||
if d.Severity == hcl.DiagError { | ||
return fmt.Sprintf("%s, and %d other diagnostic(s)", DiagnosticErrorString(d), count-1) | ||
} | ||
} | ||
|
||
// All warnings? ok... | ||
return fmt.Sprintf("%s, and %d other diagnostic(s)", DiagnosticErrorString(d[0]), count-1) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package httperror | ||
|
||
import ( | ||
"errors" | ||
|
||
"github.com/coder/coder/v2/codersdk" | ||
) | ||
|
||
type CoderSDKError interface { | ||
Response() (int, codersdk.Response) | ||
} | ||
Comment on lines
+9
to
+11
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: should probably be named |
||
|
||
func IsCoderSDKError(err error) (CoderSDKError, bool) { | ||
var responseErr CoderSDKError | ||
if errors.As(err, &responseErr) { | ||
return responseErr, true | ||
} | ||
return nil, false | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: What's the difference between
Diagnostics
andKeyedDiagnostics
?When should you use one or the other?