Skip to content

[F] Standardize HTTP status codes #7851

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
[F] Standardize HTTP status codes
  • Loading branch information
Boc-chi-no committed Nov 30, 2024
commit bfce3421574778cc3bc8a0164e3212d6648b1e32
4 changes: 2 additions & 2 deletions internal/context/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,13 +160,13 @@ func (c *Context) RedirectSubpath(location string, status ...int) {
}

// RenderWithErr used for page has form validation but need to prompt error to users.
func (c *Context) RenderWithErr(msg, tpl string, f any) {
func (c *Context) RenderWithErr(msg string, status int, tpl string, f any) {
if f != nil {
form.Assign(f, c.Data)
}
c.Flash.ErrorMsg = msg
c.Data["Flash"] = c.Flash
c.HTML(http.StatusOK, tpl)
c.HTML(status, tpl)
}

// NotFound renders the 404 page.
Expand Down
6 changes: 3 additions & 3 deletions internal/route/admin/auths.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ func NewAuthSourcePost(c *context.Context, f form.Authentication) {
c.Data["HasTLS"] = hasTLS

if c.HasError() {
c.Success(AUTH_NEW)
c.HTML(http.StatusBadRequest, AUTH_NEW)
return
}

Expand All @@ -171,7 +171,7 @@ func NewAuthSourcePost(c *context.Context, f form.Authentication) {
if err != nil {
if database.IsErrLoginSourceAlreadyExist(err) {
c.FormErr("Name")
c.RenderWithErr(c.Tr("admin.auths.login_source_exist", f.Name), AUTH_NEW, f)
c.RenderWithErr(c.Tr("admin.auths.login_source_exist", f.Name), http.StatusUnprocessableEntity, AUTH_NEW, f)
} else {
c.Error(err, "create login source")
}
Expand Down Expand Up @@ -227,7 +227,7 @@ func EditAuthSourcePost(c *context.Context, f form.Authentication) {
c.Data["HasTLS"] = source.Provider.HasTLS()

if c.HasError() {
c.Success(AUTH_EDIT)
c.HTML(http.StatusBadRequest, AUTH_EDIT)
return
}

Expand Down
13 changes: 7 additions & 6 deletions internal/route/admin/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package admin

import (
"net/http"
"strconv"
"strings"

Expand Down Expand Up @@ -72,7 +73,7 @@ func NewUserPost(c *context.Context, f form.AdminCrateUser) {
c.Data["CanSendEmail"] = conf.Email.Enabled

if c.HasError() {
c.Success(USER_NEW)
c.HTML(http.StatusBadRequest, USER_NEW)
return
}

Expand All @@ -93,13 +94,13 @@ func NewUserPost(c *context.Context, f form.AdminCrateUser) {
switch {
case database.IsErrUserAlreadyExist(err):
c.Data["Err_UserName"] = true
c.RenderWithErr(c.Tr("form.username_been_taken"), USER_NEW, &f)
c.RenderWithErr(c.Tr("form.username_been_taken"), http.StatusUnprocessableEntity, USER_NEW, &f)
case database.IsErrEmailAlreadyUsed(err):
c.Data["Err_Email"] = true
c.RenderWithErr(c.Tr("form.email_been_used"), USER_NEW, &f)
c.RenderWithErr(c.Tr("form.email_been_used"), http.StatusUnprocessableEntity, USER_NEW, &f)
case database.IsErrNameNotAllowed(err):
c.Data["Err_UserName"] = true
c.RenderWithErr(c.Tr("user.form.name_not_allowed", err.(database.ErrNameNotAllowed).Value()), USER_NEW, &f)
c.RenderWithErr(c.Tr("user.form.name_not_allowed", err.(database.ErrNameNotAllowed).Value()), http.StatusBadRequest, USER_NEW, &f)
default:
c.Error(err, "create user")
}
Expand Down Expand Up @@ -170,7 +171,7 @@ func EditUserPost(c *context.Context, f form.AdminEditUser) {
}

if c.HasError() {
c.Success(USER_EDIT)
c.HTML(http.StatusBadRequest, USER_EDIT)
return
}

Expand Down Expand Up @@ -207,7 +208,7 @@ func EditUserPost(c *context.Context, f form.AdminEditUser) {
if err != nil {
if database.IsErrEmailAlreadyUsed(err) {
c.Data["Err_Email"] = true
c.RenderWithErr(c.Tr("form.email_been_used"), USER_EDIT, &f)
c.RenderWithErr(c.Tr("form.email_been_used"), http.StatusUnprocessableEntity, USER_EDIT, &f)
} else {
c.Error(err, "update user")
}
Expand Down
36 changes: 18 additions & 18 deletions internal/route/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package route

import (
"net/http"
"net/mail"
"os"
"os/exec"
Expand Down Expand Up @@ -198,13 +199,12 @@ func InstallPost(c *context.Context, f form.Install) {
c.HasValue("Err_AdminEmail") {
c.FormErr("Admin")
}

c.Success(INSTALL)
c.HTML(http.StatusBadRequest, INSTALL)
return
}

if _, err := exec.LookPath("git"); err != nil {
c.RenderWithErr(c.Tr("install.test_git_failed", err), INSTALL, &f)
c.RenderWithErr(c.Tr("install.test_git_failed", err), http.StatusInternalServerError, INSTALL, &f)
return
}

Expand All @@ -226,18 +226,18 @@ func InstallPost(c *context.Context, f form.Install) {

if conf.Database.Type == "sqlite3" && conf.Database.Path == "" {
c.FormErr("DbPath")
c.RenderWithErr(c.Tr("install.err_empty_db_path"), INSTALL, &f)
c.RenderWithErr(c.Tr("install.err_empty_db_path"), http.StatusBadRequest, INSTALL, &f)
return
}

// Set test engine.
if err := database.NewTestEngine(); err != nil {
if strings.Contains(err.Error(), `Unknown database type: sqlite3`) {
c.FormErr("DbType")
c.RenderWithErr(c.Tr("install.sqlite3_not_available", "https://gogs.io/docs/installation/install_from_binary.html"), INSTALL, &f)
c.RenderWithErr(c.Tr("install.sqlite3_not_available", "https://gogs.io/docs/installation/install_from_binary.html"), http.StatusInternalServerError, INSTALL, &f)
} else {
c.FormErr("DbSetting")
c.RenderWithErr(c.Tr("install.invalid_db_setting", err), INSTALL, &f)
c.RenderWithErr(c.Tr("install.invalid_db_setting", err), http.StatusBadRequest, INSTALL, &f)
}
return
}
Expand All @@ -246,29 +246,29 @@ func InstallPost(c *context.Context, f form.Install) {
f.RepoRootPath = strings.ReplaceAll(f.RepoRootPath, "\\", "/")
if err := os.MkdirAll(f.RepoRootPath, os.ModePerm); err != nil {
c.FormErr("RepoRootPath")
c.RenderWithErr(c.Tr("install.invalid_repo_path", err), INSTALL, &f)
c.RenderWithErr(c.Tr("install.invalid_repo_path", err), http.StatusBadRequest, INSTALL, &f)
return
}

// Test log root path.
f.LogRootPath = strings.ReplaceAll(f.LogRootPath, "\\", "/")
if err := os.MkdirAll(f.LogRootPath, os.ModePerm); err != nil {
c.FormErr("LogRootPath")
c.RenderWithErr(c.Tr("install.invalid_log_root_path", err), INSTALL, &f)
c.RenderWithErr(c.Tr("install.invalid_log_root_path", err), http.StatusBadRequest, INSTALL, &f)
return
}

currentUser, match := conf.CheckRunUser(f.RunUser)
if !match {
c.FormErr("RunUser")
c.RenderWithErr(c.Tr("install.run_user_not_match", f.RunUser, currentUser), INSTALL, &f)
c.RenderWithErr(c.Tr("install.run_user_not_match", f.RunUser, currentUser), http.StatusForbidden, INSTALL, &f)
return
}

// Check host address and port
if len(f.SMTPHost) > 0 && !strings.Contains(f.SMTPHost, ":") {
c.FormErr("SMTP", "SMTPHost")
c.RenderWithErr(c.Tr("install.smtp_host_missing_port"), INSTALL, &f)
c.RenderWithErr(c.Tr("install.smtp_host_missing_port"), http.StatusBadRequest, INSTALL, &f)
return
}

Expand All @@ -277,27 +277,27 @@ func InstallPost(c *context.Context, f form.Install) {
_, err := mail.ParseAddress(f.SMTPFrom)
if err != nil {
c.FormErr("SMTP", "SMTPFrom")
c.RenderWithErr(c.Tr("install.invalid_smtp_from", err), INSTALL, &f)
c.RenderWithErr(c.Tr("install.invalid_smtp_from", err), http.StatusBadRequest, INSTALL, &f)
return
}
}

// Check logic loophole between disable self-registration and no admin account.
if f.DisableRegistration && f.AdminName == "" {
c.FormErr("Services", "Admin")
c.RenderWithErr(c.Tr("install.no_admin_and_disable_registration"), INSTALL, f)
c.RenderWithErr(c.Tr("install.no_admin_and_disable_registration"), http.StatusUnprocessableEntity, INSTALL, f)
return
}

// Check admin password.
if len(f.AdminName) > 0 && f.AdminPasswd == "" {
c.FormErr("Admin", "AdminPasswd")
c.RenderWithErr(c.Tr("install.err_empty_admin_password"), INSTALL, f)
c.RenderWithErr(c.Tr("install.err_empty_admin_password"), http.StatusBadRequest, INSTALL, f)
return
}
if f.AdminPasswd != f.AdminConfirmPasswd {
c.FormErr("Admin", "AdminPasswd")
c.RenderWithErr(c.Tr("form.password_not_match"), INSTALL, f)
c.RenderWithErr(c.Tr("form.password_not_match"), http.StatusBadRequest, INSTALL, f)
return
}

Expand Down Expand Up @@ -371,21 +371,21 @@ func InstallPost(c *context.Context, f form.Install) {
cfg.Section("security").Key("INSTALL_LOCK").SetValue("true")
secretKey, err := strutil.RandomChars(15)
if err != nil {
c.RenderWithErr(c.Tr("install.secret_key_failed", err), INSTALL, &f)
c.RenderWithErr(c.Tr("install.secret_key_failed", err), http.StatusInternalServerError, INSTALL, &f)
return
}
cfg.Section("security").Key("SECRET_KEY").SetValue(secretKey)

_ = os.MkdirAll(filepath.Dir(conf.CustomConf), os.ModePerm)
if err := cfg.SaveTo(conf.CustomConf); err != nil {
c.RenderWithErr(c.Tr("install.save_config_failed", err), INSTALL, &f)
c.RenderWithErr(c.Tr("install.save_config_failed", err), http.StatusInternalServerError, INSTALL, &f)
return
}

// NOTE: We reuse the current value because this handler does not have access to CLI flags.
err = GlobalInit(conf.CustomConf)
if err != nil {
c.RenderWithErr(c.Tr("install.init_failed", err), INSTALL, &f)
c.RenderWithErr(c.Tr("install.init_failed", err), http.StatusInternalServerError, INSTALL, &f)
return
}

Expand All @@ -405,7 +405,7 @@ func InstallPost(c *context.Context, f form.Install) {
if !database.IsErrUserAlreadyExist(err) {
conf.Security.InstallLock = false
c.FormErr("AdminName", "AdminEmail")
c.RenderWithErr(c.Tr("install.invalid_admin_setting", err), INSTALL, &f)
c.RenderWithErr(c.Tr("install.invalid_admin_setting", err), http.StatusBadRequest, INSTALL, &f)
return
}

Expand Down
7 changes: 4 additions & 3 deletions internal/route/org/org.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package org

import (
"net/http"

Check failure on line 8 in internal/route/org/org.go

View workflow job for this annotation

GitHub Actions / Lint

File is not `goimports`-ed (goimports)
log "unknwon.dev/clog/v2"

"gogs.io/gogs/internal/context"
Expand All @@ -25,7 +26,7 @@
c.Title("new_org")

if c.HasError() {
c.Success(CREATE)
c.HTML(http.StatusBadRequest, CREATE)
return
}

Expand All @@ -39,9 +40,9 @@
c.Data["Err_OrgName"] = true
switch {
case database.IsErrUserAlreadyExist(err):
c.RenderWithErr(c.Tr("form.org_name_been_taken"), CREATE, &f)
c.RenderWithErr(c.Tr("form.org_name_been_taken"), http.StatusUnprocessableEntity, CREATE, &f)
case database.IsErrNameNotAllowed(err):
c.RenderWithErr(c.Tr("org.form.name_not_allowed", err.(database.ErrNameNotAllowed).Value()), CREATE, &f)
c.RenderWithErr(c.Tr("org.form.name_not_allowed", err.(database.ErrNameNotAllowed).Value()), http.StatusBadRequest, CREATE, &f)
default:
c.Error(err, "create organization")
}
Expand Down
13 changes: 5 additions & 8 deletions internal/route/org/setting.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package org

import (
"net/http"

Check failure on line 8 in internal/route/org/setting.go

View workflow job for this annotation

GitHub Actions / Lint

File is not `goimports`-ed (goimports)
log "unknwon.dev/clog/v2"

"gogs.io/gogs/internal/auth"
Expand All @@ -31,7 +32,7 @@
c.Data["PageIsSettingsOptions"] = true

if c.HasError() {
c.Success(SETTINGS_OPTIONS)
c.HTML(http.StatusBadRequest, SETTINGS_OPTIONS)
return
}

Expand All @@ -42,18 +43,14 @@
err := database.Handle.Users().ChangeUsername(c.Req.Context(), c.Org.Organization.ID, f.Name)
if err != nil {
c.Data["OrgName"] = true
var msg string
switch {
case database.IsErrUserAlreadyExist(err):
msg = c.Tr("form.username_been_taken")
c.RenderWithErr(c.Tr("form.username_been_taken"), http.StatusUnprocessableEntity, SETTINGS_OPTIONS, &f)
case database.IsErrNameNotAllowed(err):
msg = c.Tr("user.form.name_not_allowed", err.(database.ErrNameNotAllowed).Value())
c.RenderWithErr(c.Tr("user.form.name_not_allowed", err.(database.ErrNameNotAllowed).Value()), http.StatusBadRequest, SETTINGS_OPTIONS, &f)
default:
c.Error(err, "change organization name")
return
}

c.RenderWithErr(msg, SETTINGS_OPTIONS, &f)
return
}

Expand Down Expand Up @@ -108,7 +105,7 @@
if c.Req.Method == "POST" {
if _, err := database.Handle.Users().Authenticate(c.Req.Context(), c.User.Name, c.Query("password"), c.User.LoginSource); err != nil {
if auth.IsErrBadCredentials(err) {
c.RenderWithErr(c.Tr("form.enterred_invalid_password"), SETTINGS_DELETE, nil)
c.RenderWithErr(c.Tr("form.enterred_invalid_password"), http.StatusUnauthorized, SETTINGS_DELETE, nil)
} else {
c.Error(err, "authenticate user")
}
Expand Down
10 changes: 5 additions & 5 deletions internal/route/org/teams.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,17 +163,17 @@ func NewTeamPost(c *context.Context, f form.CreateTeam) {
c.Data["Team"] = t

if c.HasError() {
c.Success(TEAM_NEW)
c.HTML(http.StatusBadRequest, TEAM_NEW)
return
}

if err := database.NewTeam(t); err != nil {
c.Data["Err_TeamName"] = true
switch {
case database.IsErrTeamAlreadyExist(err):
c.RenderWithErr(c.Tr("form.team_name_been_taken"), TEAM_NEW, &f)
c.RenderWithErr(c.Tr("form.team_name_been_taken"), http.StatusUnprocessableEntity, TEAM_NEW, &f)
case database.IsErrNameNotAllowed(err):
c.RenderWithErr(c.Tr("org.form.team_name_not_allowed", err.(database.ErrNameNotAllowed).Value()), TEAM_NEW, &f)
c.RenderWithErr(c.Tr("org.form.team_name_not_allowed", err.(database.ErrNameNotAllowed).Value()), http.StatusBadRequest, TEAM_NEW, &f)
default:
c.Error(err, "new team")
}
Expand Down Expand Up @@ -218,7 +218,7 @@ func EditTeamPost(c *context.Context, f form.CreateTeam) {
c.Data["Team"] = t

if c.HasError() {
c.Success(TEAM_NEW)
c.HTML(http.StatusBadRequest, TEAM_NEW)
return
}

Expand Down Expand Up @@ -249,7 +249,7 @@ func EditTeamPost(c *context.Context, f form.CreateTeam) {
c.Data["Err_TeamName"] = true
switch {
case database.IsErrTeamAlreadyExist(err):
c.RenderWithErr(c.Tr("form.team_name_been_taken"), TEAM_NEW, &f)
c.RenderWithErr(c.Tr("form.team_name_been_taken"), http.StatusUnprocessableEntity, TEAM_NEW, &f)
default:
c.Error(err, "update team")
}
Expand Down
Loading
Loading
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy