From abf9ced9b1df3fae67521f03ea63dec42399ccf2 Mon Sep 17 00:00:00 2001 From: blaedj Date: Mon, 10 Dec 2018 16:04:22 -0600 Subject: [PATCH] Add support for error extensions 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" //} } } ``` --- graphql.go | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/graphql.go b/graphql.go index 05c29b7..b4c647c 100644 --- a/graphql.go +++ b/graphql.go @@ -250,13 +250,18 @@ func ImmediatelyCloseReqBody() ClientOption { type ClientOption func(*Client) type graphErr struct { - Message string + Message string + ErrorExtensions map[string]interface{} `json:"extensions"` } func (e graphErr) Error() string { return "graphql: " + e.Message } +func (e graphErr) Extensions() map[string]interface{} { + return e.ErrorExtensions +} + type graphResponse struct { Data interface{} Errors []graphErr