Different handling of errors on routes; push towards using middleware

This commit is contained in:
Dane Everitt
2020-12-15 21:53:34 -08:00
parent 057cdbd927
commit c0a641247b
6 changed files with 32 additions and 27 deletions

View File

@@ -1,11 +1,11 @@
package router
import (
"emperror.dev/errors"
"fmt"
"github.com/apex/log"
"github.com/gin-gonic/gin"
"github.com/google/uuid"
"github.com/pkg/errors"
"github.com/pterodactyl/wings/server"
"github.com/pterodactyl/wings/server/filesystem"
"net/http"
@@ -20,6 +20,15 @@ type RequestError struct {
server *server.Server
}
// 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))
}
// 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.
@@ -42,9 +51,9 @@ func NewServerError(err error, s *server.Server) *RequestError {
func (e *RequestError) logger() *log.Entry {
if e.server != nil {
return e.server.Log().WithField("error_id", e.uuid)
return e.server.Log().WithField("error_id", e.uuid).WithField("error", e.err)
}
return log.WithField("error_id", e.uuid)
return log.WithField("error_id", e.uuid).WithField("error", e.err)
}
// Sets the output message to display to the user in the error.
@@ -67,7 +76,7 @@ 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 errors.Is(e.err, os.ErrNotExist) {
e.logger().WithField("error", e.err).Debug("encountered os.IsNotExist error while handling request")
e.logger().Debug("encountered os.IsNotExist error while handling request")
c.AbortWithStatusJSON(http.StatusNotFound, gin.H{
"error": "The requested resource was not found on the system.",
})
@@ -84,9 +93,9 @@ func (e *RequestError) AbortWithStatus(status int, c *gin.Context) {
// Otherwise, log the error to zap, and then report the error back to the user.
if status >= 500 {
e.logger().WithField("error", e.err).Error("unexpected error while handling HTTP request")
e.logger().Error("unexpected error while handling HTTP request")
} else {
e.logger().WithField("error", e.err).Debug("non-server error encountered while handling HTTP request")
e.logger().Debug("non-server error encountered while handling HTTP request")
}
if e.message == "" {

View File

@@ -1,9 +1,9 @@
package router
import (
"emperror.dev/errors"
"github.com/gin-gonic/gin"
"github.com/google/uuid"
"github.com/pkg/errors"
"github.com/pterodactyl/wings/config"
"github.com/pterodactyl/wings/server"
"io"

View File

@@ -57,14 +57,11 @@ func getServerFileContents(c *gin.Context) {
// Returns the contents of a directory for a server.
func getServerListDirectory(c *gin.Context) {
s := ExtractServer(c)
stats, err := s.Filesystem().ListDirectory(c.Query("directory"))
if err != nil {
NewServerError(err, s).AbortFilesystemError(c)
return
if stats, err := s.Filesystem().ListDirectory(c.Query("directory")); err != nil {
WithError(c, err)
} else {
c.JSON(http.StatusOK, stats)
}
c.JSON(http.StatusOK, stats)
}
type renameFile struct {