~emersion/gqlclient

4e6e9c763dd2a28f999664be6d477bd39a1085b2 — Simon Ser 5 months ago 1ccb9b1 master
Check response Content-Type

Instead of errors such as:

    invalid character '<' looking for beginning of value

Try to give slightly more useful errors with the HTTP error,
if any.
1 files changed, 15 insertions(+), 0 deletions(-)

M client.go
M client.go => client.go +15 -0
@@ 6,6 6,7 @@ import (
	"encoding/json"
	"fmt"
	"io"
	"mime"
	"net/http"
)



@@ 121,6 122,20 @@ func (c *Client) Execute(ctx context.Context, op *Operation, data interface{}) e
	}
	defer resp.Body.Close()

	contentType = resp.Header.Get("Content-Type")
	if contentType == "" {
		contentType = "text/plain"
	}
	mediaType, _, err := mime.ParseMediaType(contentType)
	if err != nil {
		return fmt.Errorf("invalid Content-Type %q: %v", contentType, err)
	} else if mediaType != "application/json" {
		if resp.StatusCode/100 != 2 {
			return fmt.Errorf("HTTP server error: %v %v", resp.StatusCode, resp.Status)
		}
		return fmt.Errorf("invalid Content-Type %q: expected application/json", contentType)
	}

	respData := struct {
		Data   interface{}
		Errors []Error