2020-04-06 01:00:33 +00:00
|
|
|
package router
|
|
|
|
|
|
|
|
import (
|
2020-12-16 05:53:34 +00:00
|
|
|
"emperror.dev/errors"
|
2020-04-06 01:00:33 +00:00
|
|
|
"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/pterodactyl/wings/server"
|
2020-09-27 19:24:08 +00:00
|
|
|
"github.com/pterodactyl/wings/server/filesystem"
|
2020-04-06 01:00:33 +00:00
|
|
|
"net/http"
|
|
|
|
"os"
|
2020-09-25 04:18:10 +00:00
|
|
|
"strings"
|
2020-04-06 01:00:33 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type RequestError struct {
|
2020-12-16 05:08:00 +00:00
|
|
|
err error
|
|
|
|
uuid string
|
|
|
|
message string
|
2020-04-06 01:00:33 +00:00
|
|
|
server *server.Server
|
|
|
|
}
|
|
|
|
|
2020-12-16 05:53:34 +00:00
|
|
|
// Attaches an error to the gin.Context object for the request and ensures that it
|
|
|
|
// has a proper stacktrace associated with it when doing so.
|
|
|
|
//
|
|
|
|
// If you just call c.Error(err) without using this function you'll likely end up
|
|
|
|
// with an error that has no annotated stack on it.
|
|
|
|
func WithError(c *gin.Context, err error) error {
|
|
|
|
return c.Error(errors.WithStackDepthIf(err, 1))
|
|
|
|
}
|
|
|
|
|
2020-04-06 01:00:33 +00:00
|
|
|
// 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.
|
2020-12-16 05:08:00 +00:00
|
|
|
func NewTrackedError(err error) *RequestError {
|
2020-04-06 01:00:33 +00:00
|
|
|
return &RequestError{
|
2020-12-16 05:08:00 +00:00
|
|
|
err: err,
|
|
|
|
uuid: uuid.Must(uuid.NewRandom()).String(),
|
2020-04-06 01:00:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-16 05:08:00 +00:00
|
|
|
// Same as NewTrackedError, except this will also attach the server instance that
|
2020-04-06 01:00:33 +00:00
|
|
|
// generated this server for the purposes of logging.
|
2020-12-16 05:08:00 +00:00
|
|
|
func NewServerError(err error, s *server.Server) *RequestError {
|
2020-04-06 01:00:33 +00:00
|
|
|
return &RequestError{
|
2020-12-16 05:08:00 +00:00
|
|
|
err: err,
|
|
|
|
uuid: uuid.Must(uuid.NewRandom()).String(),
|
|
|
|
server: s,
|
2020-04-06 01:00:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-13 17:26:35 +00:00
|
|
|
func (e *RequestError) logger() *log.Entry {
|
|
|
|
if e.server != nil {
|
2020-12-16 05:53:34 +00:00
|
|
|
return e.server.Log().WithField("error_id", e.uuid).WithField("error", e.err)
|
2020-06-13 17:26:35 +00:00
|
|
|
}
|
2020-12-16 05:53:34 +00:00
|
|
|
return log.WithField("error_id", e.uuid).WithField("error", e.err)
|
2020-06-13 17:26:35 +00:00
|
|
|
}
|
|
|
|
|
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 {
|
2020-12-16 05:08:00 +00:00
|
|
|
e.message = msg
|
2020-04-06 01:00:33 +00:00
|
|
|
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) {
|
2020-12-16 05:08:00 +00:00
|
|
|
// In instances where the status has already been set just use that existing status
|
|
|
|
// since we cannot change it at this point, and trying to do so will emit a gin warning
|
|
|
|
// into the program output.
|
|
|
|
if c.Writer.Status() != 200 {
|
|
|
|
status = c.Writer.Status()
|
|
|
|
}
|
|
|
|
|
2020-04-06 01:00:33 +00:00
|
|
|
// 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.
|
2020-12-16 05:08:00 +00:00
|
|
|
if errors.Is(e.err, os.ErrNotExist) {
|
2020-12-16 05:53:34 +00:00
|
|
|
e.logger().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
|
|
|
|
}
|
|
|
|
|
2020-12-16 05:08:00 +00:00
|
|
|
// If this is a Filesystem error just return it without all of the tracking code nonsense
|
|
|
|
// since we don't need to be logging it into the logs or anything, its just a normal error
|
|
|
|
// that the user can solve on their end.
|
|
|
|
if st, msg := e.getAsFilesystemError(); st != 0 {
|
|
|
|
c.AbortWithStatusJSON(st, gin.H{"error": msg})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-04-06 01:00:33 +00:00
|
|
|
// Otherwise, log the error to zap, and then report the error back to the user.
|
|
|
|
if status >= 500 {
|
2020-12-16 05:53:34 +00:00
|
|
|
e.logger().Error("unexpected error while handling HTTP request")
|
2020-04-12 00:48:57 +00:00
|
|
|
} else {
|
2020-12-16 05:53:34 +00:00
|
|
|
e.logger().Debug("non-server error encountered while handling HTTP request")
|
2020-04-06 01:00:33 +00:00
|
|
|
}
|
|
|
|
|
2020-12-16 05:08:00 +00:00
|
|
|
if e.message == "" {
|
|
|
|
e.message = "An unexpected error was encountered while processing this request."
|
2020-04-06 01:00:33 +00:00
|
|
|
}
|
|
|
|
|
2020-12-16 05:08:00 +00:00
|
|
|
c.AbortWithStatusJSON(status, gin.H{"error": e.message, "error_id": e.uuid})
|
2020-04-06 01:00:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Helper function to just abort with an internal server error. This is generally the response
|
|
|
|
// from most errors encountered by the API.
|
2020-12-16 04:19:09 +00:00
|
|
|
func (e *RequestError) Abort(c *gin.Context) {
|
2020-04-06 01:00:33 +00:00
|
|
|
e.AbortWithStatus(http.StatusInternalServerError, c)
|
|
|
|
}
|
|
|
|
|
2020-12-16 05:08:00 +00:00
|
|
|
// Looks at the given RequestError and determines if it is a specific filesystem error that
|
|
|
|
// we can process and return differently for the user.
|
|
|
|
func (e *RequestError) getAsFilesystemError() (int, string) {
|
|
|
|
if errors.Is(e.err, os.ErrNotExist) || filesystem.IsErrorCode(e.err, filesystem.ErrCodePathResolution) {
|
|
|
|
return http.StatusNotFound, "The requested resource was not found on the system."
|
2020-09-25 04:18:10 +00:00
|
|
|
}
|
2020-12-16 05:08:00 +00:00
|
|
|
if filesystem.IsErrorCode(e.err, filesystem.ErrCodeDiskSpace) {
|
|
|
|
return http.StatusConflict, "There is not enough disk space available to perform that action."
|
2020-09-25 04:18:10 +00:00
|
|
|
}
|
2020-12-16 05:08:00 +00:00
|
|
|
if strings.HasSuffix(e.err.Error(), "file name too long") {
|
|
|
|
return http.StatusBadRequest, "Cannot perform that action: file name is too long."
|
2020-09-25 04:18:10 +00:00
|
|
|
}
|
2020-12-16 05:08:00 +00:00
|
|
|
if e, ok := e.err.(*os.SyscallError); ok && e.Syscall == "readdirent" {
|
|
|
|
return http.StatusNotFound, "The requested directory does not exist."
|
2020-09-27 19:24:08 +00:00
|
|
|
}
|
2020-12-16 05:08:00 +00:00
|
|
|
return 0, ""
|
|
|
|
}
|
2020-09-27 19:24:08 +00:00
|
|
|
|
2020-12-16 05:08:00 +00:00
|
|
|
// Handle specific filesystem errors for a server.
|
|
|
|
func (e *RequestError) AbortFilesystemError(c *gin.Context) {
|
2020-12-16 04:19:09 +00:00
|
|
|
e.Abort(c)
|
2020-09-25 04:18:10 +00:00
|
|
|
}
|
|
|
|
|
2020-04-06 01:00:33 +00:00
|
|
|
// Format the error to a string and include the UUID.
|
|
|
|
func (e *RequestError) Error() string {
|
2020-12-16 05:08:00 +00:00
|
|
|
return fmt.Sprintf("%v (uuid: %s)", e.err, e.uuid)
|
2020-04-06 01:00:33 +00:00
|
|
|
}
|