|
package graphqlapp |
|
|
|
import ( |
|
"context" |
|
"errors" |
|
"fmt" |
|
"strconv" |
|
"strings" |
|
|
|
"github.com/99designs/gqlgen/graphql" |
|
"github.com/target/goalert/gadb" |
|
"github.com/target/goalert/graphql2" |
|
"github.com/target/goalert/notification/nfydest" |
|
"github.com/target/goalert/permission" |
|
"github.com/target/goalert/validation" |
|
"github.com/vektah/gqlparser/v2/ast" |
|
"github.com/vektah/gqlparser/v2/gqlerror" |
|
) |
|
|
|
|
|
var errAlreadySet = errors.New("error already set") |
|
|
|
|
|
|
|
|
|
|
|
type errSkipHandler struct{} |
|
|
|
var ( |
|
_ graphql.ResponseInterceptor = errSkipHandler{} |
|
_ graphql.HandlerExtension = errSkipHandler{} |
|
) |
|
|
|
func (errSkipHandler) ExtensionName() string { return "ErrorSkipHandler" } |
|
|
|
func (errSkipHandler) Validate(schema graphql.ExecutableSchema) error { return nil } |
|
|
|
func (errSkipHandler) InterceptResponse(ctx context.Context, next graphql.ResponseHandler) *graphql.Response { |
|
resp := next(ctx) |
|
|
|
filteredErrors := resp.Errors[:0] |
|
for _, err := range resp.Errors { |
|
if err.Extensions == nil { |
|
filteredErrors = append(filteredErrors, err) |
|
continue |
|
} |
|
|
|
skip, ok := err.Extensions["skip"].(bool) |
|
if !ok || !skip { |
|
filteredErrors = append(filteredErrors, err) |
|
continue |
|
} |
|
|
|
|
|
} |
|
|
|
resp.Errors = filteredErrors |
|
|
|
return resp |
|
} |
|
|
|
|
|
func errReason(err error) string { |
|
var fErr validation.FieldError |
|
if errors.As(err, &fErr) { |
|
return fErr.Reason() |
|
} |
|
|
|
return err.Error() |
|
} |
|
|
|
|
|
func appendPath(ctx context.Context, field string) ast.Path { |
|
p := graphql.GetPath(ctx) |
|
parentParts := strings.Split(field, ".") |
|
for _, part := range parentParts { |
|
if part == "" { |
|
continue |
|
} |
|
index, err := strconv.Atoi(part) |
|
if err == nil { |
|
p = append(p, ast.PathIndex(index)) |
|
continue |
|
} |
|
|
|
p = append(p, ast.PathName(part)) |
|
} |
|
|
|
return p |
|
} |
|
|
|
|
|
|
|
func addDestFieldError(ctx context.Context, parentField, fieldID string, err error) error { |
|
if err == nil { |
|
return nil |
|
} |
|
if permission.IsPermissionError(err) { |
|
|
|
return err |
|
} |
|
if !validation.IsClientError(err) { |
|
|
|
return err |
|
} |
|
|
|
graphql.AddError(ctx, &gqlerror.Error{ |
|
Message: errReason(err), |
|
Path: appendPath(ctx, parentField), |
|
Extensions: map[string]interface{}{ |
|
"code": graphql2.ErrorCodeInvalidDestFieldValue, |
|
"fieldID": fieldID, |
|
}, |
|
}) |
|
|
|
return errAlreadySet |
|
} |
|
|
|
func addInputError(ctx context.Context, err error) { |
|
field := err.(validation.FieldError).Field() |
|
|
|
graphql.AddError(ctx, &gqlerror.Error{ |
|
Message: errReason(err), |
|
Path: appendPath(ctx, field), |
|
Extensions: map[string]interface{}{ |
|
"code": graphql2.ErrorCodeInvalidInputValue, |
|
}, |
|
}) |
|
} |
|
|
|
|
|
|
|
|
|
func (a *App) ValidateDestination(ctx context.Context, fieldName string, dest *gadb.DestV1) (err error) { |
|
err = a.DestReg.ValidateDest(ctx, *dest) |
|
if errors.Is(err, nfydest.ErrUnknownType) { |
|
message := fmt.Sprintf("unsupported destination type: %s", dest.Type) |
|
if dest.Type == "" { |
|
message = "destination type is required" |
|
} |
|
|
|
|
|
graphql.AddError(ctx, &gqlerror.Error{ |
|
Message: message, |
|
Path: appendPath(ctx, fieldName+".type"), |
|
Extensions: map[string]interface{}{ |
|
"code": graphql2.ErrorCodeInvalidInputValue, |
|
}, |
|
}) |
|
|
|
return errAlreadySet |
|
} |
|
|
|
var argErr *nfydest.DestArgError |
|
if errors.As(err, &argErr) { |
|
return addDestFieldError(ctx, fieldName+".args", argErr.FieldID, argErr.Err) |
|
} |
|
|
|
if err != nil { |
|
|
|
return err |
|
} |
|
|
|
return nil |
|
} |
|
|