Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ISSUE-80] Add proper timeout error handling #79

Merged
merged 2 commits into from
Jul 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions examples/table_test/table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,3 +152,26 @@ func Test_Array(t *testing.T) {
test.Execute(context.Background(), t)
}
}

func Test_Array_Timeout(t *testing.T) {
tests := []*cute.Test{
{
Name: "test_timeout",
Middleware: nil,
Request: &cute.Request{
Builders: []cute.RequestBuilder{
cute.WithURI("https://httpstat.us/202?sleep=3000"),
cute.WithMethod(http.MethodGet),
},
},
Expect: &cute.Expect{
Code: 202,
ExecuteTime: 2,
},
},
}

for _, test := range tests {
test.Execute(context.Background(), t)
}
}
15 changes: 15 additions & 0 deletions roundtripper.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cute

import (
"context"
"errors"
"fmt"
"io"
"net/http"
Expand Down Expand Up @@ -72,6 +73,20 @@ func (it *Test) doRequest(t T, baseReq *http.Request) (*http.Response, error) {
}

resp, httpErr := it.httpClient.Do(req)
// if the timeout is triggered, we properly log the timeout error on allure and in traces
if errors.Is(httpErr, context.DeadlineExceeded) {
curl, err := http2curl.GetCurlCommand(req)
if err != nil {
return nil, err
}

it.Error(t, "Request timeout Curl: %v", curl)

cuteError := cuteErrors.NewCuteError("[HTTP] Request timeout", httpErr)
cuteError.Fields = map[string]interface{}{"curl": curl}
cuteError.Message = "Request timeout"
return nil, cuteError
}

// http client has case wheh it return response and error in one time
// we have to check this case
Expand Down