Skip to content

Commit

Permalink
Merge pull request #71 from tamccall/error-type
Browse files Browse the repository at this point in the history
Updates to the zendesk Error type
  • Loading branch information
nukosuke committed Mar 13, 2019
2 parents 1c6ad93 + 4553e39 commit 6d7c454
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 19 deletions.
60 changes: 50 additions & 10 deletions zendesk/error_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,70 @@ import (
"testing"
)

func TestError_Response(t *testing.T) {
resp := &http.Response{}
func TestError_Error(t *testing.T) {
status := http.StatusOK
resp := &http.Response{
StatusCode: status,
}
body := []byte("foo")
err := Error{
msg: "foo",
body: body,
resp: resp,
}

if err.Response() != resp {
t.Fatal("Response did not return the provided response")
expected := fmt.Sprintf("%d: %s", status, body)
if v := err.Error(); v != expected {
t.Fatalf("Error %s did not have expected value %s", v, expected)
}
}

func TestError_Error(t *testing.T) {
func TestError_ErrorEmptyBody(t *testing.T) {
status := http.StatusOK
resp := &http.Response{
StatusCode: status,
}
body := "foo"
err := Error{
msg: body,
resp: resp,
}

if err.Error() != fmt.Sprintf("%d: %s", status, body) {
t.Fatal("Error did not have expected value")
expected := fmt.Sprintf("%d: %s", status, http.StatusText(status))
if v := err.Error(); v != expected {
t.Fatalf("Error %s did not have expected value %s", v, expected)
}
}

func TestError_Headers(t *testing.T) {
retryAfter := "Retry-After"
resp := &http.Response{
StatusCode: http.StatusTooManyRequests,
Header: http.Header{
retryAfter: []string{"92"},
},
}

err := Error{
resp: resp,
}

if _, ok := err.Headers()[retryAfter]; !ok {
t.Fatal("Could not get header values from zendesk error")
}
}

func TestError_Status(t *testing.T) {
retryAfter := "Retry-After"
resp := &http.Response{
StatusCode: http.StatusTooManyRequests,
Header: http.Header{
retryAfter: []string{"92"},
},
}

err := Error{
resp: resp,
}

if status := err.Status(); status != http.StatusTooManyRequests {
t.Fatal("Status returned from error was not the correct status code")
}
}
35 changes: 26 additions & 9 deletions zendesk/zendesk.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package zendesk

import (
"bytes"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/url"
Expand All @@ -23,18 +25,33 @@ var defaultHeaders = map[string]string{

// Error an error type containing the http response from zendesk
type Error struct {
body []byte
resp *http.Response
msg string
}

// Error the error string for this error
func (e Error) Error() string {
return fmt.Sprintf("%d: %s", e.resp.StatusCode, e.msg)
msg := string(e.body)
if msg == "" {
msg = http.StatusText(e.Status())
}

return fmt.Sprintf("%d: %s", e.resp.StatusCode, msg)
}

// Body is the Body of the HTTP response
func (e Error) Body() io.ReadCloser {
return ioutil.NopCloser(bytes.NewBuffer(e.body))
}

// Headers the HTTP headers returned from zendesk
func (e Error) Headers() http.Header {
return e.resp.Header
}

// Response the http response returned by zendesk
func (e Error) Response() *http.Response {
return e.resp
// Status the HTTP status code returned from zendesk
func (e Error) Status() int {
return e.resp.StatusCode
}

var subdomainRegexp = regexp.MustCompile("^[a-z][a-z0-9-]+[a-z0-9]$")
Expand Down Expand Up @@ -120,7 +137,7 @@ func (z Client) Get(path string) ([]byte, error) {

if resp.StatusCode != http.StatusOK {
return nil, Error{
msg: string(body),
body: body,
resp: resp,
}
}
Expand Down Expand Up @@ -153,7 +170,7 @@ func (z Client) Post(path string, data interface{}) ([]byte, error) {

if resp.StatusCode != http.StatusCreated {
return nil, Error{
msg: string(body),
body: body,
resp: resp,
}
}
Expand Down Expand Up @@ -187,7 +204,7 @@ func (z Client) Put(path string, data interface{}) ([]byte, error) {

if resp.StatusCode != http.StatusOK {
return nil, Error{
msg: string(body),
body: body,
resp: resp,
}
}
Expand Down Expand Up @@ -216,7 +233,7 @@ func (z Client) Delete(path string) error {

if resp.StatusCode != http.StatusNoContent {
return Error{
msg: string(body),
body: body,
resp: resp,
}
}
Expand Down
27 changes: 27 additions & 0 deletions zendesk/zendesk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,33 @@ func TestGetFailure(t *testing.T) {
}
}

func TestGetFailureCanReadErrorBody(t *testing.T) {
mockAPI := newMockAPIWithStatus(http.MethodGet, "groups.json", http.StatusInternalServerError)
client := newTestClient(mockAPI)
defer mockAPI.Close()

_, err := client.Get("/groups.json")
if err == nil {
t.Fatal("Did not receive error from client")
}

clientErr, ok := err.(Error)
if !ok {
t.Fatalf("Did not return a zendesk error %s", err)
}

body := clientErr.Body()
_, err = ioutil.ReadAll(body)
if err != nil {
t.Fatal("Client received error while reading client body")
}

err = body.Close()
if err != nil {
t.Fatal("Client received error while closing body")
}
}

func TestPost(t *testing.T) {
mockAPI := newMockAPIWithStatus(http.MethodPost, "groups.json", http.StatusCreated)
client := newTestClient(mockAPI)
Expand Down

0 comments on commit 6d7c454

Please sign in to comment.