Skip to content

Commit 1e727d2

Browse files
authored
refactor: rename error with Err prefix to follow Go conventions (#1987)
* 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.
1 parent 38d4f25 commit 1e727d2

File tree

4 files changed

+17
-19
lines changed

4 files changed

+17
-19
lines changed

Diff for: server/middleware/jwt.go

+4-5
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,15 @@ package middleware
22

33
import (
44
"errors"
5-
"github.com/flipped-aurora/gin-vue-admin/server/global"
6-
"github.com/flipped-aurora/gin-vue-admin/server/utils"
7-
"github.com/golang-jwt/jwt/v4"
85
"strconv"
96
"time"
107

8+
"github.com/flipped-aurora/gin-vue-admin/server/global"
119
"github.com/flipped-aurora/gin-vue-admin/server/model/common/response"
1210
"github.com/flipped-aurora/gin-vue-admin/server/service"
13-
11+
"github.com/flipped-aurora/gin-vue-admin/server/utils"
1412
"github.com/gin-gonic/gin"
13+
"github.com/golang-jwt/jwt/v4"
1514
)
1615

1716
var jwtService = service.ServiceGroupApp.SystemServiceGroup.JwtService
@@ -35,7 +34,7 @@ func JWTAuth() gin.HandlerFunc {
3534
// parseToken 解析token包含的信息
3635
claims, err := j.ParseToken(token)
3736
if err != nil {
38-
if errors.Is(err, utils.TokenExpired) {
37+
if errors.Is(err, utils.ErrTokenExpired) {
3938
response.NoAuth("授权已过期", c)
4039
utils.ClearToken(c)
4140
c.Abort()

Diff for: server/utils/breakpoint_continue.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ func CheckMd5(content []byte, chunkMd5 string) (CanUpload bool) {
5555
//@return: string, error
5656

5757
func makeFileContent(content []byte, fileName string, FileDir string, contentNumber int) (string, error) {
58-
if strings.Index(fileName, "..") > -1 || strings.Index(FileDir, "..") > -1 {
58+
if strings.Contains(fileName, "..") || strings.Contains(FileDir, "..") {
5959
return "", errors.New("文件名或路径不合法")
6060
}
6161
path := FileDir + fileName + "_" + strconv.Itoa(contentNumber)

Diff for: server/utils/jwt.go

+11-12
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,20 @@ import (
44
"errors"
55
"time"
66

7-
jwt "github.com/golang-jwt/jwt/v4"
8-
97
"github.com/flipped-aurora/gin-vue-admin/server/global"
108
"github.com/flipped-aurora/gin-vue-admin/server/model/system/request"
9+
"github.com/golang-jwt/jwt/v4"
1110
)
1211

1312
type JWT struct {
1413
SigningKey []byte
1514
}
1615

1716
var (
18-
TokenExpired = errors.New("Token is expired")
19-
TokenNotValidYet = errors.New("Token not active yet")
20-
TokenMalformed = errors.New("That's not even a token")
21-
TokenInvalid = errors.New("Couldn't handle this token:")
17+
ErrTokenExpired = errors.New("token is expired")
18+
ErrTokenNotValidYet = errors.New("token not active yet")
19+
ErrTokenMalformed = errors.New("that's not even a token")
20+
ErrTokenInvalid = errors.New("couldn't handle this token")
2221
)
2322

2423
func NewJWT() *JWT {
@@ -65,24 +64,24 @@ func (j *JWT) ParseToken(tokenString string) (*request.CustomClaims, error) {
6564
if err != nil {
6665
if ve, ok := err.(*jwt.ValidationError); ok {
6766
if ve.Errors&jwt.ValidationErrorMalformed != 0 {
68-
return nil, TokenMalformed
67+
return nil, ErrTokenMalformed
6968
} else if ve.Errors&jwt.ValidationErrorExpired != 0 {
7069
// Token is expired
71-
return nil, TokenExpired
70+
return nil, ErrTokenExpired
7271
} else if ve.Errors&jwt.ValidationErrorNotValidYet != 0 {
73-
return nil, TokenNotValidYet
72+
return nil, ErrTokenNotValidYet
7473
} else {
75-
return nil, TokenInvalid
74+
return nil, ErrTokenInvalid
7675
}
7776
}
7877
}
7978
if token != nil {
8079
if claims, ok := token.Claims.(*request.CustomClaims); ok && token.Valid {
8180
return claims, nil
8281
}
83-
return nil, TokenInvalid
82+
return nil, ErrTokenInvalid
8483

8584
} else {
86-
return nil, TokenInvalid
85+
return nil, ErrTokenInvalid
8786
}
8887
}

Diff for: server/utils/zip.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ func Unzip(zipFile string, destDir string) ([]string, error) {
1919
defer zipReader.Close()
2020

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

0 commit comments

Comments
 (0)