~bigbes/core-go

32f482812bf9c3688bfc1c2df423db8a4777f34c — Conrad Hoffmann 4 months ago bbb9e45
errors: add ERR_REDIRECT

This error is meant to be returned if a resource is requested that does
not exist under the requested name anymore but was moved to a different
resource.
1 files changed, 15 insertions(+), 0 deletions(-)

M errors/errors.go
M errors/errors.go => errors/errors.go +15 -0
@@ 25,6 25,19 @@ func Errorf(code ErrorCode, format string, a ...any) *gqlerror.Error {
	}
}

// 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 @@ var (
	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 @@ var (
	ErrUnauthorized  = New(Unauthorized, "Unauthorized")
	ErrInternalError = New(InternalError, "Internal server error")
	ErrTimeout       = New(Timeout, "Operation timed out")
	ErrRedirect      = New(Redirect, "The requested resource was moved")
)