Skip to content

Commit

Permalink
Drop custom graphql error methods
Browse files Browse the repository at this point in the history
  • Loading branch information
Adam Scarr authored and vektah committed Jul 14, 2018
1 parent d390f9c commit 58e32bb
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 17 deletions.
6 changes: 2 additions & 4 deletions docs/content/reference/errors.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,9 @@ func (r Query) DoThings(ctx context.Context) (bool, error) {
// Pass an existing error out
graphql.AddError(ctx, err)

// Fully customize the error, bypassing the presenter. You
// wont get path information unless you add it yourself
graphql.AddGraphqlError(ctx, &graphql.Error{
// Or fully customize the error
graphql.AddError(ctx, &graphql.Error{
Message: "A descriptive error message",
Path: GetResolverContext(ctx).Path,
Extensions: map[string]interface{}{
"code": "10-4",
}
Expand Down
13 changes: 0 additions & 13 deletions graphql/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,14 +134,6 @@ func (c *RequestContext) Error(ctx context.Context, err error) {
c.Errors = append(c.Errors, c.ErrorPresenter(ctx, err))
}

// GraphqlError bypasses the error presenter, allowing you to directly taylor an error response from a resolver
func (c *RequestContext) GraphqlError(ctx context.Context, err *Error) {
c.errorsMu.Lock()
defer c.errorsMu.Unlock()

c.Errors = append(c.Errors, err)
}

// AddError is a convenience method for adding an error to the current response
func AddError(ctx context.Context, err error) {
GetRequestContext(ctx).Error(ctx, err)
Expand All @@ -151,8 +143,3 @@ func AddError(ctx context.Context, err error) {
func AddErrorf(ctx context.Context, format string, args ...interface{}) {
GetRequestContext(ctx).Errorf(ctx, format, args...)
}

// AddGraphqlError is a convenience method for adding an error to the current response, bypassing the formatter
func AddGraphqlError(ctx context.Context, err *Error) {
GetRequestContext(ctx).GraphqlError(ctx, err)
}
9 changes: 9 additions & 0 deletions graphql/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ type Error struct {
Extensions map[string]interface{} `json:"extensions,omitempty"`
}

func (e *Error) Error() string {
return e.Message
}

type ErrorLocation struct {
Line int `json:"line,omitempty"`
Column int `json:"column,omitempty"`
Expand All @@ -24,6 +28,11 @@ type ExtendedError interface {
}

func DefaultErrorPresenter(ctx context.Context, err error) *Error {
if gqlerr, ok := err.(*Error); ok {
gqlerr.Path = GetResolverContext(ctx).Path
return gqlerr
}

var extensions map[string]interface{}
if ee, ok := err.(ExtendedError); ok {
extensions = ee.Extensions()
Expand Down

0 comments on commit 58e32bb

Please sign in to comment.