From 49f0ffe3b153a1c47e873d2888504dffb6a4f997 Mon Sep 17 00:00:00 2001 From: Bruno Date: Thu, 14 Apr 2022 19:14:11 +0000 Subject: [PATCH 1/4] refactor: replace Code by Detail in the http API error --- coderd/httpapi/httpapi.go | 8 ++++---- coderd/organizations.go | 4 ++-- coderd/users.go | 18 +++++++++--------- coderd/workspaces.go | 4 ++-- 4 files changed, 17 insertions(+), 17 deletions(-) diff --git a/coderd/httpapi/httpapi.go b/coderd/httpapi/httpapi.go index 9bdbf59c73c86..331ec527e4328 100644 --- a/coderd/httpapi/httpapi.go +++ b/coderd/httpapi/httpapi.go @@ -58,8 +58,8 @@ type Response struct { // Error represents a scoped error to a user input. type Error struct { - Field string `json:"field" validate:"required"` - Code string `json:"code" validate:"required"` + Field string `json:"field" validate:"required"` + Detail string `json:"detail" validate:"required"` } // 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 { apiErrors := make([]Error, 0, len(validationErrors)) for _, validationError := range validationErrors { apiErrors = append(apiErrors, Error{ - Field: validationError.Field(), - Code: validationError.Tag(), + Field: validationError.Field(), + Detail: validationError.Tag(), }) } Write(rw, http.StatusBadRequest, Response{ diff --git a/coderd/organizations.go b/coderd/organizations.go index 1b8790a67a85f..c6a944ed89dc4 100644 --- a/coderd/organizations.go +++ b/coderd/organizations.go @@ -162,8 +162,8 @@ func (api *api) postTemplatesByOrganization(rw http.ResponseWriter, r *http.Requ httpapi.Write(rw, http.StatusConflict, httpapi.Response{ Message: fmt.Sprintf("template %q already exists", createTemplate.Name), Errors: []httpapi.Error{{ - Field: "name", - Code: "exists", + Field: "name", + Detail: "this value is already in use and should be unique", }}, }) return diff --git a/coderd/users.go b/coderd/users.go index 6fcab0814033f..5198703c161d3 100644 --- a/coderd/users.go +++ b/coderd/users.go @@ -45,7 +45,7 @@ func (api *api) firstUser(rw http.ResponseWriter, r *http.Request) { }) } -// Creates the initial user for a Coder deployment. +// Creates the initial user for a Detailr deployment. func (api *api) postFirstUser(rw http.ResponseWriter, r *http.Request) { var createUser codersdk.CreateFirstUserRequest if !httpapi.Read(rw, r, &createUser) { @@ -289,14 +289,14 @@ func (api *api) putUserProfile(rw http.ResponseWriter, r *http.Request) { responseErrors := []httpapi.Error{} if existentUser.Email == params.Email { responseErrors = append(responseErrors, httpapi.Error{ - Field: "email", - Code: "exists", + Field: "email", + Detail: "this value is already in use and should be unique", }) } if existentUser.Username == params.Username { responseErrors = append(responseErrors, httpapi.Error{ - Field: "username", - Code: "exists", + Field: "username", + Detail: "this value is already in use and should be unique", }) } httpapi.Write(rw, http.StatusConflict, httpapi.Response{ @@ -591,8 +591,8 @@ func (api *api) postWorkspacesByUser(rw http.ResponseWriter, r *http.Request) { httpapi.Write(rw, http.StatusBadRequest, httpapi.Response{ Message: fmt.Sprintf("template %q doesn't exist", createWorkspace.TemplateID.String()), Errors: []httpapi.Error{{ - Field: "template_id", - Code: "not_found", + Field: "template_id", + Detail: "template not found", }}, }) return @@ -637,8 +637,8 @@ func (api *api) postWorkspacesByUser(rw http.ResponseWriter, r *http.Request) { httpapi.Write(rw, http.StatusConflict, httpapi.Response{ Message: fmt.Sprintf("workspace %q already exists in the %q template", createWorkspace.Name, template.Name), Errors: []httpapi.Error{{ - Field: "name", - Code: "exists", + Field: "name", + Detail: "this value is already in use and should be unique", }}, }) return diff --git a/coderd/workspaces.go b/coderd/workspaces.go index 19504051812ba..0ae2e0524772b 100644 --- a/coderd/workspaces.go +++ b/coderd/workspaces.go @@ -115,8 +115,8 @@ func (api *api) postWorkspaceBuilds(rw http.ResponseWriter, r *http.Request) { httpapi.Write(rw, http.StatusBadRequest, httpapi.Response{ Message: "template version not found", Errors: []httpapi.Error{{ - Field: "template_version_id", - Code: "exists", + Field: "template_version_id", + Detail: "template version not found", }}, }) return From 55bf0286e629c28c3644c1ff2c956de38cd50349 Mon Sep 17 00:00:00 2001 From: Bruno Date: Thu, 14 Apr 2022 19:16:49 +0000 Subject: [PATCH 2/4] fix: http api test --- coderd/httpapi/httpapi_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/coderd/httpapi/httpapi_test.go b/coderd/httpapi/httpapi_test.go index 0e7e45881a2bd..ad251747aa604 100644 --- a/coderd/httpapi/httpapi_test.go +++ b/coderd/httpapi/httpapi_test.go @@ -76,7 +76,7 @@ func TestRead(t *testing.T) { require.NoError(t, err) require.Len(t, v.Errors, 1) require.Equal(t, v.Errors[0].Field, "value") - require.Equal(t, v.Errors[0].Code, "required") + require.Equal(t, v.Errors[0].Detail, "required") }) } From c4162dfc21b8dc456e53f3de65fc1bc3f0e29abf Mon Sep 17 00:00:00 2001 From: Bruno Quaresma Date: Thu, 14 Apr 2022 16:35:56 -0300 Subject: [PATCH 3/4] Update coderd/users.go Co-authored-by: G r e y --- coderd/users.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/coderd/users.go b/coderd/users.go index 5198703c161d3..bffc6751223ff 100644 --- a/coderd/users.go +++ b/coderd/users.go @@ -45,7 +45,7 @@ func (api *api) firstUser(rw http.ResponseWriter, r *http.Request) { }) } -// Creates the initial user for a Detailr deployment. +// Creates the initial user for a Coder deployment. func (api *api) postFirstUser(rw http.ResponseWriter, r *http.Request) { var createUser codersdk.CreateFirstUserRequest if !httpapi.Read(rw, r, &createUser) { From 5015f1c640e61d58f00a908063445a81c53277d2 Mon Sep 17 00:00:00 2001 From: Bruno Date: Fri, 15 Apr 2022 14:14:06 +0000 Subject: [PATCH 4/4] fix: replace Code by Detail --- codersdk/client.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/codersdk/client.go b/codersdk/client.go index 2c4d320628ab3..0c6f1986d664c 100644 --- a/codersdk/client.go +++ b/codersdk/client.go @@ -127,7 +127,7 @@ func (e *Error) Error() string { var builder strings.Builder _, _ = fmt.Fprintf(&builder, "status code %d: %s", e.statusCode, e.Message) for _, err := range e.Errors { - _, _ = fmt.Fprintf(&builder, "\n\t%s: %s", err.Field, err.Code) + _, _ = fmt.Fprintf(&builder, "\n\t%s: %s", err.Field, err.Detail) } return builder.String() }