Skip to content

Commit

Permalink
Merge pull request #14 from nigamishibumi/feature/return-http-status-…
Browse files Browse the repository at this point in the history
…code

enable to get status code when http get error occurs
  • Loading branch information
bearmini authored Apr 1, 2020
2 parents e679a6a + 6a5ceec commit 79b9c72
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 4 deletions.
10 changes: 6 additions & 4 deletions api_error.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ import (

// APIError represents an error ocurred while calling API
type APIError struct {
ErrorCode string
Message string
HTTPStatusCode int
ErrorCode string
Message string
}

type apiErrorResponse struct {
Expand Down Expand Up @@ -49,8 +50,9 @@ func NewAPIError(resp *http.Response) *APIError {
message = "Content-Type: " + ct + " is not supported"
}
return &APIError{
ErrorCode: errorCode,
Message: message,
HTTPStatusCode: resp.StatusCode,
ErrorCode: errorCode,
Message: message,
}
}

Expand Down
89 changes: 89 additions & 0 deletions api_error_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package soracom

import (
"bytes"
"io/ioutil"
"net/http"
"strings"
"testing"
)

func TestTextPlainApiError(t *testing.T) {
r := ioutil.NopCloser(strings.NewReader("test"))
h := http.Header{}
h.Set("Content-Type", "text/plain")
res := &http.Response{
StatusCode: 404,
Body: r,
Header: h,
}

apiError := NewAPIError(res)
if apiError.HTTPStatusCode != 404 {
t.Fatalf("wrong http status code: %v", apiError.HTTPStatusCode)
}

if apiError.ErrorCode != "UNK0001" {
t.Fatalf("wrong error code: %v", apiError.ErrorCode)
}
}

func TestJsonClientApiError(t *testing.T) {
json := `{"code":"SEM0095"}`
r := ioutil.NopCloser(bytes.NewReader([]byte(json)))
h := http.Header{}
h.Set("Content-Type", "application/json")
res := &http.Response{
StatusCode: 404,
Body: r,
Header: h,
}

apiError := NewAPIError(res)
if apiError.HTTPStatusCode != 404 {
t.Fatalf("wrong http status code: %v", apiError.HTTPStatusCode)
}

if apiError.ErrorCode != "SEM0095" {
t.Fatalf("wrong error code: %v", apiError.ErrorCode)
}
}

func TestJsonServerApiError(t *testing.T) {
json := `{}`
r := ioutil.NopCloser(bytes.NewReader([]byte(json)))
h := http.Header{}
h.Set("Content-Type", "application/json")
res := &http.Response{
StatusCode: 500,
Body: r,
Header: h,
}

apiError := NewAPIError(res)
if apiError.HTTPStatusCode != 500 {
t.Fatalf("wrong http status code: %v", apiError.HTTPStatusCode)
}

if apiError.ErrorCode != "" {
t.Fatalf("wrong error code: %v", apiError.ErrorCode)
}
}

func TestNotSupportedContentTypeApiError(t *testing.T) {
h := http.Header{}
h.Set("Content-Type", "application/pdf")
res := &http.Response{
StatusCode: 415,
Header: h,
}

apiError := NewAPIError(res)
if apiError.HTTPStatusCode != 415 {
t.Fatalf("wrong http status code: %v", apiError.HTTPStatusCode)
}

if apiError.ErrorCode != "INT0001" {
t.Fatalf("wrong error code: %v", apiError.ErrorCode)
}
}

0 comments on commit 79b9c72

Please sign in to comment.