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 all 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
4 changes: 4 additions & 0 deletions api/handler/api/class_room_service.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 8 additions & 8 deletions api/rpc/academic.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@
logger.Errorf("GetScoresRPC: RPC called failed: %v", err.Error())
return nil, errno.InternalServiceError.WithMessage(err.Error())
}
if !utils.IsSuccess(resp.Base) {
return nil, errno.BizError.WithMessage(resp.Base.Msg)
if err = utils.HandleBaseRespWithCookie(resp.Base); err != nil {
return nil, err

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

View check run for this annotation

Codecov / codecov/patch

api/rpc/academic.go#L44-L45

Added lines #L44 - L45 were not covered by tests
}
return resp.Scores, nil
}
Expand All @@ -53,8 +53,8 @@
logger.Errorf("GetGPARPC: RPC called failed: %v", err.Error())
return nil, errno.InternalServiceError.WithMessage(err.Error())
}
if !utils.IsSuccess(resp.Base) {
return nil, errno.BizError.WithMessage(resp.Base.Msg)
if err = utils.HandleBaseRespWithCookie(resp.Base); err != nil {
return nil, err

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

View check run for this annotation

Codecov / codecov/patch

api/rpc/academic.go#L56-L57

Added lines #L56 - L57 were not covered by tests
}
return resp.Gpa, nil
}
Expand All @@ -65,8 +65,8 @@
logger.Errorf("GetCreditRPC: RPC called failed: %v", err.Error())
return nil, errno.InternalServiceError.WithMessage(err.Error())
}
if !utils.IsSuccess(resp.Base) {
return nil, errno.BizError.WithMessage(resp.Base.Msg)
if err = utils.HandleBaseRespWithCookie(resp.Base); err != nil {
return nil, err

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

View check run for this annotation

Codecov / codecov/patch

api/rpc/academic.go#L68-L69

Added lines #L68 - L69 were not covered by tests
}

return resp.Major, nil
Expand All @@ -78,8 +78,8 @@
logger.Errorf("GetUnifiedExamRPC: RPC called failed: %v", err.Error())
return nil, errno.InternalServiceError.WithMessage(err.Error())
}
if !utils.IsSuccess(resp.Base) {
return nil, errno.BizError.WithMessage(resp.Base.Msg)
if err = utils.HandleBaseRespWithCookie(resp.Base); err != nil {
return nil, err

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

View check run for this annotation

Codecov / codecov/patch

api/rpc/academic.go#L81-L82

Added lines #L81 - L82 were not covered by tests
}

return resp.UnifiedExam, nil
Expand Down
4 changes: 2 additions & 2 deletions api/rpc/classroom.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@
logger.Errorf("GetExamRoomInfoRPC: RPC called failed: %v", err.Error())
return nil, errno.InternalServiceError.WithMessage(err.Error())
}
if !utils.IsSuccess(resp.Base) {
return nil, errno.BizError.WithMessage(resp.Base.Msg)
if err = utils.HandleBaseRespWithCookie(resp.Base); err != nil {
return nil, err

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

View check run for this annotation

Codecov / codecov/patch

api/rpc/classroom.go#L56-L57

Added lines #L56 - L57 were not covered by tests
}
return resp.Rooms, nil
}
5 changes: 2 additions & 3 deletions api/rpc/course.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,8 @@
logger.Errorf("GetCourseListRPC: RPC called failed: %v", err.Error())
return nil, errno.InternalServiceError.WithMessage(err.Error())
}

if !utils.IsSuccess(resp.Base) {
return nil, errno.BizError.WithMessage(resp.Base.Msg)
if err = utils.HandleBaseRespWithCookie(resp.Base); err != nil {
return nil, err

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

View check run for this annotation

Codecov / codecov/patch

api/rpc/course.go#L44-L45

Added lines #L44 - L45 were not covered by tests
}

return resp.Data, nil
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.HandleJwchError(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.HandleJwchError(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.HandleJwchError(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.HandleJwchError(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.HandleJwchError(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.HandleJwchError(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.HandleJwchError(err); err != nil {
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.HandleJwchError(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,
}
}

// HandleJwchError 对于jwch库返回的错误类型,需要使用 HandleJwchError 来保留 cookie 异常
func HandleJwchError(err error) error {
var jwchErr jwchErrno.ErrNo
if errors.As(err, &jwchErr) {
if errors.Is(jwchErr, jwchErrno.CookieError) {
return errno.NewErrNo(errno.BizJwchCookieExceptionCode, 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 @@ -49,11 +49,12 @@ const (
AuthRefreshExpiredCode = 30004 // 刷新令牌过期
AuthMissingCode = 30005 // 鉴权缺失

BizErrorCode = 40001 // 业务错误
BizLogicCode = 40002 // 业务逻辑错误
BizLimitCode = 40003 // 业务限制错误
BizNotExist = 40005 // 业务不存在错误
BizFileUploadErrorCode = 40006 // 文件上传错误(service 层)
BizErrorCode = 40001 // 业务错误
BizLogicCode = 40002 // 业务逻辑错误
BizLimitCode = 40003 // 业务限制错误
BizNotExist = 40005 // 业务不存在错误
BizFileUploadErrorCode = 40006 // 文件上传错误(service 层)
BizJwchCookieExceptionCode = 40007 // jwch cookie异常

InternalServiceErrorCode = 50001 // 未知服务错误
InternalDatabaseErrorCode = 50002 // 数据库错误
Expand Down
11 changes: 11 additions & 0 deletions pkg/utils/rpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,14 @@
func IsSuccess(baseResp *model.BaseResp) bool {
return baseResp.Code == errno.SuccessCode
}

// HandleBaseRespWithCookie 调用jwch库的接口的结果处理
func HandleBaseRespWithCookie(baseResp *model.BaseResp) error {
if baseResp.Code == errno.BizJwchCookieExceptionCode {
return errno.NewErrNo(baseResp.Code, baseResp.Msg)
}
if baseResp.Code != errno.SuccessCode {
SchwarzSail marked this conversation as resolved.
Show resolved Hide resolved
return errno.BizError.WithMessage(baseResp.Msg)
}
return nil

Check warning on line 37 in pkg/utils/rpc.go

View check run for this annotation

Codecov / codecov/patch

pkg/utils/rpc.go#L30-L37

Added lines #L30 - L37 were not covered by tests
}
Loading