wings/router/error.go

136 lines
3.7 KiB
Go
Raw Normal View History

2020-04-06 01:00:33 +00:00
package router
import (
"fmt"
2020-06-13 17:26:35 +00:00
"github.com/apex/log"
2020-04-06 01:00:33 +00:00
"github.com/gin-gonic/gin"
"github.com/google/uuid"
"github.com/pkg/errors"
"github.com/pterodactyl/wings/server"
"net/http"
"os"
"strings"
2020-04-06 01:00:33 +00:00
)
type RequestError struct {
Err error
Uuid string
Message string
server *server.Server
}
// Generates a new tracked error, which simply tracks the specific error that
// is being passed in, and also assigned a UUID to the error so that it can be
// cross referenced in the logs.
func TrackedError(err error) *RequestError {
return &RequestError{
Err: err,
Uuid: uuid.Must(uuid.NewRandom()).String(),
Message: "",
}
}
// Same as TrackedError, except this will also attach the server instance that
// generated this server for the purposes of logging.
func TrackedServerError(err error, s *server.Server) *RequestError {
return &RequestError{
2020-09-25 03:48:59 +00:00
Err: err,
2020-04-06 01:00:33 +00:00
Uuid: uuid.Must(uuid.NewRandom()).String(),
Message: "",
server: s,
}
}
2020-06-13 17:26:35 +00:00
func (e *RequestError) logger() *log.Entry {
if e.server != nil {
return e.server.Log().WithField("error_id", e.Uuid)
}
return log.WithField("error_id", e.Uuid)
}
2020-04-06 01:00:33 +00:00
// Sets the output message to display to the user in the error.
func (e *RequestError) SetMessage(msg string) *RequestError {
e.Message = msg
return e
}
// Aborts the request with the given status code, and responds with the error. This
// will also include the error UUID in the output so that the user can report that
// and link the response to a specific error in the logs.
func (e *RequestError) AbortWithStatus(status int, c *gin.Context) {
// If this error is because the resource does not exist, we likely do not need to log
// the error anywhere, just return a 404 and move on with our lives.
if os.IsNotExist(e.Err) {
e.logger().WithField("error", e.Err).Debug("encountered os.IsNotExist error while handling request")
2020-04-06 01:00:33 +00:00
c.AbortWithStatusJSON(http.StatusNotFound, gin.H{
"error": "The requested resource was not found on the system.",
})
return
}
// Otherwise, log the error to zap, and then report the error back to the user.
if status >= 500 {
2020-06-13 17:26:35 +00:00
e.logger().WithField("error", e.Err).Error("encountered HTTP/500 error while handling request")
2020-04-06 01:00:33 +00:00
c.Error(errors.WithStack(e))
2020-04-12 00:48:57 +00:00
} else {
2020-06-13 17:26:35 +00:00
e.logger().WithField("error", e.Err).Debug("encountered non-HTTP/500 error while handling request")
2020-04-06 01:00:33 +00:00
}
msg := "An unexpected error was encountered while processing this request."
if e.Message != "" {
msg = e.Message
}
c.AbortWithStatusJSON(status, gin.H{
"error": msg,
"error_id": e.Uuid,
})
}
// Helper function to just abort with an internal server error. This is generally the response
// from most errors encountered by the API.
func (e *RequestError) AbortWithServerError(c *gin.Context) {
e.AbortWithStatus(http.StatusInternalServerError, c)
}
// Handle specific filesystem errors for a server.
func (e *RequestError) AbortFilesystemError(c *gin.Context) {
if errors.Is(e.Err, os.ErrNotExist) {
c.AbortWithStatusJSON(http.StatusNotFound, gin.H{
"error": "The requested resource was not found.",
})
return
}
if errors.Is(e.Err, server.ErrNotEnoughDiskSpace) {
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{
"error": server.ErrNotEnoughDiskSpace.Error(),
})
return
}
if strings.HasSuffix(e.Err.Error(), "file name too long") {
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{
"error": "File name is too long.",
})
return
}
if e, ok := e.Err.(*os.SyscallError); ok && e.Syscall == "readdirent" {
c.AbortWithStatusJSON(http.StatusNotFound, gin.H{
"error": "The requested directory does not exist.",
})
return
}
e.AbortWithServerError(c)
}
2020-04-06 01:00:33 +00:00
// Format the error to a string and include the UUID.
func (e *RequestError) Error() string {
return fmt.Sprintf("%v (uuid: %s)", e.Err, e.Uuid)
}