From 2d1d4f3dccd1706d0a18875d53f1747d70b7757f Mon Sep 17 00:00:00 2001 From: Yousong Zhou Date: Mon, 11 Jan 2021 15:53:15 +0800 Subject: [PATCH] apigateway: reword error messages --- pkg/apigateway/handler/auth.go | 8 ++++---- pkg/apigateway/handler/auth_totp.go | 16 ++++++++-------- pkg/apigateway/handler/idp.go | 2 +- pkg/apigateway/handler/misc.go | 16 +++++++++------- pkg/apigateway/handler/resource.go | 6 +++--- 5 files changed, 25 insertions(+), 23 deletions(-) diff --git a/pkg/apigateway/handler/auth.go b/pkg/apigateway/handler/auth.go index e663ca6aa4..bb2278f5f7 100644 --- a/pkg/apigateway/handler/auth.go +++ b/pkg/apigateway/handler/auth.go @@ -996,7 +996,7 @@ func (h *AuthHandlers) getPermissionDetails(ctx context.Context, w http.Response _, query, body := appsrv.FetchEnv(ctx, w, req) if body == nil { - httperrors.InvalidInputError(ctx, w, "body is empty") + httperrors.InvalidInputError(ctx, w, "request body is empty") return } var name string @@ -1030,7 +1030,7 @@ func (h *AuthHandlers) doCreatePolicies(ctx context.Context, w http.ResponseWrit // } _, _, body := appsrv.FetchEnv(ctx, w, req) if body == nil { - httperrors.InvalidInputError(ctx, w, "body is empty") + httperrors.InvalidInputError(ctx, w, "request body is empty") return } s := auth.GetSession(ctx, t, FetchRegion(req), "") @@ -1114,7 +1114,7 @@ func (h *AuthHandlers) resetUserPassword(ctx context.Context, w http.ResponseWri _, _, body := appsrv.FetchEnv(ctx, w, req) if body == nil { - httperrors.InvalidInputError(ctx, w, "body is empty") + httperrors.InvalidInputError(ctx, w, "request body is empty") return } @@ -1150,7 +1150,7 @@ func (h *AuthHandlers) resetUserPassword(ctx context.Context, w http.ResponseWri return } } - httperrors.InputParameterError(ctx, w, "密码错误") + httperrors.InputParameterError(ctx, w, "wrong password") return } diff --git a/pkg/apigateway/handler/auth_totp.go b/pkg/apigateway/handler/auth_totp.go index 3b74ff54fe..9c6abdc60a 100644 --- a/pkg/apigateway/handler/auth_totp.go +++ b/pkg/apigateway/handler/auth_totp.go @@ -205,7 +205,7 @@ func validateTotpRecoverySecrets(s *mcclient.ClientSession, uid string, question func initTotpSecrets(ctx context.Context, w http.ResponseWriter, req *http.Request) { t, authToken, err := fetchAuthInfo(ctx, req) if err != nil { - httperrors.InvalidCredentialError(ctx, w, "fetchAuthInfo fail %s", err) + httperrors.InvalidCredentialError(ctx, w, "fetchAuthInfo fail: %s", err) return } if authToken.IsTotpInitialized() { @@ -232,14 +232,14 @@ func initTotpSecrets(ctx context.Context, w http.ResponseWriter, req *http.Reque func validatePasscodeHandler(ctx context.Context, w http.ResponseWriter, req *http.Request) { t, authToken, err := fetchAuthInfo(ctx, req) if err != nil { - httperrors.InvalidCredentialError(ctx, w, "fetchAuthInfo fail %s", err) + httperrors.InvalidCredentialError(ctx, w, "fetchAuthInfo fail: %s", err) return } s := auth.GetAdminSession(ctx, FetchRegion(req), "") _, _, body := appsrv.FetchEnv(ctx, w, req) if body == nil { - httperrors.InvalidInputError(ctx, w, "body is empty") + httperrors.InvalidInputError(ctx, w, "request body is empty") return } @@ -271,14 +271,14 @@ func validatePasscodeHandler(ctx context.Context, w http.ResponseWriter, req *ht func resetTotpSecrets(ctx context.Context, w http.ResponseWriter, req *http.Request) { t, _, err := fetchAuthInfo(ctx, req) if err != nil { - httperrors.InvalidCredentialError(ctx, w, "fetchAuthInfo fail %s", err) + httperrors.InvalidCredentialError(ctx, w, "fetchAuthInfo fail: %s", err) return } s := auth.GetAdminSession(ctx, FetchRegion(req), "") _, _, body := appsrv.FetchEnv(ctx, w, req) if body == nil { - httperrors.InvalidInputError(ctx, w, "body is empty") + httperrors.InvalidInputError(ctx, w, "request body is empty") return } @@ -309,7 +309,7 @@ func resetTotpSecrets(ctx context.Context, w http.ResponseWriter, req *http.Requ func listTotpRecoveryQuestions(ctx context.Context, w http.ResponseWriter, req *http.Request) { t, _, err := fetchAuthInfo(ctx, req) if err != nil { - httperrors.InvalidCredentialError(ctx, w, "fetchAuthInfo fail %s", err) + httperrors.InvalidCredentialError(ctx, w, "fetchAuthInfo fail: %s", err) return } @@ -335,14 +335,14 @@ func listTotpRecoveryQuestions(ctx context.Context, w http.ResponseWriter, req * func resetTotpRecoveryQuestions(ctx context.Context, w http.ResponseWriter, req *http.Request) { t, _, err := fetchAuthInfo(ctx, req) if err != nil { - httperrors.InvalidCredentialError(ctx, w, "fetchAuthInfo fail %s", err) + httperrors.InvalidCredentialError(ctx, w, "fetchAuthInfo fail: %s", err) return } s := auth.GetAdminSession(ctx, FetchRegion(req), "") _, _, body := appsrv.FetchEnv(ctx, w, req) if body == nil { - httperrors.InvalidInputError(ctx, w, "body is empty") + httperrors.InvalidInputError(ctx, w, "request body is empty") return } diff --git a/pkg/apigateway/handler/idp.go b/pkg/apigateway/handler/idp.go index 047bbf6213..a304120fe3 100644 --- a/pkg/apigateway/handler/idp.go +++ b/pkg/apigateway/handler/idp.go @@ -170,7 +170,7 @@ func (h *AuthHandlers) handleSsoLogin(ctx context.Context, w http.ResponseWriter case "POST": formData, err := appsrv.Fetch(req) if err != nil { - httperrors.InputParameterError(ctx, w, "fetch formdata error: %s", err) + httperrors.InputParameterError(ctx, w, "fetch form data error: %s", err) } body, err = jsonutils.ParseQueryString(string(formData)) if err != nil { diff --git a/pkg/apigateway/handler/misc.go b/pkg/apigateway/handler/misc.go index b5fdd07bc7..afacdd9f81 100644 --- a/pkg/apigateway/handler/misc.go +++ b/pkg/apigateway/handler/misc.go @@ -42,6 +42,8 @@ import ( "yunion.io/x/onecloud/pkg/util/httputils" ) +const contentTypeSpreadsheet = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" + const ( HOST_MAC = "*MAC地址" HOST_NAME = "*名称" @@ -154,8 +156,8 @@ func (mh *MiscHandler) DoBatchHostRegister(ctx context.Context, w http.ResponseW fileHeader := hostfiles[0].Header contentType := fileHeader.Get("Content-Type") - if contentType != "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" { - e := httperrors.NewInputParameterError("Wrong content type %s, required application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", contentType) + if contentType != contentTypeSpreadsheet { + e := httperrors.NewInputParameterError("Wrong content type %s, want %s", contentType, contentTypeSpreadsheet) httperrors.JsonClientError(ctx, w, e) return } @@ -199,7 +201,7 @@ func (mh *MiscHandler) DoBatchHostRegister(ctx context.Context, w http.ResponseW } if !titlesOk { - httperrors.InputParameterError(ctx, w, "template file is invalid.please check.") + httperrors.InputParameterError(ctx, w, "template file is invalid. please check.") return } @@ -302,8 +304,8 @@ func (mh *MiscHandler) DoBatchUserRegister(ctx context.Context, w http.ResponseW fileHeader := userfiles[0].Header contentType := fileHeader.Get("Content-Type") - if contentType != "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" { - e := httperrors.NewInputParameterError("Wrong content type %s, required application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", contentType) + if contentType != contentTypeSpreadsheet { + e := httperrors.NewInputParameterError("Wrong content type %s, want %s", contentType, contentTypeSpreadsheet) httperrors.JsonClientError(ctx, w, e) return } @@ -328,7 +330,7 @@ func (mh *MiscHandler) DoBatchUserRegister(ctx context.Context, w http.ResponseW // skipped header row rows := xlsx.GetRows("users") if len(rows) <= 1 { - e := httperrors.NewInputParameterError("empty file") + e := httperrors.NewInputParameterError("empty file content") httperrors.JsonClientError(ctx, w, e) return } else if len(rows) > BATCH_USER_REGISTER_QUANTITY_LIMITATION { @@ -431,7 +433,7 @@ func (mh *MiscHandler) getDownloadsHandler(ctx context.Context, w http.ResponseW params := appctx.AppContextParams(ctx) template, ok := params[""] if !ok || len(template) == 0 { - httperrors.InvalidInputError(ctx, w, "not found") + httperrors.InvalidInputError(ctx, w, "template_id") return } diff --git a/pkg/apigateway/handler/resource.go b/pkg/apigateway/handler/resource.go index fc9baa6031..9dd19d1c1f 100644 --- a/pkg/apigateway/handler/resource.go +++ b/pkg/apigateway/handler/resource.go @@ -142,11 +142,11 @@ func fetchIdList(ctx context.Context, query jsonutils.JSONObject, w http.Respons if e == nil && len(idlist) > 0 { queryDict := query.(*jsonutils.JSONDict) queryDict.Remove("id") - log.Debugf("Get idlist: %s", idlist) + log.Debugf("Get id list: %s", idlist) return jsonutils.JSONArray2StringArray(idlist) } else { - log.Debugf("Cannot find idlist in query: %s", query) - httperrors.InvalidInputError(ctx, w, "No idlist found") + log.Debugf("Cannot find id list in query: %s", query) + httperrors.InvalidInputError(ctx, w, "No id list found") return nil } }