mirror of
https://github.com/coder/coder.git
synced 2026-09-24 15:04:27 +08:00
chore: Update BE http errors to be ui friendly (#1994)
* chore: More UI friendly errors Mainly capitlization + messages prefix error
This commit is contained in:
+64
-35
@@ -30,7 +30,8 @@ func (api *API) firstUser(rw http.ResponseWriter, r *http.Request) {
|
||||
userCount, err := api.Database.GetUserCount(r.Context())
|
||||
if err != nil {
|
||||
httpapi.Write(rw, http.StatusInternalServerError, httpapi.Response{
|
||||
Message: fmt.Sprintf("get user count: %s", err.Error()),
|
||||
Message: "Internal error fetching user count",
|
||||
Detail: err.Error(),
|
||||
})
|
||||
return
|
||||
}
|
||||
@@ -58,7 +59,8 @@ func (api *API) postFirstUser(rw http.ResponseWriter, r *http.Request) {
|
||||
userCount, err := api.Database.GetUserCount(r.Context())
|
||||
if err != nil {
|
||||
httpapi.Write(rw, http.StatusInternalServerError, httpapi.Response{
|
||||
Message: fmt.Sprintf("get user count: %s", err.Error()),
|
||||
Message: "Internal error fetching user count",
|
||||
Detail: err.Error(),
|
||||
})
|
||||
return
|
||||
}
|
||||
@@ -66,7 +68,7 @@ func (api *API) postFirstUser(rw http.ResponseWriter, r *http.Request) {
|
||||
// If a user already exists, the initial admin user no longer can be created.
|
||||
if userCount != 0 {
|
||||
httpapi.Write(rw, http.StatusConflict, httpapi.Response{
|
||||
Message: "the initial user has already been created",
|
||||
Message: "The initial user has already been created",
|
||||
})
|
||||
return
|
||||
}
|
||||
@@ -78,7 +80,8 @@ func (api *API) postFirstUser(rw http.ResponseWriter, r *http.Request) {
|
||||
})
|
||||
if err != nil {
|
||||
httpapi.Write(rw, http.StatusInternalServerError, httpapi.Response{
|
||||
Message: err.Error(),
|
||||
Message: "Internal error creating user",
|
||||
Detail: err.Error(),
|
||||
})
|
||||
return
|
||||
}
|
||||
@@ -93,7 +96,8 @@ func (api *API) postFirstUser(rw http.ResponseWriter, r *http.Request) {
|
||||
})
|
||||
if err != nil {
|
||||
httpapi.Write(rw, http.StatusInternalServerError, httpapi.Response{
|
||||
Message: err.Error(),
|
||||
Message: "Internal error updating user's roles",
|
||||
Detail: err.Error(),
|
||||
})
|
||||
return
|
||||
}
|
||||
@@ -121,6 +125,9 @@ func (api *API) users(rw http.ResponseWriter, r *http.Request) {
|
||||
default:
|
||||
httpapi.Write(rw, http.StatusBadRequest, httpapi.Response{
|
||||
Message: fmt.Sprintf("%q is not a valid user status", filter),
|
||||
Validations: []httpapi.Error{
|
||||
{Field: "status", Detail: "invalid status"},
|
||||
},
|
||||
})
|
||||
return
|
||||
}
|
||||
@@ -150,7 +157,8 @@ func (api *API) users(rw http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
if err != nil {
|
||||
httpapi.Write(rw, http.StatusInternalServerError, httpapi.Response{
|
||||
Message: err.Error(),
|
||||
Message: "Internal error fetching users",
|
||||
Detail: err.Error(),
|
||||
})
|
||||
return
|
||||
}
|
||||
@@ -165,7 +173,8 @@ func (api *API) users(rw http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
if err != nil {
|
||||
httpapi.Write(rw, http.StatusInternalServerError, httpapi.Response{
|
||||
Message: err.Error(),
|
||||
Message: "Internal error fetching user's organizations",
|
||||
Detail: err.Error(),
|
||||
})
|
||||
return
|
||||
}
|
||||
@@ -204,13 +213,14 @@ func (api *API) postUser(rw http.ResponseWriter, r *http.Request) {
|
||||
})
|
||||
if err == nil {
|
||||
httpapi.Write(rw, http.StatusConflict, httpapi.Response{
|
||||
Message: "user already exists",
|
||||
Message: "User already exists",
|
||||
})
|
||||
return
|
||||
}
|
||||
if !errors.Is(err, sql.ErrNoRows) {
|
||||
httpapi.Write(rw, http.StatusInternalServerError, httpapi.Response{
|
||||
Message: fmt.Sprintf("get user: %s", err),
|
||||
Message: "Internal error fetching user",
|
||||
Detail: err.Error(),
|
||||
})
|
||||
return
|
||||
}
|
||||
@@ -218,13 +228,14 @@ func (api *API) postUser(rw http.ResponseWriter, r *http.Request) {
|
||||
_, err = api.Database.GetOrganizationByID(r.Context(), createUser.OrganizationID)
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
httpapi.Write(rw, http.StatusNotFound, httpapi.Response{
|
||||
Message: "organization does not exist with the provided id",
|
||||
Message: fmt.Sprintf("Organization does not exist with the provided id %q", createUser.OrganizationID),
|
||||
})
|
||||
return
|
||||
}
|
||||
if err != nil {
|
||||
httpapi.Write(rw, http.StatusInternalServerError, httpapi.Response{
|
||||
Message: fmt.Sprintf("get organization: %s", err),
|
||||
Message: "Internal error fetching organization",
|
||||
Detail: err.Error(),
|
||||
})
|
||||
return
|
||||
}
|
||||
@@ -232,7 +243,8 @@ func (api *API) postUser(rw http.ResponseWriter, r *http.Request) {
|
||||
user, _, err := api.createUser(r.Context(), createUser)
|
||||
if err != nil {
|
||||
httpapi.Write(rw, http.StatusInternalServerError, httpapi.Response{
|
||||
Message: err.Error(),
|
||||
Message: "Internal error creating user",
|
||||
Detail: err.Error(),
|
||||
})
|
||||
return
|
||||
}
|
||||
@@ -252,7 +264,8 @@ func (api *API) userByName(rw http.ResponseWriter, r *http.Request) {
|
||||
|
||||
if err != nil {
|
||||
httpapi.Write(rw, http.StatusInternalServerError, httpapi.Response{
|
||||
Message: fmt.Sprintf("get organization IDs: %s", err.Error()),
|
||||
Message: "Internal error fetching user's organizations",
|
||||
Detail: err.Error(),
|
||||
})
|
||||
return
|
||||
}
|
||||
@@ -285,14 +298,15 @@ func (api *API) putUserProfile(rw http.ResponseWriter, r *http.Request) {
|
||||
})
|
||||
}
|
||||
httpapi.Write(rw, http.StatusConflict, httpapi.Response{
|
||||
Message: "user already exists",
|
||||
Errors: responseErrors,
|
||||
Message: "User already exists",
|
||||
Validations: responseErrors,
|
||||
})
|
||||
return
|
||||
}
|
||||
if !errors.Is(err, sql.ErrNoRows) && isDifferentUser {
|
||||
httpapi.Write(rw, http.StatusInternalServerError, httpapi.Response{
|
||||
Message: fmt.Sprintf("get user: %s", err),
|
||||
Message: "Internal error fetching user",
|
||||
Detail: err.Error(),
|
||||
})
|
||||
return
|
||||
}
|
||||
@@ -306,7 +320,8 @@ func (api *API) putUserProfile(rw http.ResponseWriter, r *http.Request) {
|
||||
|
||||
if err != nil {
|
||||
httpapi.Write(rw, http.StatusInternalServerError, httpapi.Response{
|
||||
Message: fmt.Sprintf("patch user: %s", err.Error()),
|
||||
Message: "Internal error updating user",
|
||||
Detail: err.Error(),
|
||||
})
|
||||
return
|
||||
}
|
||||
@@ -314,7 +329,8 @@ func (api *API) putUserProfile(rw http.ResponseWriter, r *http.Request) {
|
||||
organizationIDs, err := userOrganizationIDs(r.Context(), api, user)
|
||||
if err != nil {
|
||||
httpapi.Write(rw, http.StatusInternalServerError, httpapi.Response{
|
||||
Message: fmt.Sprintf("get organization IDs: %s", err.Error()),
|
||||
Message: "Internal error fetching user's organizations",
|
||||
Detail: err.Error(),
|
||||
})
|
||||
return
|
||||
}
|
||||
@@ -346,7 +362,8 @@ func (api *API) putUserStatus(status database.UserStatus) func(rw http.ResponseW
|
||||
|
||||
if err != nil {
|
||||
httpapi.Write(rw, http.StatusInternalServerError, httpapi.Response{
|
||||
Message: fmt.Sprintf("put user suspended: %s", err.Error()),
|
||||
Message: fmt.Sprintf("Internal error updating user's status to %q", status),
|
||||
Detail: err.Error(),
|
||||
})
|
||||
return
|
||||
}
|
||||
@@ -354,7 +371,8 @@ func (api *API) putUserStatus(status database.UserStatus) func(rw http.ResponseW
|
||||
organizations, err := userOrganizationIDs(r.Context(), api, user)
|
||||
if err != nil {
|
||||
httpapi.Write(rw, http.StatusInternalServerError, httpapi.Response{
|
||||
Message: fmt.Sprintf("get organization IDs: %s", err.Error()),
|
||||
Message: "Internal error fetching user's organizations",
|
||||
Detail: err.Error(),
|
||||
})
|
||||
return
|
||||
}
|
||||
@@ -381,7 +399,8 @@ func (api *API) putUserPassword(rw http.ResponseWriter, r *http.Request) {
|
||||
err := userpassword.Validate(params.Password)
|
||||
if err != nil {
|
||||
httpapi.Write(rw, http.StatusBadRequest, httpapi.Response{
|
||||
Errors: []httpapi.Error{
|
||||
Message: "Invalid password",
|
||||
Validations: []httpapi.Error{
|
||||
{
|
||||
Field: "password",
|
||||
Detail: err.Error(),
|
||||
@@ -398,13 +417,15 @@ func (api *API) putUserPassword(rw http.ResponseWriter, r *http.Request) {
|
||||
ok, err := userpassword.Compare(string(user.HashedPassword), params.OldPassword)
|
||||
if err != nil {
|
||||
httpapi.Write(rw, http.StatusInternalServerError, httpapi.Response{
|
||||
Message: fmt.Sprintf("compare user password: %s", err.Error()),
|
||||
Message: "Internal error with passwords",
|
||||
Detail: err.Error(),
|
||||
})
|
||||
return
|
||||
}
|
||||
if !ok {
|
||||
httpapi.Write(rw, http.StatusBadRequest, httpapi.Response{
|
||||
Errors: []httpapi.Error{
|
||||
Message: "Old password is incorrect",
|
||||
Validations: []httpapi.Error{
|
||||
{
|
||||
Field: "old_password",
|
||||
Detail: "Old password is incorrect.",
|
||||
@@ -418,7 +439,8 @@ func (api *API) putUserPassword(rw http.ResponseWriter, r *http.Request) {
|
||||
hashedPassword, err := userpassword.Hash(params.Password)
|
||||
if err != nil {
|
||||
httpapi.Write(rw, http.StatusInternalServerError, httpapi.Response{
|
||||
Message: fmt.Sprintf("hash password: %s", err.Error()),
|
||||
Message: "Internal error hashing new password",
|
||||
Detail: err.Error(),
|
||||
})
|
||||
return
|
||||
}
|
||||
@@ -428,7 +450,8 @@ func (api *API) putUserPassword(rw http.ResponseWriter, r *http.Request) {
|
||||
})
|
||||
if err != nil {
|
||||
httpapi.Write(rw, http.StatusInternalServerError, httpapi.Response{
|
||||
Message: fmt.Sprintf("put user password: %s", err.Error()),
|
||||
Message: "Internal error updating user's password",
|
||||
Detail: err.Error(),
|
||||
})
|
||||
return
|
||||
}
|
||||
@@ -452,7 +475,8 @@ func (api *API) userRoles(rw http.ResponseWriter, r *http.Request) {
|
||||
memberships, err := api.Database.GetOrganizationMembershipsByUserID(r.Context(), user.ID)
|
||||
if err != nil {
|
||||
httpapi.Write(rw, http.StatusInternalServerError, httpapi.Response{
|
||||
Message: fmt.Sprintf("get user memberships: %s", err),
|
||||
Message: "Internal error fetching user's organization memberships",
|
||||
Detail: err.Error(),
|
||||
})
|
||||
return
|
||||
}
|
||||
@@ -518,7 +542,8 @@ func (api *API) putUserRoles(rw http.ResponseWriter, r *http.Request) {
|
||||
organizationIDs, err := userOrganizationIDs(r.Context(), api, user)
|
||||
if err != nil {
|
||||
httpapi.Write(rw, http.StatusInternalServerError, httpapi.Response{
|
||||
Message: fmt.Sprintf("get organization IDs: %s", err.Error()),
|
||||
Message: "Internal error fetching user's organizations",
|
||||
Detail: err.Error(),
|
||||
})
|
||||
return
|
||||
}
|
||||
@@ -532,7 +557,7 @@ func (api *API) updateSiteUserRoles(ctx context.Context, args database.UpdateUse
|
||||
// Enforce only site wide roles.
|
||||
for _, r := range args.GrantedRoles {
|
||||
if _, ok := rbac.IsOrgRole(r); ok {
|
||||
return database.User{}, xerrors.Errorf("must only update site wide roles")
|
||||
return database.User{}, xerrors.Errorf("Must only update site wide roles")
|
||||
}
|
||||
|
||||
if _, err := rbac.RoleByName(r); err != nil {
|
||||
@@ -558,7 +583,8 @@ func (api *API) organizationsByUser(rw http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
if err != nil {
|
||||
httpapi.Write(rw, http.StatusInternalServerError, httpapi.Response{
|
||||
Message: fmt.Sprintf("get organizations: %s", err.Error()),
|
||||
Message: "Internal error fetching user's organizations",
|
||||
Detail: err.Error(),
|
||||
})
|
||||
return
|
||||
}
|
||||
@@ -610,7 +636,7 @@ func (api *API) postLogin(rw http.ResponseWriter, r *http.Request) {
|
||||
})
|
||||
if err != nil && !xerrors.Is(err, sql.ErrNoRows) {
|
||||
httpapi.Write(rw, http.StatusInternalServerError, httpapi.Response{
|
||||
Message: fmt.Sprintf("get user: %s", err.Error()),
|
||||
Message: "Internal error",
|
||||
})
|
||||
return
|
||||
}
|
||||
@@ -619,7 +645,7 @@ func (api *API) postLogin(rw http.ResponseWriter, r *http.Request) {
|
||||
equal, err := userpassword.Compare(string(user.HashedPassword), loginWithPassword.Password)
|
||||
if err != nil {
|
||||
httpapi.Write(rw, http.StatusInternalServerError, httpapi.Response{
|
||||
Message: fmt.Sprintf("compare: %s", err.Error()),
|
||||
Message: "Internal error",
|
||||
})
|
||||
}
|
||||
if !equal {
|
||||
@@ -634,7 +660,7 @@ func (api *API) postLogin(rw http.ResponseWriter, r *http.Request) {
|
||||
// If the user logged into a suspended account, reject the login request.
|
||||
if user.Status != database.UserStatusActive {
|
||||
httpapi.Write(rw, http.StatusUnauthorized, httpapi.Response{
|
||||
Message: "You are suspended, contact an admin to reactivate your account",
|
||||
Message: "Your account is suspended. Contact an admin to reactivate your account.",
|
||||
})
|
||||
return
|
||||
}
|
||||
@@ -693,7 +719,8 @@ func (api *API) postLogout(rw http.ResponseWriter, r *http.Request) {
|
||||
err := api.Database.DeleteAPIKeyByID(r.Context(), apiKey.ID)
|
||||
if err != nil {
|
||||
httpapi.Write(rw, http.StatusInternalServerError, httpapi.Response{
|
||||
Message: fmt.Sprintf("delete api key: %s", err.Error()),
|
||||
Message: "Internal error deleting API key",
|
||||
Detail: err.Error(),
|
||||
})
|
||||
return
|
||||
}
|
||||
@@ -722,7 +749,8 @@ func (api *API) createAPIKey(rw http.ResponseWriter, r *http.Request, params dat
|
||||
keyID, keySecret, err := generateAPIKeyIDSecret()
|
||||
if err != nil {
|
||||
httpapi.Write(rw, http.StatusInternalServerError, httpapi.Response{
|
||||
Message: fmt.Sprintf("generate api key parts: %s", err.Error()),
|
||||
Message: "Internal error generating API key",
|
||||
Detail: err.Error(),
|
||||
})
|
||||
return "", false
|
||||
}
|
||||
@@ -754,7 +782,8 @@ func (api *API) createAPIKey(rw http.ResponseWriter, r *http.Request, params dat
|
||||
})
|
||||
if err != nil {
|
||||
httpapi.Write(rw, http.StatusInternalServerError, httpapi.Response{
|
||||
Message: fmt.Sprintf("insert api key: %s", err.Error()),
|
||||
Message: "Internal error inserting API key",
|
||||
Detail: err.Error(),
|
||||
})
|
||||
return "", false
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user