mirror of
https://github.com/coder/coder.git
synced 2025-07-15 22:20:27 +00:00
chore: create interface for pkgs to return codersdk errors (#18719)
This interface allows it to create rich codersdk errors and pass them up to the `wsbuilder` error handling.
This commit is contained in:
@ -2,60 +2,17 @@ package httperror
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"sort"
|
||||
|
||||
"github.com/hashicorp/hcl/v2"
|
||||
|
||||
"github.com/coder/coder/v2/coderd/dynamicparameters"
|
||||
"github.com/coder/coder/v2/coderd/httpapi"
|
||||
"github.com/coder/coder/v2/coderd/wsbuilder"
|
||||
"github.com/coder/coder/v2/codersdk"
|
||||
)
|
||||
|
||||
func WriteWorkspaceBuildError(ctx context.Context, rw http.ResponseWriter, err error) {
|
||||
var buildErr wsbuilder.BuildError
|
||||
if errors.As(err, &buildErr) {
|
||||
if httpapi.IsUnauthorizedError(err) {
|
||||
buildErr.Status = http.StatusForbidden
|
||||
}
|
||||
if responseErr, ok := IsResponder(err); ok {
|
||||
code, resp := responseErr.Response()
|
||||
|
||||
httpapi.Write(ctx, rw, buildErr.Status, codersdk.Response{
|
||||
Message: buildErr.Message,
|
||||
Detail: buildErr.Error(),
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
var parameterErr *dynamicparameters.ResolverError
|
||||
if errors.As(err, ¶meterErr) {
|
||||
resp := codersdk.Response{
|
||||
Message: "Unable to validate parameters",
|
||||
Validations: nil,
|
||||
}
|
||||
|
||||
// Sort the parameter names so that the order is consistent.
|
||||
sortedNames := make([]string, 0, len(parameterErr.Parameter))
|
||||
for name := range parameterErr.Parameter {
|
||||
sortedNames = append(sortedNames, name)
|
||||
}
|
||||
sort.Strings(sortedNames)
|
||||
|
||||
for _, name := range sortedNames {
|
||||
diag := parameterErr.Parameter[name]
|
||||
resp.Validations = append(resp.Validations, codersdk.ValidationError{
|
||||
Field: name,
|
||||
Detail: DiagnosticsErrorString(diag),
|
||||
})
|
||||
}
|
||||
|
||||
if parameterErr.Diagnostics.HasErrors() {
|
||||
resp.Detail = DiagnosticsErrorString(parameterErr.Diagnostics)
|
||||
}
|
||||
|
||||
httpapi.Write(ctx, rw, http.StatusBadRequest, resp)
|
||||
httpapi.Write(ctx, rw, code, resp)
|
||||
return
|
||||
}
|
||||
|
||||
@ -64,28 +21,3 @@ func WriteWorkspaceBuildError(ctx context.Context, rw http.ResponseWriter, err e
|
||||
Detail: err.Error(),
|
||||
})
|
||||
}
|
||||
|
||||
func DiagnosticError(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 DiagnosticError(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)", DiagnosticError(d), count-1)
|
||||
}
|
||||
}
|
||||
|
||||
// All warnings? ok...
|
||||
return fmt.Sprintf("%s, and %d other diagnostic(s)", DiagnosticError(d[0]), count-1)
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user