machinebox / graphql

Simple low-level GraphQL HTTP client for Go
https://blog.machinebox.io/a-graphql-client-library-for-go-5bffd0455878
Apache License 2.0
937 stars 218 forks source link

Add support for error extensions #24

Open blaedj opened 5 years ago

blaedj commented 5 years ago

This commit:

Why?

An example usage:

package foo

import "github.com/machinebox/graphql"

type extendedError interface {
    Extensions() map[string]interface{}
    Error() string
}

func bar() {
   client := graphql.NewClient(...)
   //...
   err := c.client.Run(ctx, req, resp)
   if ok := err.(extendedError); ok {
       extMsg, _ := json.Marshal(ee.Extensions())
       fmt.Println(extMsg)
      // prints out the extensions, e.g.:
      //{
      //  "message": "Name for character with ID 1002 could not be fetched.",
      //  "locations": [ { "line": 6, "column": 7 } ],
      //  "path": [ "hero", "heroFriends", 1, "name" ],
      //  "code": "CAN_NOT_FETCH_BY_ID",
      //  "timestamp": "Fri Feb 9 14:33:09 UTC 2018"
      //}
   }
}