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

增加 ErrNoResponse 以规避写两次响应 #19

Merged
merged 1 commit into from
Mar 14, 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
19 changes: 19 additions & 0 deletions error.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright 2023 ecodeclub
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package ginx

import "github.com/ecodeclub/ginx/internal/errs"

var ErrNoResponse = errs.ErrNoResponse
5 changes: 5 additions & 0 deletions internal/errs/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,8 @@ import "errors"

var ErrUnauthorized = errors.New("未授权")
var ErrSessionKeyNotFound = errors.New("session 中没找到对应的 key")

// ErrNoResponse 是一个 sentinel 错误。
// 也就是说,你可以通过返回这个 ErrNoResponse 来告诉 ginx 不需要继续写响应。
// 大多数情况下,这意味着你已经写回了响应。
var ErrNoResponse = errors.New("不需要返回 response")
16 changes: 16 additions & 0 deletions wrapper_func.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ import (
func W(fn func(ctx *Context) (Result, error)) gin.HandlerFunc {
return func(ctx *gin.Context) {
res, err := fn(&Context{Context: ctx})
if errors.Is(err, ErrNoResponse) {
slog.Debug("不需要响应", slog.Any("err", err))
return
}
if errors.Is(err, errs.ErrUnauthorized) {
slog.Debug("未授权", slog.Any("err", err))
ctx.AbortWithStatus(http.StatusUnauthorized)
Expand All @@ -49,6 +53,10 @@ func B[Req any](fn func(ctx *Context, req Req) (Result, error)) gin.HandlerFunc
return
}
res, err := fn(&Context{Context: ctx}, req)
if errors.Is(err, ErrNoResponse) {
slog.Debug("不需要响应", slog.Any("err", err))
return
}
if errors.Is(err, errs.ErrUnauthorized) {
slog.Debug("未授权", slog.Any("err", err))
ctx.AbortWithStatus(http.StatusUnauthorized)
Expand Down Expand Up @@ -80,6 +88,10 @@ func BS[Req any](fn func(ctx *Context, req Req, sess session.Session) (Result, e
return
}
res, err := fn(gtx, req, sess)
if errors.Is(err, ErrNoResponse) {
slog.Debug("不需要响应", slog.Any("err", err))
return
}
// 如果里面有权限校验,那么会返回 401 错误(目前来看,主要是登录态校验)
if errors.Is(err, errs.ErrUnauthorized) {
slog.Debug("未授权", slog.Any("err", err))
Expand All @@ -106,6 +118,10 @@ func S(fn func(ctx *Context, sess session.Session) (Result, error)) gin.HandlerF
return
}
res, err := fn(gtx, sess)
if errors.Is(err, ErrNoResponse) {
slog.Debug("不需要响应", slog.Any("err", err))
return
}
// 如果里面有权限校验,那么会返回 401 错误(目前来看,主要是登录态校验)
if errors.Is(err, errs.ErrUnauthorized) {
slog.Debug("未授权", slog.Any("err", err))
Expand Down
Loading