Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for error extensions #24

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

blaedj
Copy link

@blaedj blaedj commented Dec 10, 2018

This commit:

  • adds an ErrorExtensions attr to the graphErr type
  • adds an Extensions method to the graphErr type

Why?

  • This adds support for error extensions as described in a
    recent(ish) update to the graphql spec.
    The interface changes are minimally invasive, and are in fact
    invisible unless the importing code defines an interface that
    includes the Extensions() method.

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"
      //}
   }
}

This commit:
 - adds an ErrorExtensions attr to the graphErr type
 - adds an Extensions method to the graphErr type

Why?
 - This adds support for error extensions as described in a
 recent(ish)  update to the [graphql
 spec](https://facebook.github.io/graphql/June2018/#example-fce18).
 The interface changes are minimally invasive, and are in fact
 invisible unless the importing code defines an interface that
 includes the `Extensions()` method.

An example usage:

```go
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"
    //}
   }
}

```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant