diff --git a/kenall.go b/kenall.go index a0614ef..999159e 100644 --- a/kenall.go +++ b/kenall.go @@ -3,10 +3,12 @@ package kenall import ( "context" "encoding/json" + "errors" "fmt" "io" "io/ioutil" "net/http" + "os" "strconv" "time" ) @@ -33,6 +35,8 @@ var ( ErrMethodNotAllowed = fmt.Errorf("kenall: 405 method not allowed error") // ErrInternalServerError is an error value that will be returned when some error occurs in the kenall service. ErrInternalServerError = fmt.Errorf("kenall: 500 internal server error") + // ErrTimeout is an error value that will be returned when the request is timeout. + ErrTimeout = func(err error) error { return fmt.Errorf("kenall: request timeout: %w", err) } ) type ( @@ -187,12 +191,16 @@ func (cli *Client) sendRequest(req *http.Request, res interface{}) error { resp, err := cli.HTTPClient.Do(req) if err != nil { + if errors.Is(err, context.DeadlineExceeded) || os.IsTimeout(err) { + return ErrTimeout(err) + } + return fmt.Errorf("kenall: failed to do http client with a request for kenall service: %w", err) } defer func() { - defer resp.Body.Close() - io.Copy(ioutil.Discard, resp.Body) + _ = resp.Body.Close() + _, _ = io.Copy(ioutil.Discard, resp.Body) }() switch resp.StatusCode { diff --git a/kenall_test.go b/kenall_test.go index 5c90c28..3b3888a 100644 --- a/kenall_test.go +++ b/kenall_test.go @@ -71,8 +71,10 @@ func TestNewClient(t *testing.T) { func TestClient_GetAddress(t *testing.T) { t.Parallel() + toctx, cancel := context.WithTimeout(context.Background(), 1*time.Nanosecond) srv := runTestingServer(t) t.Cleanup(func() { + cancel() srv.Close() }) @@ -97,6 +99,7 @@ func TestClient_GetAddress(t *testing.T) { "Wrong endpoint": {endpoint: "", token: "opencollector", ctx: context.Background(), postalCode: "0000000", checkAsError: true, wantError: &url.Error{}, wantJISX0402: ""}, "Wrong response": {endpoint: srv.URL, token: "opencollector", ctx: context.Background(), postalCode: "0000001", checkAsError: true, wantError: &json.MarshalerError{}, wantJISX0402: ""}, "Nil context": {endpoint: srv.URL, token: "opencollector", ctx: nil, postalCode: "0000000", checkAsError: true, wantError: errors.New("net/http: nil Context"), wantJISX0402: ""}, + "Timeout context": {endpoint: srv.URL, token: "opencollector", ctx: toctx, postalCode: "1008105", checkAsError: true, wantError: kenall.ErrTimeout(context.DeadlineExceeded), wantJISX0402: ""}, } for name, c := range cases { @@ -126,8 +129,10 @@ func TestClient_GetAddress(t *testing.T) { func TestClient_GetCity(t *testing.T) { t.Parallel() + toctx, cancel := context.WithTimeout(context.Background(), 1*time.Nanosecond) srv := runTestingServer(t) t.Cleanup(func() { + cancel() srv.Close() }) @@ -152,6 +157,7 @@ func TestClient_GetCity(t *testing.T) { "Wrong endpoint": {endpoint: "", token: "opencollector", ctx: context.Background(), prefectureCode: "00", checkAsError: true, wantError: &url.Error{}, wantJISX0402: ""}, "Wrong response": {endpoint: srv.URL, token: "opencollector", ctx: context.Background(), prefectureCode: "95", checkAsError: true, wantError: &json.MarshalerError{}, wantJISX0402: ""}, "Nil context": {endpoint: srv.URL, token: "opencollector", ctx: nil, prefectureCode: "00", checkAsError: true, wantError: errors.New("net/http: nil Context"), wantJISX0402: ""}, + "Timeout context": {endpoint: srv.URL, token: "opencollector", ctx: toctx, prefectureCode: "13", checkAsError: true, wantError: kenall.ErrTimeout(context.DeadlineExceeded), wantJISX0402: ""}, } for name, c := range cases {