30 lines
765 B
Go
30 lines
765 B
Go
package middleware
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
applogger "github.com/gochat/gochat/pkg/logger"
|
|
"github.com/gochat/gochat/pkg/response"
|
|
)
|
|
|
|
// Recovery handles panic recovery and returns a 500 error.
|
|
// Corresponds to Chatwoot's ActionDispatch::Reloader middleware.
|
|
func Recovery() gin.HandlerFunc {
|
|
return func(c *gin.Context) {
|
|
defer func() {
|
|
if err := recover(); err != nil {
|
|
applogger.L().Errorf("Panic recovered: %v\nPath: %s %s", err, c.Request.Method, c.Request.URL.Path)
|
|
c.AbortWithStatusJSON(http.StatusInternalServerError, response.APIResponse{
|
|
Success: false,
|
|
Error: &response.ErrorBody{
|
|
Code: response.ErrInternal,
|
|
Message: "Internal server error",
|
|
},
|
|
})
|
|
}
|
|
}()
|
|
c.Next()
|
|
}
|
|
}
|