Skip to content

Commit

Permalink
refactor: rename error with Err prefix to follow Go conventions (#1987
Browse files Browse the repository at this point in the history
)

* Replace `strings.Index` with `strings.Contains` for better readability

Replace `strings.Index(str, substr) > -1` with `strings.Contains(str, substr)`
使用 strings.Contains 替代 strings.Index 方法

* refactor: rename error with `Err` prefix to follow Go conventions

- Rename JWT error variables with `Err` prefix following Go conventions.
- Improve error messages to be more consistent and lowercase, more to see https://go.dev/wiki/CodeReviewComments#error-strings
- Update JWT error references in middleware
This change improves code readability and follows better Go naming conventions for error variables.
  • Loading branch information
huiyifyj authored Jan 17, 2025
1 parent 38d4f25 commit 1e727d2
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 19 deletions.
9 changes: 4 additions & 5 deletions server/middleware/jwt.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,15 @@ package middleware

import (
"errors"
"github.com/flipped-aurora/gin-vue-admin/server/global"
"github.com/flipped-aurora/gin-vue-admin/server/utils"
"github.com/golang-jwt/jwt/v4"
"strconv"
"time"

"github.com/flipped-aurora/gin-vue-admin/server/global"
"github.com/flipped-aurora/gin-vue-admin/server/model/common/response"
"github.com/flipped-aurora/gin-vue-admin/server/service"

"github.com/flipped-aurora/gin-vue-admin/server/utils"
"github.com/gin-gonic/gin"
"github.com/golang-jwt/jwt/v4"
)

var jwtService = service.ServiceGroupApp.SystemServiceGroup.JwtService
Expand All @@ -35,7 +34,7 @@ func JWTAuth() gin.HandlerFunc {
// parseToken 解析token包含的信息
claims, err := j.ParseToken(token)
if err != nil {
if errors.Is(err, utils.TokenExpired) {
if errors.Is(err, utils.ErrTokenExpired) {
response.NoAuth("授权已过期", c)
utils.ClearToken(c)
c.Abort()
Expand Down
2 changes: 1 addition & 1 deletion server/utils/breakpoint_continue.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func CheckMd5(content []byte, chunkMd5 string) (CanUpload bool) {
//@return: string, error

func makeFileContent(content []byte, fileName string, FileDir string, contentNumber int) (string, error) {
if strings.Index(fileName, "..") > -1 || strings.Index(FileDir, "..") > -1 {
if strings.Contains(fileName, "..") || strings.Contains(FileDir, "..") {
return "", errors.New("文件名或路径不合法")
}
path := FileDir + fileName + "_" + strconv.Itoa(contentNumber)
Expand Down
23 changes: 11 additions & 12 deletions server/utils/jwt.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,20 @@ import (
"errors"
"time"

jwt "github.com/golang-jwt/jwt/v4"

"github.com/flipped-aurora/gin-vue-admin/server/global"
"github.com/flipped-aurora/gin-vue-admin/server/model/system/request"
"github.com/golang-jwt/jwt/v4"
)

type JWT struct {
SigningKey []byte
}

var (
TokenExpired = errors.New("Token is expired")
TokenNotValidYet = errors.New("Token not active yet")
TokenMalformed = errors.New("That's not even a token")
TokenInvalid = errors.New("Couldn't handle this token:")
ErrTokenExpired = errors.New("token is expired")
ErrTokenNotValidYet = errors.New("token not active yet")
ErrTokenMalformed = errors.New("that's not even a token")
ErrTokenInvalid = errors.New("couldn't handle this token")
)

func NewJWT() *JWT {
Expand Down Expand Up @@ -65,24 +64,24 @@ func (j *JWT) ParseToken(tokenString string) (*request.CustomClaims, error) {
if err != nil {
if ve, ok := err.(*jwt.ValidationError); ok {
if ve.Errors&jwt.ValidationErrorMalformed != 0 {
return nil, TokenMalformed
return nil, ErrTokenMalformed
} else if ve.Errors&jwt.ValidationErrorExpired != 0 {
// Token is expired
return nil, TokenExpired
return nil, ErrTokenExpired
} else if ve.Errors&jwt.ValidationErrorNotValidYet != 0 {
return nil, TokenNotValidYet
return nil, ErrTokenNotValidYet
} else {
return nil, TokenInvalid
return nil, ErrTokenInvalid
}
}
}
if token != nil {
if claims, ok := token.Claims.(*request.CustomClaims); ok && token.Valid {
return claims, nil
}
return nil, TokenInvalid
return nil, ErrTokenInvalid

} else {
return nil, TokenInvalid
return nil, ErrTokenInvalid
}
}
2 changes: 1 addition & 1 deletion server/utils/zip.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func Unzip(zipFile string, destDir string) ([]string, error) {
defer zipReader.Close()

for _, f := range zipReader.File {
if strings.Index(f.Name, "..") > -1 {
if strings.Contains(f.Name, "..") {
return []string{}, fmt.Errorf("%s 文件名不合法", f.Name)
}
fpath := filepath.Join(destDir, f.Name)
Expand Down

0 comments on commit 1e727d2

Please sign in to comment.