From 2c593de9a48d107dac2b4b76e4e310d28fcc2d78 Mon Sep 17 00:00:00 2001 From: Ming Deng Date: Thu, 14 Mar 2024 20:54:11 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=20ErrNoResponse=20=E4=BB=A5?= =?UTF-8?q?=E8=A7=84=E9=81=BF=E5=86=99=E4=B8=A4=E6=AC=A1=E5=93=8D=E5=BA=94?= =?UTF-8?q?=20(#19)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- error.go | 19 +++++++++++++++++++ internal/errs/error.go | 5 +++++ wrapper_func.go | 16 ++++++++++++++++ 3 files changed, 40 insertions(+) create mode 100644 error.go diff --git a/error.go b/error.go new file mode 100644 index 0000000..3f0a0a8 --- /dev/null +++ b/error.go @@ -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 diff --git a/internal/errs/error.go b/internal/errs/error.go index ccd9085..d89da3d 100644 --- a/internal/errs/error.go +++ b/internal/errs/error.go @@ -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") diff --git a/wrapper_func.go b/wrapper_func.go index 54e135f..081060c 100644 --- a/wrapper_func.go +++ b/wrapper_func.go @@ -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) @@ -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) @@ -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)) @@ -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))