From 32f0d844271f7162d36dddefed601e3de5d224c4 Mon Sep 17 00:00:00 2001 From: Geno Date: Thu, 22 Jul 2021 18:16:05 +0200 Subject: [PATCH] web: error as errors --- web/auth/api_login.go | 10 +++++----- web/auth/api_login_test.go | 6 +++--- web/auth/api_my_delete.go | 2 +- web/auth/api_my_delete_test.go | 2 +- web/auth/api_my_password.go | 6 +++--- web/auth/api_my_password_test.go | 4 ++-- web/auth/api_my_status_test.go | 2 +- web/auth/api_password_code.go | 10 +++++----- web/auth/api_password_code_test.go | 4 ++-- web/auth/error.go | 27 ++++++++++++++++----------- web/auth/lib_user.go | 6 +++--- web/auth/middleware.go | 4 ++-- web/error.go | 10 ++++++---- 13 files changed, 50 insertions(+), 43 deletions(-) diff --git a/web/auth/api_login.go b/web/auth/api_login.go index 89035f5..26eb043 100644 --- a/web/auth/api_login.go +++ b/web/auth/api_login.go @@ -32,7 +32,7 @@ func apiLogin(r *gin.Engine, ws *web.Service) { var data login if err := c.BindJSON(&data); err != nil { c.JSON(http.StatusBadRequest, web.HTTPError{ - Message: web.APIErrorInvalidRequestFormat, + Message: web.ErrAPIInvalidRequestFormat.Error(), Error: err.Error(), }) return @@ -42,20 +42,20 @@ func apiLogin(r *gin.Engine, ws *web.Service) { if err := ws.DB.Where(map[string]interface{}{"username": data.Username}).First(d).Error; err != nil { if errors.Is(err, gorm.ErrRecordNotFound) { c.JSON(http.StatusUnauthorized, web.HTTPError{ - Message: APIErrorUserNotFound, + Message: ErrAPIUserNotFound.Error(), Error: err.Error(), }) return } c.JSON(http.StatusInternalServerError, web.HTTPError{ - Message: web.APIErrorInternalDatabase, + Message: web.ErrAPIInternalDatabase.Error(), Error: err.Error(), }) return } if !d.ValidatePassword(data.Password) { c.JSON(http.StatusUnauthorized, web.HTTPError{ - Message: APIErrorIncorrectPassword, + Message: ErrAPIIncorrectPassword.Error(), }) return } @@ -64,7 +64,7 @@ func apiLogin(r *gin.Engine, ws *web.Service) { session.Set("user_id", d.ID.String()) if err := session.Save(); err != nil { c.JSON(http.StatusBadRequest, web.HTTPError{ - Message: APIErrorCreateSession, + Message: ErrAPICreateSession.Error(), Error: err.Error(), }) return diff --git a/web/auth/api_login_test.go b/web/auth/api_login_test.go index 494ad37..8082d34 100644 --- a/web/auth/api_login_test.go +++ b/web/auth/api_login_test.go @@ -21,21 +21,21 @@ func TestAPILogin(t *testing.T) { // invalid err = s.Request(http.MethodPost, "/api/v1/auth/login", 1, http.StatusBadRequest, &hErr) assert.NoError(err) - assert.Equal(web.APIErrorInvalidRequestFormat, hErr.Message) + assert.Equal(web.ErrAPIInvalidRequestFormat.Error(), hErr.Message) req := login{} hErr = web.HTTPError{} // invalid - user err = s.Request(http.MethodPost, "/api/v1/auth/login", &req, http.StatusUnauthorized, &hErr) assert.NoError(err) - assert.Equal(APIErrorUserNotFound, hErr.Message) + assert.Equal(ErrAPIUserNotFound.Error(), hErr.Message) req.Username = "admin" hErr = web.HTTPError{} // invalid - password err = s.Request(http.MethodPost, "/api/v1/auth/login", &req, http.StatusUnauthorized, &hErr) assert.NoError(err) - assert.Equal(APIErrorIncorrectPassword, hErr.Message) + assert.Equal(ErrAPIIncorrectPassword.Error(), hErr.Message) req.Password = "CHANGEME" obj := User{} diff --git a/web/auth/api_my_delete.go b/web/auth/api_my_delete.go index 15b92eb..a62c295 100644 --- a/web/auth/api_my_delete.go +++ b/web/auth/api_my_delete.go @@ -25,7 +25,7 @@ func apiMyDelete(r *gin.Engine, ws *web.Service) { } if err := ws.DB.Delete(&User{ID: id}).Error; err != nil { c.JSON(http.StatusInternalServerError, web.HTTPError{ - Message: web.APIErrorInternalDatabase, + Message: web.ErrAPIInternalDatabase.Error(), Error: err.Error(), }) return diff --git a/web/auth/api_my_delete_test.go b/web/auth/api_my_delete_test.go index de3c785..0e15daf 100644 --- a/web/auth/api_my_delete_test.go +++ b/web/auth/api_my_delete_test.go @@ -21,7 +21,7 @@ func TestAPIDeleteMyProfil(t *testing.T) { // invalid err = s.Request(http.MethodDelete, "/api/v1/my/profil", nil, http.StatusUnauthorized, &hErr) assert.NoError(err) - assert.Equal(APIErrorNoSession, hErr.Message) + assert.Equal(ErrAPINoSession.Error(), hErr.Message) err = s.Login(webtest.Login{ Username: "admin", diff --git a/web/auth/api_my_password.go b/web/auth/api_my_password.go index 345d2a3..1fb657b 100644 --- a/web/auth/api_my_password.go +++ b/web/auth/api_my_password.go @@ -29,14 +29,14 @@ func apiMyPassword(r *gin.Engine, ws *web.Service) { var password string if err := c.BindJSON(&password); err != nil { c.JSON(http.StatusBadRequest, web.HTTPError{ - Message: web.APIErrorInvalidRequestFormat, + Message: web.ErrAPIInvalidRequestFormat.Error(), Error: err.Error(), }) return } if err := d.SetPassword(password); err != nil { c.JSON(http.StatusInternalServerError, web.HTTPError{ - Message: APIErrroCreatePassword, + Message: ErrAPICreatePassword.Error(), Error: err.Error(), }) return @@ -44,7 +44,7 @@ func apiMyPassword(r *gin.Engine, ws *web.Service) { if err := ws.DB.Save(&d).Error; err != nil { c.JSON(http.StatusInternalServerError, web.HTTPError{ - Message: web.APIErrorInternalDatabase, + Message: web.ErrAPIInternalDatabase.Error(), Error: err.Error(), }) return diff --git a/web/auth/api_my_password_test.go b/web/auth/api_my_password_test.go index 33635fb..6cb971f 100644 --- a/web/auth/api_my_password_test.go +++ b/web/auth/api_my_password_test.go @@ -24,7 +24,7 @@ func TestAPIPassword(t *testing.T) { // no auth err = s.Request(http.MethodPost, "/api/v1/my/auth/password", &passwordNew, http.StatusUnauthorized, &hErr) assert.NoError(err) - assert.Equal(APIErrorNoSession, hErr.Message) + assert.Equal(ErrAPINoSession.Error(), hErr.Message) err = s.TestLogin() assert.NoError(err) @@ -33,7 +33,7 @@ func TestAPIPassword(t *testing.T) { // invalid err = s.Request(http.MethodPost, "/api/v1/my/auth/password", nil, http.StatusBadRequest, &hErr) assert.NoError(err) - assert.Equal(web.APIErrorInvalidRequestFormat, hErr.Message) + assert.Equal(web.ErrAPIInvalidRequestFormat.Error(), hErr.Message) res := false // set new password diff --git a/web/auth/api_my_status_test.go b/web/auth/api_my_status_test.go index da969c0..4b0fb10 100644 --- a/web/auth/api_my_status_test.go +++ b/web/auth/api_my_status_test.go @@ -21,7 +21,7 @@ func TestAPIMyStatus(t *testing.T) { // invalid err = s.Request(http.MethodGet, "/api/v1/my/auth/status", nil, http.StatusUnauthorized, &hErr) assert.NoError(err) - assert.Equal(APIErrorNoSession, hErr.Message) + assert.Equal(ErrAPINoSession.Error(), hErr.Message) err = s.TestLogin() assert.NoError(err) diff --git a/web/auth/api_password_code.go b/web/auth/api_password_code.go index 8157354..058b642 100644 --- a/web/auth/api_password_code.go +++ b/web/auth/api_password_code.go @@ -33,7 +33,7 @@ func apiPasswordCode(r *gin.Engine, ws *web.Service) { var req PasswordWithForgetCode if err := c.BindJSON(&req); err != nil { c.JSON(http.StatusBadRequest, web.HTTPError{ - Message: web.APIErrorInvalidRequestFormat, + Message: web.ErrAPIInvalidRequestFormat.Error(), Error: err.Error(), }) return @@ -42,20 +42,20 @@ func apiPasswordCode(r *gin.Engine, ws *web.Service) { if err := ws.DB.Where("forget_code", req.ForgetCode).First(&d).Error; err != nil { if errors.Is(err, gorm.ErrRecordNotFound) { c.JSON(http.StatusBadRequest, web.HTTPError{ - Message: APIErrorUserNotFound, + Message: ErrAPIUserNotFound.Error(), Error: err.Error(), }) return } c.JSON(http.StatusInternalServerError, web.HTTPError{ - Message: APIErrroCreatePassword, + Message: ErrAPICreatePassword.Error(), Error: err.Error(), }) return } if err := d.SetPassword(req.Password); err != nil { c.JSON(http.StatusInternalServerError, web.HTTPError{ - Message: APIErrroCreatePassword, + Message: ErrAPICreatePassword.Error(), Error: err.Error(), }) return @@ -64,7 +64,7 @@ func apiPasswordCode(r *gin.Engine, ws *web.Service) { if err := ws.DB.Save(&d).Error; err != nil { c.JSON(http.StatusInternalServerError, web.HTTPError{ - Message: web.APIErrorInternalDatabase, + Message: web.ErrAPIInternalDatabase.Error(), Error: err.Error(), }) return diff --git a/web/auth/api_password_code_test.go b/web/auth/api_password_code_test.go index ec0c0a9..e567576 100644 --- a/web/auth/api_password_code_test.go +++ b/web/auth/api_password_code_test.go @@ -28,7 +28,7 @@ func TestAPIPasswordCode(t *testing.T) { // invalid err = s.Request(http.MethodPost, "/api/v1/auth/password/code", &passwordNew, http.StatusBadRequest, &hErr) assert.NoError(err) - assert.Equal(web.APIErrorInvalidRequestFormat, hErr.Message) + assert.Equal(web.ErrAPIInvalidRequestFormat.Error(), hErr.Message) res := "" // set new password @@ -46,7 +46,7 @@ func TestAPIPasswordCode(t *testing.T) { Password: passwordCurrent, }, http.StatusBadRequest, &hErr) assert.NoError(err) - assert.Equal(APIErrorUserNotFound, hErr.Message) + assert.Equal(ErrAPIUserNotFound.Error(), hErr.Message) forgetCode = uuid.New() s.DB.DB.Model(&User{ID: TestUser1ID}).Update("forget_code", forgetCode) diff --git a/web/auth/error.go b/web/auth/error.go index 2fed1c7..40e8c5e 100644 --- a/web/auth/error.go +++ b/web/auth/error.go @@ -1,15 +1,20 @@ package auth -const ( - // APIErrorUserNotFound api error string if user not found - APIErrorUserNotFound string = "user not found" - // APIErrorIncorrectPassword api error string if password is incorrect - APIErrorIncorrectPassword string = "incorrect password" - // APIErrorNoSession api error string if no session exists - APIErrorNoSession string = "no session" - // APIErrorCreateSession api error string if session could not created - APIErrorCreateSession string = "create session" +import "errors" - // APIErrroCreatePassword api error string if password could not created - APIErrroCreatePassword string = "error during create password" +var ( + // ErrAPIUserNotFound api error string if user not found + ErrAPIUserNotFound = errors.New("user not found") + // ErrAPIIncorrectPassword api error string if password is incorrect + ErrAPIIncorrectPassword = errors.New("incorrect password") + // ErrAPINoSession api error string if no session exists + ErrAPINoSession = errors.New("no session") + // ErrAPICreateSession api error string if session could not created + ErrAPICreateSession = errors.New("create session") + + // ErrAPICreatePassword api error string if password could not created + ErrAPICreatePassword = errors.New("error during create password") + + // ErrAPINoPermission api error string if an error happen on accesing this object + ErrAPINoPermission = errors.New("error on access an object") ) diff --git a/web/auth/lib_user.go b/web/auth/lib_user.go index 70609a7..36bd023 100644 --- a/web/auth/lib_user.go +++ b/web/auth/lib_user.go @@ -19,7 +19,7 @@ func GetCurrentUserID(c *gin.Context) (uuid.UUID, bool) { v := session.Get("user_id") if v == nil { c.JSON(http.StatusUnauthorized, web.HTTPError{ - Message: APIErrorNoSession, + Message: ErrAPINoSession.Error(), }) return uuid.Nil, false } @@ -38,13 +38,13 @@ func GetCurrentUser(c *gin.Context, ws *web.Service) (*User, bool) { if err := ws.DB.First(d).Error; err != nil { if errors.Is(err, gorm.ErrRecordNotFound) { c.JSON(http.StatusUnauthorized, web.HTTPError{ - Message: APIErrorUserNotFound, + Message: ErrAPIUserNotFound.Error(), Error: err.Error(), }) return nil, false } c.JSON(http.StatusInternalServerError, web.HTTPError{ - Message: web.APIErrorInternalDatabase, + Message: web.ErrAPIInternalDatabase.Error(), Error: err.Error(), }) return nil, false diff --git a/web/auth/middleware.go b/web/auth/middleware.go index 58616d4..bd8916b 100644 --- a/web/auth/middleware.go +++ b/web/auth/middleware.go @@ -34,7 +34,7 @@ func MiddlewarePermissionParam(ws *web.Service, obj HasPermission, param string) objID, err := uuid.Parse(c.Params.ByName(param)) if err != nil { c.JSON(http.StatusUnauthorized, web.HTTPError{ - Message: web.APIErrorInvalidRequestFormat, + Message: web.ErrAPIInvalidRequestFormat.Error(), Error: err.Error(), }) c.Abort() @@ -42,7 +42,7 @@ func MiddlewarePermissionParam(ws *web.Service, obj HasPermission, param string) _, err = obj.HasPermission(ws.DB, userID, objID) if err != nil { c.JSON(http.StatusUnauthorized, web.HTTPError{ - Message: http.StatusText(http.StatusUnauthorized), + Message: ErrAPINoPermission.Error(), Error: err.Error(), }) c.Abort() diff --git a/web/error.go b/web/error.go index a88a51e..26c10ec 100644 --- a/web/error.go +++ b/web/error.go @@ -1,5 +1,7 @@ package web +import "errors" + // HTTPError is returned in HTTP error responses. type HTTPError struct { Message string `json:"message" example:"invalid format"` @@ -8,8 +10,8 @@ type HTTPError struct { } // Error strings used for HTTPError.Message. -const ( - APIErrorInvalidRequestFormat = "Invalid Request Format" - APIErrorInternalDatabase = "Internal Database Error" - APIErrorNotFound = "Not found" +var ( + ErrAPIInvalidRequestFormat = errors.New("Invalid Request Format") + ErrAPIInternalDatabase = errors.New("Internal Database Error") + ErrAPINotFound = errors.New("Not found") )