Skip to content

Commit

Permalink
feat: support-better-ctx-fail-show-code-info
Browse files Browse the repository at this point in the history
  • Loading branch information
whatwewant committed Aug 21, 2023
1 parent 3dbe9b2 commit 01dfc1b
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,14 @@ import (
"net"
"net/http"
"os"
"runtime"
"strings"
"sync"

"time"

"github.com/go-zoox/cache"
"github.com/go-zoox/fs"
"github.com/go-zoox/i18n"
"github.com/go-zoox/proxy"
"github.com/go-zoox/pubsub"
Expand Down Expand Up @@ -435,8 +438,19 @@ func (ctx *Context) Fail(err error, code int, message string, status ...int) {
statusX = status[0]
}

// ctx.Logger.Error("[Fail] %s", err)
fmt.Println("[Fail]", err)
funcName := "unknown"
// get panic error occurred file and line
pc, filepath, line, ok := runtime.Caller(2)
if ok {
filepath = filepath[len(fs.CurrentDir())+1:]
funcName = runtime.FuncForPC(pc).Name()
funcNameParts := strings.Split(runtime.FuncForPC(pc).Name(), ".")
if len(funcNameParts) > 0 {
funcName = funcNameParts[len(funcNameParts)-1]
}
}

ctx.Logger.Errorf("[fail][%s:%d,%s][%s %s] %s", filepath, line, funcName, ctx.Method, ctx.Path, err)

ctx.JSON(statusX, map[string]any{
"code": code,
Expand Down

0 comments on commit 01dfc1b

Please sign in to comment.