| 84 | } |
| 85 | |
| 86 | func (e *DiagnosticError) Response() (int, codersdk.Response) { |
| 87 | resp := codersdk.Response{ |
| 88 | Message: e.Message, |
| 89 | Validations: nil, |
| 90 | } |
| 91 | |
| 92 | // Sort the parameter names so that the order is consistent. |
| 93 | sortedNames := make([]string, 0, len(e.KeyedDiagnostics)) |
| 94 | for name := range e.KeyedDiagnostics { |
| 95 | sortedNames = append(sortedNames, name) |
| 96 | } |
| 97 | slices.Sort(sortedNames) |
| 98 | |
| 99 | for _, name := range sortedNames { |
| 100 | diag := e.KeyedDiagnostics[name] |
| 101 | resp.Validations = append(resp.Validations, codersdk.ValidationError{ |
| 102 | Field: name, |
| 103 | Detail: DiagnosticsErrorString(diag), |
| 104 | }) |
| 105 | } |
| 106 | |
| 107 | if e.Diagnostics.HasErrors() { |
| 108 | resp.Detail = DiagnosticsErrorString(e.Diagnostics) |
| 109 | } |
| 110 | |
| 111 | return http.StatusBadRequest, resp |
| 112 | } |
| 113 | |
| 114 | func DiagnosticErrorString(d *hcl.Diagnostic) string { |
| 115 | return fmt.Sprintf("%s; %s", d.Summary, d.Detail) |