From 4826cdb1aea07e0d1afaae3c0b306d19cb261321 Mon Sep 17 00:00:00 2001 From: caixw Date: Mon, 11 Mar 2024 10:09:08 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20recover=20=E6=97=B6=E5=90=8C?= =?UTF-8?q?=E6=97=B6=E6=89=93=E5=8D=B0=E5=A0=86=E6=A0=88=E4=BF=A1=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- go.mod | 2 +- router.go | 9 +++++---- web.go | 2 +- 3 files changed, 7 insertions(+), 6 deletions(-) diff --git a/go.mod b/go.mod index f25b487b..acd7ae41 100644 --- a/go.mod +++ b/go.mod @@ -13,6 +13,7 @@ require ( github.com/issue9/query/v3 v3.1.3 github.com/issue9/scheduled v0.19.3 github.com/issue9/sliceutil v0.15.1 + github.com/issue9/source v0.8.2 github.com/issue9/term/v3 v3.2.7 github.com/issue9/unique/v2 v2.0.1 github.com/klauspost/compress v1.17.7 @@ -27,7 +28,6 @@ require ( github.com/cespare/xxhash/v2 v2.2.0 // indirect github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect github.com/issue9/rands/v2 v2.0.1 // indirect - github.com/issue9/source v0.8.2 // indirect github.com/redis/go-redis/v9 v9.5.1 // indirect golang.org/x/mod v0.16.0 // indirect golang.org/x/net v0.21.0 // indirect diff --git a/router.go b/router.go index d4a321b6..1b55be4a 100644 --- a/router.go +++ b/router.go @@ -13,6 +13,7 @@ import ( "github.com/issue9/mux/v7" "github.com/issue9/mux/v7/group" "github.com/issue9/mux/v7/types" + "github.com/issue9/source" "github.com/issue9/web/internal/errs" "github.com/issue9/web/internal/header" @@ -102,8 +103,8 @@ func Recovery(l *logs.Logger) RouterOption { return mux.Recovery(func(w http.ResponseWriter, msg any) { err, ok := msg.(error) if !ok { - l.Print(msg) - w.WriteHeader(http.StatusInternalServerError) + http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError) + l.String(source.Stack(4, err)) return } @@ -112,8 +113,8 @@ func Recovery(l *logs.Logger) RouterOption { he.Status = http.StatusInternalServerError he.Message = err } - l.Error(he.Message) - w.WriteHeader(he.Status) + http.Error(w, http.StatusText(he.Status), he.Status) + l.String(source.Stack(4, he.Message)) }) } diff --git a/web.go b/web.go index 5f796431..4acf7021 100644 --- a/web.go +++ b/web.go @@ -24,7 +24,7 @@ import ( ) // Version 当前框架的版本 -const Version = "0.87.5" +const Version = "0.87.6" type ( Logger = logs.Logger