Repository: jensneuse/graphql-go-tools
Pull request #72 (2019-04-24 19:38:20)

Handle DispatchRequest error in a non-silent way
In response to #66 


Patch:
diff --git a/README.md b/README.md
index 9031c445..17649827 100644
--- a/README.md
+++ b/README.md
@@ -80,7 +80,7 @@ By fixing this issue validation time for the introspection query dropped from ~
 ## Contributors
 
 - [Jens Neuse][jens-neuse-github] (Project Lead & Active Maintainer)
-- [Mantas Vidutis][mantas-vidutis-github](Contributions to the http proxy & the Context Middleware)
+- [Mantas Vidutis][mantas-vidutis-github] (Contributions to the http proxy & the Context Middleware)
 - [Jonas Bergner][jonas-bergner-github] (Contributions to the initial version of the parser, contributions to the tests)
 
 [jens-neuse-github]: https://github.com/jensneuse
diff --git a/pkg/proxy/http/proxy.go b/pkg/proxy/http/proxy.go
index e3a9d87d..9d542b55 100644
--- a/pkg/proxy/http/proxy.go
+++ b/pkg/proxy/http/proxy.go
@@ -4,13 +4,15 @@ import (
 	"bytes"
 	"context"
 	"encoding/json"
-	"github.com/jensneuse/graphql-go-tools/pkg/middleware"
-	"github.com/jensneuse/graphql-go-tools/pkg/proxy"
+	"fmt"
 	"io"
 	"io/ioutil"
 	"log"
 	"net/http"
 	"sync"
+
+	"github.com/jensneuse/graphql-go-tools/pkg/middleware"
+	"github.com/jensneuse/graphql-go-tools/pkg/proxy"
 )
 
 type Proxy struct {
@@ -88,6 +90,8 @@ func (pr *ProxyRequest) DispatchRequest(buff *bytes.Buffer) (io.ReadCloser, erro
 	response, err := client.Do(&request)
 	if err != nil {
 		return nil, err
+	} else if response.StatusCode >= 400 {
+		return nil, fmt.Errorf("received status code %d, body %s", response.StatusCode, response.Body)
 	}
 	return response.Body, nil
 }
@@ -124,6 +128,7 @@ func (p *Proxy) ServeHTTP(w http.ResponseWriter, r *http.Request) {
 	if err != nil {
 		p.BufferPool.Put(buff)
 		r.Body.Close()
+		p.HandleError(err, w)
 		return
 	}
 


Test patch:
diff --git a/pkg/proxy/http/proxy_test.go b/pkg/proxy/http/proxy_test.go
index e6347005..ceeac6b6 100644
--- a/pkg/proxy/http/proxy_test.go
+++ b/pkg/proxy/http/proxy_test.go
@@ -2,8 +2,6 @@ package http
 
 import (
 	"fmt"
-	hackmiddleware "github.com/jensneuse/graphql-go-tools/hack/middleware"
-	"github.com/jensneuse/graphql-go-tools/pkg/proxy"
 	"io/ioutil"
 	"net/http"
 	"net/http/httptest"
@@ -12,6 +10,9 @@ import (
 	"strings"
 	"testing"
 	"time"
+
+	hackmiddleware "github.com/jensneuse/graphql-go-tools/hack/middleware"
+	"github.com/jensneuse/graphql-go-tools/pkg/proxy"
 )
 
 func TestProxyHandler(t *testing.T) {
@@ -49,6 +50,46 @@ func TestProxyHandler(t *testing.T) {
 	})
 }
 
+func TestProxyHandlerError(t *testing.T) {
+	endpointServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+		w.WriteHeader(http.StatusInternalServerError)
+		if _, err := w.Write([]byte("induced failure")); err != nil {
+			t.Fatal(err)
+		}
+	}))
+	defer endpointServer.Close()
+
+	schema := []byte(assetSchema)
+	backendURL, err := url.Parse(endpointServer.URL)
+	if err != nil {
+		t.Fatal(err)
+	}
+
+	requestConfigProvider := proxy.NewStaticRequestConfigProvider(proxy.RequestConfig{
+		Schema:     &schema,
+		BackendURL: *backendURL,
+	})
+
+	ph := NewDefaultProxy(requestConfigProvider, &hackmiddleware.AssetUrlMiddleware{})
+	handlerHit := false
+	ph.HandleError = func(err error, w http.ResponseWriter) {
+		handlerHit = true
+	}
+	proxyServer := httptest.NewServer(ph)
+	defer proxyServer.Close()
+
+	t.Run("Test proxy handler", func(t *testing.T) {
+		_, err := http.Post(proxyServer.URL, "application/graphql", strings.NewReader(assetInput))
+		if err != nil {
+			t.Error(err)
+		}
+
+		if handlerHit != true {
+			t.Error("Error handler was not hit")
+		}
+	})
+}
+
 func BenchmarkProxyHandler(b *testing.B) {
 
 	//go printMemUsage()