Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: unified cookie exception error return #133

Merged
merged 4 commits into from
Dec 9, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions api/rpc/academic.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@
logger.Errorf("GetScoresRPC: RPC called failed: %v", err.Error())
return nil, errno.InternalServiceError.WithMessage(err.Error())
}
if resp.Base.Code == errno.AuthJwchCookieExceptionCode {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个 code check 归类到 if !utils.IsSuccess(resp.Base)里面吧,算作错误处理

你这个单开一个和下面的逻辑就混了

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

我尝试整合了,确认一下有没有问题

return nil, errno.NewErrNo(resp.Base.Code, resp.Base.Msg)
}

Check warning on line 46 in api/rpc/academic.go

View check run for this annotation

Codecov / codecov/patch

api/rpc/academic.go#L44-L46

Added lines #L44 - L46 were not covered by tests
if !utils.IsSuccess(resp.Base) {
return nil, errno.BizError.WithMessage(resp.Base.Msg)
}
Expand All @@ -53,6 +56,9 @@
logger.Errorf("GetGPARPC: RPC called failed: %v", err.Error())
return nil, errno.InternalServiceError.WithMessage(err.Error())
}
if resp.Base.Code == errno.AuthJwchCookieExceptionCode {
return nil, errno.NewErrNo(resp.Base.Code, resp.Base.Msg)
}

Check warning on line 61 in api/rpc/academic.go

View check run for this annotation

Codecov / codecov/patch

api/rpc/academic.go#L59-L61

Added lines #L59 - L61 were not covered by tests
if !utils.IsSuccess(resp.Base) {
return nil, errno.BizError.WithMessage(resp.Base.Msg)
}
Expand All @@ -65,6 +71,9 @@
logger.Errorf("GetCreditRPC: RPC called failed: %v", err.Error())
return nil, errno.InternalServiceError.WithMessage(err.Error())
}
if resp.Base.Code == errno.AuthJwchCookieExceptionCode {
return nil, errno.NewErrNo(resp.Base.Code, resp.Base.Msg)
}

Check warning on line 76 in api/rpc/academic.go

View check run for this annotation

Codecov / codecov/patch

api/rpc/academic.go#L74-L76

Added lines #L74 - L76 were not covered by tests
if !utils.IsSuccess(resp.Base) {
return nil, errno.BizError.WithMessage(resp.Base.Msg)
}
Expand All @@ -78,6 +87,9 @@
logger.Errorf("GetUnifiedExamRPC: RPC called failed: %v", err.Error())
return nil, errno.InternalServiceError.WithMessage(err.Error())
}
if resp.Base.Code == errno.AuthJwchCookieExceptionCode {
return nil, errno.NewErrNo(resp.Base.Code, resp.Base.Msg)
}

Check warning on line 92 in api/rpc/academic.go

View check run for this annotation

Codecov / codecov/patch

api/rpc/academic.go#L90-L92

Added lines #L90 - L92 were not covered by tests
if !utils.IsSuccess(resp.Base) {
return nil, errno.BizError.WithMessage(resp.Base.Msg)
}
Expand Down
3 changes: 3 additions & 0 deletions api/rpc/classroom.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@
logger.Errorf("GetExamRoomInfoRPC: RPC called failed: %v", err.Error())
return nil, errno.InternalServiceError.WithMessage(err.Error())
}
if resp.Base.Code == errno.AuthJwchCookieExceptionCode {
return nil, errno.NewErrNo(resp.Base.Code, resp.Base.Msg)
}

Check warning on line 58 in api/rpc/classroom.go

View check run for this annotation

Codecov / codecov/patch

api/rpc/classroom.go#L56-L58

Added lines #L56 - L58 were not covered by tests
if !utils.IsSuccess(resp.Base) {
return nil, errno.BizError.WithMessage(resp.Base.Msg)
}
Expand Down
4 changes: 3 additions & 1 deletion api/rpc/course.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@
logger.Errorf("GetCourseListRPC: RPC called failed: %v", err.Error())
return nil, errno.InternalServiceError.WithMessage(err.Error())
}

if resp.Base.Code == errno.AuthJwchCookieExceptionCode {
return nil, errno.NewErrNo(resp.Base.Code, resp.Base.Msg)
}

Check warning on line 46 in api/rpc/course.go

View check run for this annotation

Codecov / codecov/patch

api/rpc/course.go#L44-L46

Added lines #L44 - L46 were not covered by tests
if !utils.IsSuccess(resp.Base) {
return nil, errno.BizError.WithMessage(resp.Base.Msg)
}
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ require (
github.com/segmentio/kafka-go v0.4.47
github.com/spf13/viper v1.19.0
github.com/upyun/go-sdk/v3 v3.0.4
github.com/west2-online/jwch v0.1.8
github.com/west2-online/jwch v0.2.0
gorm.io/driver/mysql v1.5.7
gorm.io/gorm v1.25.12
k8s.io/client-go v0.31.3
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -727,8 +727,8 @@ github.com/upyun/go-sdk/v3 v3.0.4 h1:2DCJa/Yi7/3ZybT9UCPATSzvU3wpPPxhXinNlb1Hi8Q
github.com/upyun/go-sdk/v3 v3.0.4/go.mod h1:P/SnuuwhrIgAVRd/ZpzDWqCsBAf/oHg7UggbAxyZa0E=
github.com/urfave/cli v1.20.0/go.mod h1:70zkFmudgCuE/ngEzBv17Jvp/497gISqfk5gWijbERA=
github.com/urfave/cli v1.22.1/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtXRu0=
github.com/west2-online/jwch v0.1.8 h1:ShBTrbqnDPurv1QsBGtfTuz3qMNcbpHq9WvsOydY+Zc=
github.com/west2-online/jwch v0.1.8/go.mod h1:0gfO+djdY0BmmFtjb45uDzxDLNWCbT0f9wVzw5OQBXI=
github.com/west2-online/jwch v0.2.0 h1:TQOThDZOJ5r8tEdcHJY0sRxtVidQuB5M0IDblmRaPCA=
github.com/west2-online/jwch v0.2.0/go.mod h1:0gfO+djdY0BmmFtjb45uDzxDLNWCbT0f9wVzw5OQBXI=
github.com/xdg-go/pbkdf2 v1.0.0 h1:Su7DPu48wXMwC3bs7MCNG+z4FhcyEuz5dlvchbq0B0c=
github.com/xdg-go/pbkdf2 v1.0.0/go.mod h1:jrpuAogTd400dnrH08LKmI/xc1MbPOebTwRqcT5RDeI=
github.com/xdg-go/scram v1.1.2 h1:FHX5I5B4i4hKRVRBCFRxq1iQRej7WO3hhBuJf+UUySY=
Expand Down
3 changes: 2 additions & 1 deletion internal/academic/service/get_credit.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,15 @@ import (
"fmt"

"github.com/west2-online/fzuhelper-server/kitex_gen/academic"
"github.com/west2-online/fzuhelper-server/pkg/base"
"github.com/west2-online/fzuhelper-server/pkg/utils"
"github.com/west2-online/jwch"
)

func (s *AcademicService) GetCredit(req *academic.GetCreditRequest) ([]*jwch.CreditStatistics, error) {
stu := jwch.NewStudent().WithLoginData(req.Id, utils.ParseCookies(req.Cookies))
credit, err := stu.GetCredit()
if err != nil {
if err = base.HandleErrorSolve(err); err != nil {
return nil, fmt.Errorf("service.Credit: Get credit info fail %w", err)
}

Expand Down
3 changes: 2 additions & 1 deletion internal/academic/service/get_gpa.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,15 @@ import (
"fmt"

"github.com/west2-online/fzuhelper-server/kitex_gen/academic"
"github.com/west2-online/fzuhelper-server/pkg/base"
"github.com/west2-online/fzuhelper-server/pkg/utils"
"github.com/west2-online/jwch"
)

func (s *AcademicService) GetGPA(req *academic.GetGPARequest) (*jwch.GPABean, error) {
stu := jwch.NewStudent().WithLoginData(req.Id, utils.ParseCookies(req.Cookies))
gpa, err := stu.GetGPA()
if err != nil {
if err = base.HandleErrorSolve(err); err != nil {
return nil, fmt.Errorf("service.GetGPA: Get gpa info fail %w", err)
}

Expand Down
3 changes: 2 additions & 1 deletion internal/academic/service/get_scores.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,15 @@ import (
"fmt"

"github.com/west2-online/fzuhelper-server/kitex_gen/academic"
"github.com/west2-online/fzuhelper-server/pkg/base"
"github.com/west2-online/fzuhelper-server/pkg/utils"
"github.com/west2-online/jwch"
)

func (s *AcademicService) GetScores(req *academic.GetScoresRequest) ([]*jwch.Mark, error) {
stu := jwch.NewStudent().WithLoginData(req.Id, utils.ParseCookies(req.Cookies))
scores, err := stu.GetMarks()
if err != nil {
if err = base.HandleErrorSolve(err); err != nil {
return nil, fmt.Errorf("service.GetScores: Get scores info fail %w", err)
}

Expand Down
5 changes: 3 additions & 2 deletions internal/academic/service/get_unified.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,19 @@ import (
"fmt"

"github.com/west2-online/fzuhelper-server/kitex_gen/academic"
"github.com/west2-online/fzuhelper-server/pkg/base"
"github.com/west2-online/fzuhelper-server/pkg/utils"
"github.com/west2-online/jwch"
)

func (s *AcademicService) GetUnifiedExam(req *academic.GetUnifiedExamRequest) ([]*jwch.UnifiedExam, error) {
stu := jwch.NewStudent().WithLoginData(req.Id, utils.ParseCookies(req.Cookies))
cet, err := stu.GetCET()
if err != nil {
if err = base.HandleErrorSolve(err); err != nil {
return nil, fmt.Errorf("service.GetUnifiedExam: Get cet info fail %w", err)
}
js, err := stu.GetJS()
if err != nil {
if err = base.HandleErrorSolve(err); err != nil {
return nil, fmt.Errorf("service.GetUnifiedExam: Get js info fail %w", err)
}
unifiedExam := append(append([]*jwch.UnifiedExam{}, cet...), js...)
Expand Down
3 changes: 2 additions & 1 deletion internal/classroom/service/get_exam_room.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,15 @@ import (
"fmt"

"github.com/west2-online/fzuhelper-server/kitex_gen/classroom"
"github.com/west2-online/fzuhelper-server/pkg/base"
"github.com/west2-online/fzuhelper-server/pkg/utils"
"github.com/west2-online/jwch"
)

func (s *ClassroomService) GetExamRoomInfo(req *classroom.ExamRoomInfoRequest) ([]*jwch.ExamRoomInfo, error) {
stu := jwch.NewStudent().WithLoginData(req.LoginData.Id, utils.ParseCookies(req.LoginData.Cookies))
rooms, err := stu.GetExamRoom(jwch.ExamRoomReq{Term: req.Term})
if err != nil {
if err = base.HandleErrorSolve(err); err != nil {
return nil, fmt.Errorf("service.GetExamRoomInfo: Get exam room info fail %w", err)
}
return rooms, nil
Expand Down
5 changes: 3 additions & 2 deletions internal/course/service/get_course_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"slices"

"github.com/west2-online/fzuhelper-server/kitex_gen/course"
"github.com/west2-online/fzuhelper-server/pkg/base"
"github.com/west2-online/fzuhelper-server/pkg/db/model"
"github.com/west2-online/fzuhelper-server/pkg/logger"
"github.com/west2-online/fzuhelper-server/pkg/utils"
Expand All @@ -32,7 +33,7 @@ func (s *CourseService) GetCourseList(req *course.CourseListRequest) ([]*jwch.Co
stu := jwch.NewStudent().WithLoginData(req.LoginData.Id, utils.ParseCookies(req.LoginData.Cookies))

terms, err := stu.GetTerms()
if err != nil {
if err = base.HandleErrorSolve(err); err != nil {
jiuxia211 marked this conversation as resolved.
Show resolved Hide resolved
return nil, fmt.Errorf("service.GetCourseList: Get terms failed: %w", err)
}

Expand All @@ -42,7 +43,7 @@ func (s *CourseService) GetCourseList(req *course.CourseListRequest) ([]*jwch.Co
}

courses, err := stu.GetSemesterCourses(req.Term, terms.ViewState, terms.EventValidation)
if err != nil {
if err = base.HandleErrorSolve(err); err != nil {
return nil, fmt.Errorf("service.GetCourseList: Get semester courses failed: %w", err)
}

Expand Down
14 changes: 14 additions & 0 deletions pkg/base/pack.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,12 @@
package base

import (
"errors"

"github.com/west2-online/fzuhelper-server/kitex_gen/model"
"github.com/west2-online/fzuhelper-server/pkg/errno"
"github.com/west2-online/fzuhelper-server/pkg/logger"
jwchErrno "github.com/west2-online/jwch/errno"
)

func BuildBaseResp(err error) *model.BaseResp {
Expand Down Expand Up @@ -72,3 +75,14 @@
Msg: Errno.ErrorMsg,
}
}

// HandleErrorSolve 对于jwch库返回的错误类型,需要使用 HandleErrorSolve 来保留 cookie 异常
func HandleErrorSolve(err error) error {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

函数名改成 HandleError 或者别的吧。。你不觉得这个有语病吗。。

var jwchErr jwchErrno.ErrNo
if errors.As(err, &jwchErr) {
if errors.Is(jwchErr, jwchErrno.CookieError) {
return errno.NewErrNo(errno.AuthJwchCookieExceptionCode, jwchErr.ErrorMsg)
}

Check warning on line 85 in pkg/base/pack.go

View check run for this annotation

Codecov / codecov/patch

pkg/base/pack.go#L83-L85

Added lines #L83 - L85 were not covered by tests
}
return err
}
6 changes: 3 additions & 3 deletions pkg/constants/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ const (
TypeAccessToken = 0
TypeRefreshToken = 1

AccessTokenTTL = time.Minute * 15 // Access Token 有效期15分钟
RefreshTokenTTL = time.Hour * 24 * 7 // Refresh Token 有效期7天
Issuer = "west2-online" // token 颁发者
AccessTokenTTL = time.Hour * 24 * 7 // Access Token 有效期7天
RefreshTokenTTL = time.Hour * 24 * 30 // Refresh Token 有效期30天
Issuer = "west2-online" // token 颁发者

AuthHeader = "Authorization" // 获取 Token 时的请求头
AccessTokenHeader = "Access-Token" // 响应时的访问令牌头
Expand Down
11 changes: 6 additions & 5 deletions pkg/errno/code.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,12 @@ const (
ParamFileNotExistCode = 20012 // 文件不存在
ParamFileReadErrorCode = 20013 // 文件读取错误

AuthErrorCode = 30001 // 鉴权错误
AuthInvalidCode = 30002 // 鉴权无效
AuthAccessExpiredCode = 30003 // 访问令牌过期
AuthRefreshExpiredCode = 30004 // 刷新令牌过期
AuthMissingCode = 30005 // 鉴权缺失
AuthErrorCode = 30001 // 鉴权错误
AuthInvalidCode = 30002 // 鉴权无效
AuthAccessExpiredCode = 30003 // 访问令牌过期
AuthRefreshExpiredCode = 30004 // 刷新令牌过期
AuthMissingCode = 30005 // 鉴权缺失
AuthJwchCookieExceptionCode = 30006 // jwch cookie异常
jiuxia211 marked this conversation as resolved.
Show resolved Hide resolved

BizErrorCode = 40001 // 业务错误
BizLogicCode = 40002 // 业务逻辑错误
Expand Down
Loading