refactor: replace Code by Detail in the http API error (#1011)

This commit is contained in:
Bruno Quaresma
2022-04-18 11:02:54 -05:00
committed by GitHub
parent 9faa39aa23
commit 3311c2f65d
6 changed files with 18 additions and 18 deletions

View File

@ -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{

View File

@ -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")
})
}

View File

@ -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

View File

@ -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

View File

@ -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