github.com/vektah/gqlgen@v0.7.2/docs/content/reference/errors.md (about)

     1  ---
     2  linkTitle: Handling Errors
     3  title: Sending custom error data in the graphql response
     4  description: Customising graphql error types to send custom error data back to the client using gqlgen.
     5  menu: { main: { parent: 'reference' } }
     6  ---
     7  
     8  ## Returning errors
     9  
    10  All resolvers simply return an error to be sent to the user. It's assumed that any error message returned
    11  here is safe for users. If certain messages aren't safe, customise the error presenter.
    12  
    13  ### Multiple errors
    14  
    15  To return multiple errors you can call the `graphql.Error` functions like so:
    16  
    17  ```go
    18  package foo
    19  
    20  import (
    21  	"context"
    22  	
    23  	"github.com/vektah/gqlparser/gqlerror"
    24  	"github.com/99designs/gqlgen/graphql"
    25  )
    26  
    27  func (r Query) DoThings(ctx context.Context) (bool, error) {
    28  	// Print a formatted string
    29  	graphql.AddErrorf(ctx, "Error %d", 1)
    30  
    31  	// Pass an existing error out
    32  	graphql.AddError(ctx, gqlerror.Errorf("zzzzzt"))
    33  
    34  	// Or fully customize the error
    35  	graphql.AddError(ctx, &gqlerror.Error{
    36  		Message: "A descriptive error message",
    37  		Extensions: map[string]interface{}{
    38  			"code": "10-4",
    39  		},
    40  	})
    41  
    42  	// And you can still return an error if you need
    43  	return false, gqlerror.Errorf("BOOM! Headshot")
    44  }
    45  ```
    46  
    47  They will be returned in the same order in the response, eg:
    48  ```json
    49  {
    50    "data": {
    51      "todo": null
    52    },
    53    "errors": [
    54      { "message": "Error 1", "path": [ "todo" ] },
    55      { "message": "zzzzzt", "path": [ "todo" ] },
    56      { "message": "A descriptive error message", "path": [ "todo" ], "extensions": { "code": "10-4" } },
    57      { "message": "BOOM! Headshot", "path": [ "todo" ] }
    58    ]
    59  }
    60  ```
    61  
    62  ## Hooks
    63  
    64  ### The error presenter
    65  
    66  All `errors` returned by resolvers, or from validation, pass through a hook before being displayed to the user.
    67  This hook gives you the ability to customise errors however makes sense in your app.
    68  
    69  The default error presenter will capture the resolver path and use the Error() message in the response. It will
    70  also call an Extensions() method if one is present to return graphql extensions.
    71  
    72  You change this when creating the handler:
    73  ```go
    74  server := handler.GraphQL(MakeExecutableSchema(resolvers),
    75  	handler.ErrorPresenter(
    76  		func(ctx context.Context, e error) *gqlerror.Error {
    77  			// any special logic you want to do here. Must specify path for correct null bubbling behaviour.
    78  			if myError, ok := e.(MyError) ; ok {
    79  				return gqlerror.ErrorPathf(graphql.GetResolverContext(ctx).Path(), "Eeek!")
    80  			}
    81  
    82  			return graphql.DefaultErrorPresenter(ctx, e)
    83  		}
    84  	),
    85  )
    86  ```
    87  
    88  This function will be called with the the same resolver context that generated it, so you can extract the
    89  current resolver path and whatever other state you might want to notify the client about.
    90  
    91  
    92  ### The panic handler
    93  
    94  There is also a panic handler, called whenever a panic happens to gracefully return a message to the user before
    95  stopping parsing. This is a good spot to notify your bug tracker and send a custom message to the user. Any errors
    96  returned from here will also go through the error presenter.
    97  
    98  You change this when creating the handler:
    99  ```go
   100  server := handler.GraphQL(MakeExecutableSchema(resolvers),
   101  	handler.RecoverFunc(func(ctx context.Context, err interface{}) error {
   102  		// notify bug tracker...
   103  
   104  		return fmt.Errorf("Internal server error!")
   105  	}
   106  }
   107  ```
   108