Skip to content

Commit

Permalink
Add ErrTimeout
Browse files Browse the repository at this point in the history
  • Loading branch information
osamingo committed Feb 17, 2022
1 parent dbfda3e commit bb07dc2
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
12 changes: 10 additions & 2 deletions kenall.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ package kenall
import (
"context"
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"strconv"
"time"
)
Expand All @@ -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 (
Expand Down Expand Up @@ -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 {
Expand Down
6 changes: 6 additions & 0 deletions kenall_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
})

Expand All @@ -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 {
Expand Down Expand Up @@ -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()
})

Expand All @@ -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 {
Expand Down

0 comments on commit bb07dc2

Please sign in to comment.