Skip to content

Commit

Permalink
Fix errors docs
Browse files Browse the repository at this point in the history
  • Loading branch information
Adam Scarr committed Aug 6, 2018
1 parent 96e6aab commit 8a7ed61
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
1 change: 1 addition & 0 deletions docs/content/getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ Exec "go run ./server/server.go" to start GraphQL server
```

This has created an empty skeleton with all files we need:

- gqlgen.yml - The gqlgen config file, knobs for controlling the generated code.
- generated.go - The graphql execution runtime, the bulk of the generated code
- models_gen.go - Generated models required to build the graph. Often you will override these with models you write yourself. Still very useful for input types.
Expand Down
19 changes: 14 additions & 5 deletions docs/content/reference/errors.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,32 @@ here is safe for users, if certain messages arent safe, customize the error pres
To return multiple errors you can call the `graphql.Error` functions like so:

```go
package foo

import (
"context"

"github.com/vektah/gqlparser/gqlerror"
"github.com/99designs/gqlgen/graphql"
)

func (r Query) DoThings(ctx context.Context) (bool, error) {
// Print a formatted string
graphql.AddErrorf(ctx, "Error %d", 1)

// Pass an existing error out
graphql.AddError(ctx, errors.New("zzzzzt"))
graphql.AddError(ctx, gqlerror.Errorf("zzzzzt"))

// Or fully customize the error
graphql.AddError(ctx, &graphql.Error{
graphql.AddError(ctx, &gqlerror.Error{
Message: "A descriptive error message",
Extensions: map[string]interface{}{
"code": "10-4",
},
})

// And you can still return an error if you need
return nil, errors.New("BOOM! Headshot")
return false, gqlerror.Errorf("BOOM! Headshot")
}
```

Expand Down Expand Up @@ -64,11 +73,11 @@ You change this when creating the handler:
```go
server := handler.GraphQL(MakeExecutableSchema(resolvers),
handler.ErrorPresenter(
func(ctx context.Context, e error) *graphql.Error {
func(ctx context.Context, e error) *gqlerror.Error {
// any special logic you want to do here. This only
// requirement is that it can be json encoded
if myError, ok := e.(MyError) ; ok {
return &graphql.Error{Message: "Eeek!"}
return &gqlerror.Errorf("Eeek!")
}

return graphql.DefaultErrorPresenter(ctx, e)
Expand Down

0 comments on commit 8a7ed61

Please sign in to comment.