Fix duplicate responses
This commit is contained in:
parent
941c033323
commit
b989f1b82d
@ -1,6 +1,7 @@
|
|||||||
package middleware
|
package middleware
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"errors"
|
"errors"
|
||||||
"github.com/TicketsBot/GoPanel/app"
|
"github.com/TicketsBot/GoPanel/app"
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
@ -10,7 +11,19 @@ type ErrorResponse struct {
|
|||||||
Error string `json:"error"`
|
Error string `json:"error"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type copyWriter struct {
|
||||||
|
gin.ResponseWriter
|
||||||
|
buf *bytes.Buffer
|
||||||
|
}
|
||||||
|
|
||||||
|
func (cw copyWriter) Write(b []byte) (int, error) {
|
||||||
|
return cw.buf.Write(b)
|
||||||
|
}
|
||||||
|
|
||||||
func ErrorHandler(c *gin.Context) {
|
func ErrorHandler(c *gin.Context) {
|
||||||
|
cw := ©Writer{buf: &bytes.Buffer{}, ResponseWriter: c.Writer}
|
||||||
|
c.Writer = cw
|
||||||
|
|
||||||
c.Next()
|
c.Next()
|
||||||
|
|
||||||
if len(c.Errors) > 0 {
|
if len(c.Errors) > 0 {
|
||||||
@ -26,11 +39,17 @@ func ErrorHandler(c *gin.Context) {
|
|||||||
c.JSON(-1, ErrorResponse{
|
c.JSON(-1, ErrorResponse{
|
||||||
Error: message,
|
Error: message,
|
||||||
})
|
})
|
||||||
|
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if c.Writer.Status() >= 500 && len(c.Errors) == 0 {
|
if c.Writer.Status() >= 500 {
|
||||||
c.JSON(-1, ErrorResponse{
|
c.JSON(-1, ErrorResponse{
|
||||||
Error: "An internal server error occurred",
|
Error: "An internal server error occurred",
|
||||||
})
|
})
|
||||||
|
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
c.Writer.Write(cw.buf.Bytes())
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user