diff --git a/errors/errors.go b/errors/errors.go index 0da300acf420d24ff7185ef35eb7ba5ac62d7f3a..c5885d6991fe0334cd0b1e131977c87bb2d65c48 100644 --- a/errors/errors.go +++ b/errors/errors.go @@ -25,6 +25,19 @@ Extensions: map[string]any{"code": code}, } } +// Creates a new "redirect" GraphQL error pointing to the provided path. The +// path should be broken down into path segments. The length of the path is +// dependent on the type of resource being requested. +func RedirectTo(path []string) *gqlerror.Error { + return &gqlerror.Error{ + Message: "The requested resource was moved", + Extensions: map[string]any{ + "code": Redirect, + "redirect": path, + }, + } +} + // Sets the field name that caused the error func Field(err *gqlerror.Error, field string) *gqlerror.Error { err.Extensions["field"] = field @@ -57,6 +70,7 @@ Unsupported ErrorCode = "ERR_UNSUPPORTED" Unauthorized ErrorCode = "ERR_UNAUTHORIZED" InternalError ErrorCode = "ERR_INTERNAL" Timeout ErrorCode = "ERR_TIMEOUT" + Redirect ErrorCode = "ERR_REDIRECT" ) // Error codes as Go errors @@ -67,4 +81,5 @@ ErrUnsupported = New(Unsupported, "Not supported") ErrUnauthorized = New(Unauthorized, "Unauthorized") ErrInternalError = New(InternalError, "Internal server error") ErrTimeout = New(Timeout, "Operation timed out") + ErrRedirect = New(Redirect, "The requested resource was moved") )