Improve session abort error context

Signed-off-by: Sean Du <do4suki@gmail.com>
This commit is contained in:
2026-06-28 18:13:59 +08:00
parent 36ccf46df5
commit 61b33ae0e3
+23 -1
View File
@@ -3,11 +3,14 @@ package session
import ( import (
"encoding/base64" "encoding/base64"
"encoding/json" "encoding/json"
"fmt"
usermodel "honoka-chan/internal/model/user" usermodel "honoka-chan/internal/model/user"
"honoka-chan/pkg/db" "honoka-chan/pkg/db"
"honoka-chan/pkg/encrypt" "honoka-chan/pkg/encrypt"
"log" "log"
"net/http" "net/http"
"runtime"
"strings"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"xorm.io/xorm" "xorm.io/xorm"
@@ -116,7 +119,26 @@ func (ss *Session) Abort(err error) {
ss.UserEng = nil ss.UserEng = nil
} }
ss.resp.writeJSON(500, gin.H{"error": err.Error()}) skip := 1
if pc, _, _, ok := runtime.Caller(1); ok {
fn := runtime.FuncForPC(pc)
if fn != nil && strings.HasSuffix(fn.Name(), ".(*Session).CheckErr") {
skip = 2
}
}
loc := "unknown"
if pc, file, line, ok := runtime.Caller(skip); ok {
fn := runtime.FuncForPC(pc)
funcName := ""
if fn != nil {
funcName = fn.Name()
}
loc = fmt.Sprintf("%s:%d (%s)", file, line, funcName)
}
msg := fmt.Sprintf("[%s] %s", loc, err.Error())
log.Println(msg)
ss.resp.writeJSON(500, gin.H{"error": msg})
ss.resp.abort() ss.resp.abort()
} }