Unify error handling and HTTP statuses

Signed-off-by: Sean Du <do4suki@gmail.com>
This commit is contained in:
2026-07-11 16:18:17 +08:00
parent 61b33ae0e3
commit cb8f587e30
113 changed files with 408 additions and 166 deletions
+40
View File
@@ -0,0 +1,40 @@
package middleware
import (
"honoka-chan/internal/session"
honokautils "honoka-chan/internal/utils"
"log"
"net/http"
"runtime/debug"
"github.com/gin-gonic/gin"
)
func Recovery() gin.HandlerFunc {
return func(ctx *gin.Context) {
defer func() {
recovered := recover()
if recovered == nil {
return
}
stack := debug.Stack()
log.Printf("panic serving %s %s: %v\n%s", ctx.Request.Method, ctx.Request.URL.String(), recovered, stack)
if !honokautils.IsMainPHPRequest(ctx.Request.URL.Path) {
ctx.AbortWithStatus(http.StatusInternalServerError)
return
}
content := honokautils.NewPanicContent(recovered, stack)
if value, ok := ctx.Get("session"); ok {
value.(*session.Session).AbortWithStatus(http.StatusInternalServerError, content)
return
}
honokautils.AbortMaintenanceJSON(ctx, http.StatusInternalServerError, content)
}()
ctx.Next()
}
}