Skip to content

Commit

Permalink
PR: Errors - Move errors to errors.go file.
Browse files Browse the repository at this point in the history
  • Loading branch information
shahzadlone committed Jan 21, 2023
1 parent 7eca6c0 commit 83387a6
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 13 deletions.
9 changes: 8 additions & 1 deletion planner/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ package planner
import "github.com/sourcenetwork/defradb/errors"

const (
errUnknownDependency string = "The given field does not exist"
errUnknownDependency string = "given field does not exist"
errFailedToClosePlan string = "failed to close the plan"
)

var (
Expand All @@ -22,13 +23,19 @@ var (
ErrMissingQueryOrMutation = errors.New("query is missing query or mutation statements")
ErrOperationDefinitionMissingSelection = errors.New("operationDefinition is missing selections")
ErrFailedToFindGroupSource = errors.New("failed to identify group source")
ErrCantExplainSubscriptionRequest = errors.New("can not explain a subscription request")
ErrGroupOutsideOfGroupBy = errors.New("_group may only be referenced when within a groupBy query")
ErrMissingChildSelect = errors.New("expected child select but none was found")
ErrMissingChildValue = errors.New("expected child value, however none was yielded")
ErrUnknownRelationType = errors.New("failed sub selection, unknown relation type")
ErrUnknownExplainRequestType = errors.New("can not explain request of unknown type")
ErrUnknownDependency = errors.New(errUnknownDependency)
)

func NewErrUnknownDependency(name string) error {
return errors.New(errUnknownDependency, errors.NewKV("Name", name))
}

func NewErrFailedToClosePlan(inner error, location string) error {
return errors.Wrap(errFailedToClosePlan, inner, errors.NewKV("Location", location))
}
9 changes: 4 additions & 5 deletions planner/planner.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import (
"github.com/sourcenetwork/defradb/client/request"
"github.com/sourcenetwork/defradb/core"
"github.com/sourcenetwork/defradb/datastore"
"github.com/sourcenetwork/defradb/errors"
"github.com/sourcenetwork/defradb/logging"
"github.com/sourcenetwork/defradb/planner/mapper"
)
Expand Down Expand Up @@ -462,7 +461,7 @@ func (p *Planner) explainRequest(
return explainResult, nil

default:
return nil, errors.New("can't explain request of unknown type")
return nil, ErrUnknownExplainRequestType
}
}

Expand Down Expand Up @@ -508,13 +507,13 @@ func (p *Planner) RunRequest(

defer func() {
if e := plan.Close(); e != nil {
err = errors.Wrap("failed to close the plan after running request", e)
err = NewErrFailedToClosePlan(e, "running request")
}
}()

// Ensure subscription request doesn't ever end up with an explain directive.
if len(req.Subscription) > 0 && req.Subscription[0].Directives.ExplainType.HasValue() {
return nil, errors.New("subscription request can not be explained")
return nil, ErrCantExplainSubscriptionRequest
}

if len(req.Queries) > 0 && req.Queries[0].Directives.ExplainType.HasValue() {
Expand All @@ -541,7 +540,7 @@ func (p *Planner) RunSubscriptionRequest(

defer func() {
if e := plan.Close(); e != nil {
err = errors.Wrap("failed to close the plan after running subscription request", e)
err = NewErrFailedToClosePlan(e, "running subscription request")
}
}()

Expand Down
1 change: 0 additions & 1 deletion planner/type_join.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ func (p *Planner) makeTypeIndexJoin(
desc := parent.sourceInfo.collectionDescription
typeFieldDesc, ok := desc.GetField(subType.Name)
if !ok {
// return nil, errors.Wrap("Unknown field on sub selection")
return nil, client.NewErrFieldNotExist(subType.Name)
}

Expand Down
3 changes: 3 additions & 0 deletions query/graphql/parser/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,8 @@ var (
ErrFailedToParseConditionValue = errors.New("failed to parse condition value from query filter statement")
ErrEmptyDataPayload = errors.New("given data payload is empty")
ErrUnknownMutationName = errors.New("unknown mutation name")
ErrInvalidExplainTypeArg = errors.New("invalid explain request type argument")
ErrInvalidNumberOfExplainArgs = errors.New("invalid number of arguments to an explain request")
ErrUnknownExplainType = errors.New("invalid / unknown explain type")
ErrUnknownGQLOperation = errors.New("unknown GraphQL operation type")
)
9 changes: 3 additions & 6 deletions query/graphql/parser/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,22 +134,19 @@ func parseExplainDirective(astDirective *ast.Directive) (immutable.Option[reques
}

if len(astDirective.Arguments) != 1 {
return immutable.None[request.ExplainType](),
errors.New("invalid number of arguments to an explain request")
return immutable.None[request.ExplainType](), ErrInvalidNumberOfExplainArgs
}

arg := astDirective.Arguments[0]
if arg.Name.Value != schemaTypes.ExplainArgNameType {
return immutable.None[request.ExplainType](),
errors.New("invalid explain request argument")
return immutable.None[request.ExplainType](), ErrInvalidExplainTypeArg
}

switch arg.Value.GetValue() {
case schemaTypes.ExplainArgSimple:
return immutable.Some(request.SimpleExplain), nil
default:
return immutable.None[request.ExplainType](),
errors.New("invalid / unknown explain type")
return immutable.None[request.ExplainType](), ErrUnknownExplainType
}
}

Expand Down

0 comments on commit 83387a6

Please sign in to comment.