Unify error handling and HTTP statuses
Signed-off-by: Sean Du <do4suki@gmail.com>
This commit is contained in:
@@ -3,6 +3,7 @@ package middleware
|
||||
import (
|
||||
"fmt"
|
||||
"honoka-chan/internal/session"
|
||||
honokautils "honoka-chan/internal/utils"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strconv"
|
||||
@@ -31,7 +32,7 @@ func Common(ctx *gin.Context) {
|
||||
|
||||
if ctx.Request.URL.Path != "/login/authkey" && ctx.Request.URL.Path != "/login/login" {
|
||||
if ss.UserPref.ForceRelogin {
|
||||
ctx.AbortWithStatus(http.StatusNotFound)
|
||||
honokautils.AbortMaintenanceJSON(ctx, http.StatusNotFound, honokautils.NewNotFoundContent(ctx.Request.URL.String()))
|
||||
return
|
||||
}
|
||||
}
|
||||
@@ -39,7 +40,7 @@ func Common(ctx *gin.Context) {
|
||||
authorize := ctx.GetHeader("Authorize")
|
||||
params, err := url.ParseQuery(authorize)
|
||||
if err != nil {
|
||||
ctx.AbortWithStatus(http.StatusNotFound)
|
||||
honokautils.AbortMaintenanceJSON(ctx, http.StatusNotFound, honokautils.NewNotFoundContent(ctx.Request.URL.String()))
|
||||
return
|
||||
}
|
||||
|
||||
@@ -58,7 +59,7 @@ func Common(ctx *gin.Context) {
|
||||
ctx.Header("server_version", "20120129")
|
||||
ctx.Header("Server-Version", "97.4.6")
|
||||
ctx.Header("version_up", "0")
|
||||
ctx.Header("status_code", "200")
|
||||
ctx.Header("status_code", strconv.Itoa(http.StatusOK))
|
||||
|
||||
ctx.Next()
|
||||
}
|
||||
|
||||
@@ -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()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user