Skip to content

Commit 3311c2f

Browse files
refactor: replace Code by Detail in the http API error (coder#1011)
1 parent 9faa39a commit 3311c2f

File tree

6 files changed

+18
-18
lines changed

6 files changed

+18
-18
lines changed

coderd/httpapi/httpapi.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,8 @@ type Response struct {
5858

5959
// Error represents a scoped error to a user input.
6060
type Error struct {
61-
Field string `json:"field" validate:"required"`
62-
Code string `json:"code" validate:"required"`
61+
Field string `json:"field" validate:"required"`
62+
Detail string `json:"detail" validate:"required"`
6363
}
6464

6565
// Write outputs a standardized format to an HTTP response body.
@@ -97,8 +97,8 @@ func Read(rw http.ResponseWriter, r *http.Request, value interface{}) bool {
9797
apiErrors := make([]Error, 0, len(validationErrors))
9898
for _, validationError := range validationErrors {
9999
apiErrors = append(apiErrors, Error{
100-
Field: validationError.Field(),
101-
Code: validationError.Tag(),
100+
Field: validationError.Field(),
101+
Detail: validationError.Tag(),
102102
})
103103
}
104104
Write(rw, http.StatusBadRequest, Response{

coderd/httpapi/httpapi_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ func TestRead(t *testing.T) {
7676
require.NoError(t, err)
7777
require.Len(t, v.Errors, 1)
7878
require.Equal(t, v.Errors[0].Field, "value")
79-
require.Equal(t, v.Errors[0].Code, "required")
79+
require.Equal(t, v.Errors[0].Detail, "required")
8080
})
8181
}
8282

coderd/organizations.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -162,8 +162,8 @@ func (api *api) postTemplatesByOrganization(rw http.ResponseWriter, r *http.Requ
162162
httpapi.Write(rw, http.StatusConflict, httpapi.Response{
163163
Message: fmt.Sprintf("template %q already exists", createTemplate.Name),
164164
Errors: []httpapi.Error{{
165-
Field: "name",
166-
Code: "exists",
165+
Field: "name",
166+
Detail: "this value is already in use and should be unique",
167167
}},
168168
})
169169
return

coderd/users.go

+8-8
Original file line numberDiff line numberDiff line change
@@ -289,14 +289,14 @@ func (api *api) putUserProfile(rw http.ResponseWriter, r *http.Request) {
289289
responseErrors := []httpapi.Error{}
290290
if existentUser.Email == params.Email {
291291
responseErrors = append(responseErrors, httpapi.Error{
292-
Field: "email",
293-
Code: "exists",
292+
Field: "email",
293+
Detail: "this value is already in use and should be unique",
294294
})
295295
}
296296
if existentUser.Username == params.Username {
297297
responseErrors = append(responseErrors, httpapi.Error{
298-
Field: "username",
299-
Code: "exists",
298+
Field: "username",
299+
Detail: "this value is already in use and should be unique",
300300
})
301301
}
302302
httpapi.Write(rw, http.StatusConflict, httpapi.Response{
@@ -591,8 +591,8 @@ func (api *api) postWorkspacesByUser(rw http.ResponseWriter, r *http.Request) {
591591
httpapi.Write(rw, http.StatusBadRequest, httpapi.Response{
592592
Message: fmt.Sprintf("template %q doesn't exist", createWorkspace.TemplateID.String()),
593593
Errors: []httpapi.Error{{
594-
Field: "template_id",
595-
Code: "not_found",
594+
Field: "template_id",
595+
Detail: "template not found",
596596
}},
597597
})
598598
return
@@ -637,8 +637,8 @@ func (api *api) postWorkspacesByUser(rw http.ResponseWriter, r *http.Request) {
637637
httpapi.Write(rw, http.StatusConflict, httpapi.Response{
638638
Message: fmt.Sprintf("workspace %q already exists in the %q template", createWorkspace.Name, template.Name),
639639
Errors: []httpapi.Error{{
640-
Field: "name",
641-
Code: "exists",
640+
Field: "name",
641+
Detail: "this value is already in use and should be unique",
642642
}},
643643
})
644644
return

coderd/workspaces.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,8 @@ func (api *api) postWorkspaceBuilds(rw http.ResponseWriter, r *http.Request) {
115115
httpapi.Write(rw, http.StatusBadRequest, httpapi.Response{
116116
Message: "template version not found",
117117
Errors: []httpapi.Error{{
118-
Field: "template_version_id",
119-
Code: "exists",
118+
Field: "template_version_id",
119+
Detail: "template version not found",
120120
}},
121121
})
122122
return

codersdk/client.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ func (e *Error) Error() string {
127127
var builder strings.Builder
128128
_, _ = fmt.Fprintf(&builder, "status code %d: %s", e.statusCode, e.Message)
129129
for _, err := range e.Errors {
130-
_, _ = fmt.Fprintf(&builder, "\n\t%s: %s", err.Field, err.Code)
130+
_, _ = fmt.Fprintf(&builder, "\n\t%s: %s", err.Field, err.Detail)
131131
}
132132
return builder.String()
133133
}

0 commit comments

Comments
 (0)