Skip to content

Commit

Permalink
Merge pull request linode#144 from Charliekenney23/fix/handle-non-jso…
Browse files Browse the repository at this point in the history
…n-errors

handle Linode API default 502 response
  • Loading branch information
0xch4z authored Apr 27, 2020
2 parents 47fcb98 + dd66623 commit 401777d
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 11 deletions.
7 changes: 7 additions & 0 deletions errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,13 @@ func coupleAPIErrors(r *resty.Response, err error) (*resty.Response, error) {
expectedContentType := r.Request.Header.Get("Accept")
responseContentType := r.Header().Get("Content-Type")

// If the upstream Linode API server being fronted fails to respond to the request,
// the http server will respond with a default "Bad Gateway" page with Content-Type
// "text/html".
if r.StatusCode() == http.StatusBadGateway && responseContentType == "text/html" {
return nil, Error{Code: http.StatusBadGateway, Message: http.StatusText(http.StatusBadGateway)}
}

if responseContentType != expectedContentType {
msg := fmt.Sprintf(
"Unexpected Content-Type: Expected: %v, Received: %v",
Expand Down
45 changes: 45 additions & 0 deletions errors_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package linodego

import (
"bytes"
"errors"
"io/ioutil"
"net/http"
"testing"

"github.com/go-resty/resty/v2"
"github.com/google/go-cmp/cmp"
)

func TestCoupleAPIErrors_badGatewayError(t *testing.T) {
rawResponse := []byte(`<html>
<head><title>502 Bad Gateway</title></head>
<body bgcolor="white">
<center><h1>502 Bad Gateway</h1></center>
<hr><center>nginx</center>
</body>
</html>`)
buf := ioutil.NopCloser(bytes.NewBuffer(rawResponse))

resp := &resty.Response{
Request: &resty.Request{
Error: errors.New("Bad Gateway"),
},
RawResponse: &http.Response{
Header: http.Header{
"Content-Type": []string{"text/html"},
},
StatusCode: http.StatusBadGateway,
Body: buf,
},
}

expectedError := Error{
Code: http.StatusBadGateway,
Message: http.StatusText(http.StatusBadGateway),
}

if _, err := coupleAPIErrors(resp, nil); !cmp.Equal(err, expectedError) {
t.Errorf("expected error %#v to match error %#v", err, expectedError)
}
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ require (
github.com/dnaeon/go-vcr v1.0.1
github.com/go-resty/resty/v2 v2.1.1-0.20191201195748-d7b97669fe48
github.com/golang/protobuf v1.2.0 // indirect
github.com/google/go-cmp v0.4.0
github.com/kr/pretty v0.1.0 // indirect
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f // indirect
Expand Down
3 changes: 3 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ github.com/go-resty/resty/v2 v2.1.1-0.20191201195748-d7b97669fe48 h1:JVrqSeQfdhY
github.com/go-resty/resty/v2 v2.1.1-0.20191201195748-d7b97669fe48/go.mod h1:dZGr0i9PLlaaTD4H/hoZIDjQ+r6xq8mgbRzHZf7f2J8=
github.com/golang/protobuf v1.2.0 h1:P3YflyNX/ehuJFLhxviNdFxQPkGK5cDcApsge1SqnvM=
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/google/go-cmp v0.4.0 h1:xsAVV57WRhGj6kEIi8ReJzQlHHqcBYCElAvkovg3B/4=
github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
Expand All @@ -19,6 +21,7 @@ golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJ
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42Tnys***REMOVED***UPdjciGhY=
golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/appengine v1.1.0 h1:igQkv0AAhEIvTEpD5LIpAfav2eeVO9HBTjvKHVJPRSs=
google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
Expand Down
14 changes: 3 additions & 11 deletions test/integration/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@ package integration

import (
"context"
"strings"
"testing"

. "github.com/linode/linodego"
)

func TestClientAliases(t *testing.T) {
Expand Down Expand Up @@ -47,13 +44,8 @@ func TestClient_APIResponseBadGateway(t *testing.T) {
t.Errorf("Error should be thrown on 502 Response from API")
}

responseError, ok := err.(*Error)

if !ok {
t.Errorf("Error type did not match the expected result")
}

if !strings.Contains(responseError.Message, "Unexpected Content-Type") {
t.Errorf("Error message does not contain: \"Unexpected Content-Type\"")
expectedErr := "[502] Bad Gateway"
if err.Error() != expectedErr {
t.Errorf(`expected error to be "%s" but got "%s"`, expectedErr, err)
}
}

0 comments on commit 401777d

Please sign in to comment.