Skip to content

Commit

Permalink
fix: 开放ErrUnauthorized错误供外界使用 (#23)
Browse files Browse the repository at this point in the history
Signed-off-by: longyue0521 <[email protected]>
  • Loading branch information
longyue0521 authored Apr 5, 2024
1 parent 80840e6 commit b236eb6
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 5 deletions.
1 change: 1 addition & 0 deletions error.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ package ginx
import "github.com/ecodeclub/ginx/internal/errs"

var ErrNoResponse = errs.ErrNoResponse
var ErrUnauthorized = errs.ErrUnauthorized
9 changes: 4 additions & 5 deletions wrapper_func.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
"log/slog"
"net/http"

"github.com/ecodeclub/ginx/internal/errs"
"github.com/ecodeclub/ginx/session"
"github.com/gin-gonic/gin"
)
Expand All @@ -31,7 +30,7 @@ func W(fn func(ctx *Context) (Result, error)) gin.HandlerFunc {
slog.Debug("不需要响应", slog.Any("err", err))
return
}
if errors.Is(err, errs.ErrUnauthorized) {
if errors.Is(err, ErrUnauthorized) {
slog.Debug("未授权", slog.Any("err", err))
ctx.AbortWithStatus(http.StatusUnauthorized)
return
Expand All @@ -57,7 +56,7 @@ func B[Req any](fn func(ctx *Context, req Req) (Result, error)) gin.HandlerFunc
slog.Debug("不需要响应", slog.Any("err", err))
return
}
if errors.Is(err, errs.ErrUnauthorized) {
if errors.Is(err, ErrUnauthorized) {
slog.Debug("未授权", slog.Any("err", err))
ctx.AbortWithStatus(http.StatusUnauthorized)
return
Expand Down Expand Up @@ -93,7 +92,7 @@ func BS[Req any](fn func(ctx *Context, req Req, sess session.Session) (Result, e
return
}
// 如果里面有权限校验,那么会返回 401 错误(目前来看,主要是登录态校验)
if errors.Is(err, errs.ErrUnauthorized) {
if errors.Is(err, ErrUnauthorized) {
slog.Debug("未授权", slog.Any("err", err))
ctx.AbortWithStatus(http.StatusUnauthorized)
return
Expand Down Expand Up @@ -123,7 +122,7 @@ func S(fn func(ctx *Context, sess session.Session) (Result, error)) gin.HandlerF
return
}
// 如果里面有权限校验,那么会返回 401 错误(目前来看,主要是登录态校验)
if errors.Is(err, errs.ErrUnauthorized) {
if errors.Is(err, ErrUnauthorized) {
slog.Debug("未授权", slog.Any("err", err))
ctx.AbortWithStatus(http.StatusUnauthorized)
return
Expand Down

0 comments on commit b236eb6

Please sign in to comment.