web: error as errors
continuous-integration/drone the build is pending Details

This commit is contained in:
Geno 2021-07-22 18:16:05 +02:00
parent 9542ac4272
commit 32f0d84427
13 changed files with 50 additions and 43 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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