Skip to content

Commit 5e5bd40

Browse files
authored
Merge pull request #410 from cisco-open/fix/httpclient_error_messages
fixed http client package's error message formatting
2 parents 297b7da + 4d576e8 commit 5e5bd40

File tree

4 files changed

+64
-11
lines changed

4 files changed

+64
-11
lines changed

pkg/integrate/httpclient/client.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ func (c *client) WithService(service string, opts ...SDOptions) (Client, error)
7676

7777
instancer, e := c.sdClient.Instancer(service)
7878
if e != nil {
79-
return nil, NewNoEndpointFoundError("cannot create client with service name: %v", e)
79+
return nil, NewNoEndpointFoundError(fmt.Errorf("cannot create client with service name: %s", service), e)
8080
}
8181

8282
defaultOpts := func(opts *SDOption) {
@@ -86,7 +86,7 @@ func (c *client) WithService(service string, opts ...SDOptions) (Client, error)
8686
opts = append([]SDOptions{defaultOpts}, opts...)
8787
targetResolver, e := NewSDTargetResolver(instancer, opts...)
8888
if e != nil {
89-
return nil, NewNoEndpointFoundError("cannot create client with service name: %v", e)
89+
return nil, NewNoEndpointFoundError(fmt.Errorf("cannot create client with service name: %s", service), e)
9090
}
9191

9292
cp := c.shallowCopy()
@@ -97,7 +97,7 @@ func (c *client) WithService(service string, opts ...SDOptions) (Client, error)
9797
func (c *client) WithBaseUrl(baseUrl string) (Client, error) {
9898
endpointer, e := NewStaticTargetResolver(baseUrl)
9999
if e != nil {
100-
return nil, NewNoEndpointFoundError("cannot create client with base URL: %v", e)
100+
return nil, NewNoEndpointFoundError(fmt.Errorf("cannot create client with base URL: %s", baseUrl), e)
101101
}
102102

103103
cp := c.shallowCopy()

pkg/integrate/httpclient/client_test.go

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,10 @@ func TestWithMockedServer(t *testing.T) {
103103
test.GomegaSubTest(SubTestWithNoInfoSD(&di), "TestWithNoInfoSD"),
104104
test.GomegaSubTest(SubTestWithSDNoResponseContent(&di), "TestWithSDNoResponseContent"),
105105
test.GomegaSubTest(SubTestWithBaseURL(&di), "TestWithBaseURL"),
106+
test.GomegaSubTest(SubTestWithBaseURLFailure(&di), "TestWithBaseUrlFailure"),
107+
test.GomegaSubTest(SubTestWithSDFailure(&di), "TestWithSDFailure"),
106108
test.GomegaSubTest(SubTestWithErrorResponse(&di), "TestWithErrorResponse"),
109+
test.GomegaSubTest(SubTestWithUnexpectedStatusCodeInErrorResponse(&di), "TestWithUnexpectedStatusCodeInErrorResponse"),
107110
test.GomegaSubTest(SubTestWithNoContentErrorResponse(&di), "SubTestWithNoContentErrorResponse"),
108111
test.GomegaSubTest(SubTestWithFailedSD(&di), "TestWithFailedSD"),
109112
test.GomegaSubTest(SubTestWithRetry(&di), "TestWithRetry"),
@@ -164,7 +167,7 @@ func SubTestWithNoInfoSD(di *TestDI) test.GomegaSubTestFunc {
164167
if len(target.Port()) != 0 {
165168
return nil, fmt.Errorf("target URL [%s] should not have port", target.String())
166169
}
167-
target.Host = fmt.Sprintf(`%s:%d`,target.Host, webtest.CurrentPort(ctx))
170+
target.Host = fmt.Sprintf(`%s:%d`, target.Host, webtest.CurrentPort(ctx))
168171
return http.NewRequestWithContext(ctx, method, target.String(), nil)
169172
}
170173
performEchoTest(ctx, t, g, client, httpclient.WithRequestCreator(urlRewriteCreator))
@@ -179,6 +182,16 @@ func SubTestWithSDNoResponseContent(di *TestDI) test.GomegaSubTestFunc {
179182
}
180183
}
181184

185+
func SubTestWithSDFailure(di *TestDI) test.GomegaSubTestFunc {
186+
return func(ctx context.Context, t *testing.T, g *gomega.WithT) {
187+
_, e := di.HttpClient.WithService("")
188+
//checking the error type, ignoring the error message
189+
g.Expect(e).To(MatchError(httpclient.NewNoEndpointFoundError("error message doesn't matter")))
190+
//check that the message is formatted
191+
g.Expect(e).To(MatchError(Not(ContainSubstring("%"))))
192+
}
193+
}
194+
182195
func SubTestWithBaseURL(di *TestDI) test.GomegaSubTestFunc {
183196
return func(ctx context.Context, t *testing.T, g *gomega.WithT) {
184197
baseUrl := fmt.Sprintf(`http://localhost:%d%s`, webtest.CurrentPort(ctx), webtest.CurrentContextPath(ctx))
@@ -188,6 +201,18 @@ func SubTestWithBaseURL(di *TestDI) test.GomegaSubTestFunc {
188201
}
189202
}
190203

204+
func SubTestWithBaseURLFailure(di *TestDI) test.GomegaSubTestFunc {
205+
return func(ctx context.Context, t *testing.T, g *gomega.WithT) {
206+
baseUrl := webtest.CurrentContextPath(ctx)
207+
_, e := di.HttpClient.WithBaseUrl(baseUrl)
208+
//checking the error type, ignoring the error message
209+
g.Expect(e).To(MatchError(httpclient.NewNoEndpointFoundError("error message doesn't matter")))
210+
//check that the message is formatted
211+
g.Expect(e).To(MatchError(Not(ContainSubstring("%"))))
212+
g.Expect(e).To(MatchError(ContainSubstring(baseUrl)))
213+
}
214+
}
215+
191216
func SubTestWithErrorResponse(di *TestDI) test.GomegaSubTestFunc {
192217
return func(ctx context.Context, t *testing.T, g *gomega.WithT) {
193218
client, e := di.HttpClient.WithService(SDServiceNameFullInfo)
@@ -205,6 +230,26 @@ func SubTestWithErrorResponse(di *TestDI) test.GomegaSubTestFunc {
205230
}
206231
}
207232

233+
func SubTestWithUnexpectedStatusCodeInErrorResponse(di *TestDI) test.GomegaSubTestFunc {
234+
return func(ctx context.Context, t *testing.T, g *gomega.WithT) {
235+
client, e := di.HttpClient.WithService(SDServiceNameFullInfo)
236+
g.Expect(e).To(Succeed(), "client with service name should be available")
237+
238+
sc := 300 + utils.RandomIntN(10)
239+
reqBody := makeEchoRequestBody()
240+
req := httpclient.NewRequest(TestErrorPath, http.MethodPut,
241+
httpclient.WithParam("sc", fmt.Sprintf("%d", sc)),
242+
httpclient.WithBody(reqBody),
243+
)
244+
245+
_, err := client.Execute(ctx, req, httpclient.JsonBody(&EchoResponse{}))
246+
// check the error type is the expected type
247+
g.Expect(err).To(MatchError(httpclient.ErrorSubTypeInternalError))
248+
// check the error message is formatted
249+
g.Expect(err).To(MatchError(Not(ContainSubstring("%"))))
250+
}
251+
}
252+
208253
func SubTestWithNoContentErrorResponse(di *TestDI) test.GomegaSubTestFunc {
209254
return func(ctx context.Context, t *testing.T, g *gomega.WithT) {
210255
client, e := di.HttpClient.WithService(SDServiceNameFullInfo)

pkg/integrate/httpclient/error.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -176,9 +176,12 @@ func (e Error) WithMessage(msg string, args ...interface{}) *Error {
176176
return newError(NewCodedError(e.CodedError.Code(), fmt.Errorf(msg, args...)), e.Response)
177177
}
178178

179-
/**********************
180-
Constructors
181-
**********************/
179+
/*
180+
*********************
181+
182+
Constructors
183+
*********************
184+
*/
182185
func newError(codedErr *CodedError, errResp *ErrorResponse) *Error {
183186
err := &Error{
184187
CodedError: *codedErr,
@@ -218,7 +221,7 @@ func NewErrorWithStatusCode(e interface{}, resp *http.Response, rawBody []byte,
218221
case resp.StatusCode >= 500 && resp.StatusCode <= 599:
219222
code = ErrorCodeGenericServerSide
220223
default:
221-
return NewError(ErrorCodeInternal, "attempt to create response error with non error status code %d", resp.StatusCode)
224+
return NewError(ErrorCodeInternal, fmt.Errorf("attempt to create response error with non error status code %d", resp.StatusCode))
222225
}
223226
return NewErrorWithResponse(code, e, resp, rawBody, causes...)
224227
}

test/sdtest/client.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,19 @@ package sdtest
1818

1919
import (
2020
"context"
21+
"fmt"
2122
"github.com/cisco-open/go-lanai/pkg/discovery"
2223
"time"
2324
)
2425

2526
type ClientMock struct {
26-
ctx context.Context
27+
ctx context.Context
2728
Instancers map[string]*InstancerMock
2829
}
2930

3031
func NewMockClient(ctx context.Context) *ClientMock {
3132
return &ClientMock{
32-
ctx: ctx,
33+
ctx: ctx,
3334
Instancers: map[string]*InstancerMock{},
3435
}
3536
}
@@ -41,6 +42,10 @@ func (c *ClientMock) Context() context.Context {
4142
}
4243

4344
func (c *ClientMock) Instancer(serviceName string) (discovery.Instancer, error) {
45+
if serviceName == "" {
46+
return nil, fmt.Errorf("empty service name")
47+
}
48+
4449
if instancer, ok := c.Instancers[serviceName]; ok {
4550
return instancer, nil
4651
}
@@ -68,4 +73,4 @@ func (c *ClientMock) UpdateMockedService(svcName string, matcher InstanceMockMat
6873
func (c *ClientMock) MockError(svcName string, what error, when time.Time) {
6974
instancer, _ := c.Instancer(svcName)
7075
instancer.(*InstancerMock).MockError(what, when)
71-
}
76+
}

0 commit comments

Comments
 (0)