Skip to content

Commit

Permalink
use custom error type less
Browse files Browse the repository at this point in the history
  • Loading branch information
neelance committed Oct 31, 2016
1 parent 0a7a37d commit bdfd5ce
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 24 deletions.
7 changes: 4 additions & 3 deletions internal/common/types.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package common

import (
"github.com/neelance/graphql-go/errors"
"fmt"

"github.com/neelance/graphql-go/internal/lexer"
)

Expand Down Expand Up @@ -47,7 +48,7 @@ func parseNullType(l *lexer.Lexer) Type {

type Resolver func(name string) Type

func ResolveType(t Type, resolver Resolver) (Type, *errors.QueryError) {
func ResolveType(t Type, resolver Resolver) (Type, error) {
switch t := t.(type) {
case *List:
ofType, err := ResolveType(t.OfType, resolver)
Expand All @@ -64,7 +65,7 @@ func ResolveType(t Type, resolver Resolver) (Type, *errors.QueryError) {
case *TypeName:
refT := resolver(t.Name)
if refT == nil {
return nil, errors.Errorf("type %q not found", t.Name)
return nil, fmt.Errorf("type %q not found", t.Name)
}
return refT, nil
default:
Expand Down
16 changes: 8 additions & 8 deletions internal/exec/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -384,12 +384,12 @@ func (r *request) handlePanic() {
func ExecuteRequest(ctx context.Context, e *Exec, document *query.Document, operationName string, variables map[string]interface{}) (interface{}, []*errors.QueryError) {
op, err := getOperation(document, operationName)
if err != nil {
return nil, []*errors.QueryError{err}
return nil, []*errors.QueryError{errors.Errorf("%s", err)}
}

coercedVariables, err := coerceInputObject(&op.Vars, variables)
if err != nil {
return nil, []*errors.QueryError{err}
return nil, []*errors.QueryError{errors.Errorf("%s", err)}
}

r := &request{
Expand Down Expand Up @@ -417,14 +417,14 @@ func ExecuteRequest(ctx context.Context, e *Exec, document *query.Document, oper
return data, r.errs
}

func getOperation(document *query.Document, operationName string) (*query.Operation, *errors.QueryError) {
func getOperation(document *query.Document, operationName string) (*query.Operation, error) {
if len(document.Operations) == 0 {
return nil, errors.Errorf("no operations in query document")
return nil, fmt.Errorf("no operations in query document")
}

if operationName == "" {
if len(document.Operations) > 1 {
return nil, errors.Errorf("more than one operation in query document and no operation name given")
return nil, fmt.Errorf("more than one operation in query document and no operation name given")
}
for _, op := range document.Operations {
return op, nil // return the one and only operation
Expand All @@ -433,12 +433,12 @@ func getOperation(document *query.Document, operationName string) (*query.Operat

op, ok := document.Operations[operationName]
if !ok {
return nil, errors.Errorf("no operation with name %q", operationName)
return nil, fmt.Errorf("no operation with name %q", operationName)
}
return op, nil
}

func coerceInputObject(io *common.InputMap, variables map[string]interface{}) (map[string]interface{}, *errors.QueryError) {
func coerceInputObject(io *common.InputMap, variables map[string]interface{}) (map[string]interface{}, error) {
coerced := make(map[string]interface{})
for _, iv := range io.Fields {
value, ok := variables[iv.Name]
Expand All @@ -458,7 +458,7 @@ func coerceInputObject(io *common.InputMap, variables map[string]interface{}) (m
return coerced, nil
}

func coerceInputValue(iv *common.InputValue, value interface{}) (interface{}, *errors.QueryError) {
func coerceInputValue(iv *common.InputValue, value interface{}) (interface{}, error) {
t, _ := unwrapNonNull(iv.Type)
switch t := t.(type) {
case *scalar:
Expand Down
16 changes: 8 additions & 8 deletions internal/exec/scalar.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
package exec

import (
"fmt"
"math"
"reflect"

"github.com/neelance/graphql-go/errors"
"github.com/neelance/graphql-go/internal/schema"
)

type scalar struct {
name string
reflectType reflect.Type
coerceInput func(input interface{}) (interface{}, *errors.QueryError)
coerceInput func(input interface{}) (interface{}, error)
}

func (*scalar) Kind() string { return "SCALAR" }
Expand All @@ -21,39 +21,39 @@ var builtinScalars = []*scalar{
&scalar{
name: "Int",
reflectType: reflect.TypeOf(int32(0)),
coerceInput: func(input interface{}) (interface{}, *errors.QueryError) {
coerceInput: func(input interface{}) (interface{}, error) {
i := input.(int)
if i < math.MinInt32 || i > math.MaxInt32 {
return nil, errors.Errorf("not a 32-bit integer: %d", i)
return nil, fmt.Errorf("not a 32-bit integer: %d", i)
}
return int32(i), nil
},
},
&scalar{
name: "Float",
reflectType: reflect.TypeOf(float64(0)),
coerceInput: func(input interface{}) (interface{}, *errors.QueryError) {
coerceInput: func(input interface{}) (interface{}, error) {
return input, nil // TODO
},
},
&scalar{
name: "String",
reflectType: reflect.TypeOf(""),
coerceInput: func(input interface{}) (interface{}, *errors.QueryError) {
coerceInput: func(input interface{}) (interface{}, error) {
return input, nil // TODO
},
},
&scalar{
name: "Boolean",
reflectType: reflect.TypeOf(true),
coerceInput: func(input interface{}) (interface{}, *errors.QueryError) {
coerceInput: func(input interface{}) (interface{}, error) {
return input, nil // TODO
},
},
&scalar{
name: "ID",
reflectType: reflect.TypeOf(""),
coerceInput: func(input interface{}) (interface{}, *errors.QueryError) {
coerceInput: func(input interface{}) (interface{}, error) {
return input, nil // TODO
},
},
Expand Down
2 changes: 1 addition & 1 deletion internal/query/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func Parse(queryString string, resolver common.Resolver) (doc *Document, err *er
for _, v := range op.Vars.Fields {
t, err := common.ResolveType(v.Type, resolver)
if err != nil {
return nil, err
return nil, errors.Errorf("%s", err)
}
v.Type = t
}
Expand Down
8 changes: 4 additions & 4 deletions internal/schema/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func New() *Schema {
}
}

func (s *Schema) Parse(schemaString string) *errors.QueryError {
func (s *Schema) Parse(schemaString string) error {
sc := &scanner.Scanner{
Mode: scanner.ScanIdents | scanner.ScanInts | scanner.ScanFloats | scanner.ScanStrings,
}
Expand Down Expand Up @@ -151,7 +151,7 @@ func (s *Schema) Parse(schemaString string) *errors.QueryError {
return nil
}

func resolveNamedType(s *Schema, t NamedType) *errors.QueryError {
func resolveNamedType(s *Schema, t NamedType) error {
switch t := t.(type) {
case *Object:
for _, f := range t.Fields {
Expand All @@ -173,7 +173,7 @@ func resolveNamedType(s *Schema, t NamedType) *errors.QueryError {
return nil
}

func resolveField(s *Schema, f *Field) *errors.QueryError {
func resolveField(s *Schema, f *Field) error {
t, err := common.ResolveType(f.Type, s.Resolve)
if err != nil {
return err
Expand All @@ -182,7 +182,7 @@ func resolveField(s *Schema, f *Field) *errors.QueryError {
return resolveInputObject(s, &f.Args)
}

func resolveInputObject(s *Schema, io *common.InputMap) *errors.QueryError {
func resolveInputObject(s *Schema, io *common.InputMap) error {
for _, f := range io.Fields {
t, err := common.ResolveType(f.Type, s.Resolve)
if err != nil {
Expand Down

0 comments on commit bdfd5ce

Please sign in to comment.