Skip to content

Commit 90d759e

Browse files
ashwini-rajaAshwini Raja
andauthored
Sending back more information in BadResponseError (#184)
* returning body for error * minor * spacing * updating verb and excluding macos-latest for v1.23 in test and build workflows * removing adding to v1 * changes to build and test yml --------- Co-authored-by: Ashwini Raja <[email protected]>
1 parent f30fede commit 90d759e

File tree

7 files changed

+76
-7
lines changed

7 files changed

+76
-7
lines changed

.github/workflows/build.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ jobs:
77
build:
88
strategy:
99
matrix:
10-
go-version: ["1.23.x", "1.24.x"]
11-
os: [ ubuntu-latest, macos-latest, windows-latest ]
10+
go-version: ["1.24.x", "1.25.x"]
11+
os: [ ubuntu-latest ]
1212
runs-on: ${{ matrix.os }}
1313
steps:
1414
- name: Harden the runner (Audit all outbound calls)

.github/workflows/test.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ jobs:
77
test:
88
strategy:
99
matrix:
10-
go-version: ["1.23.x", "1.24.x"]
11-
os: [ ubuntu-latest, macos-latest, windows-latest ]
10+
go-version: ["1.24.x", "1.25.x"]
11+
os: [ ubuntu-latest]
1212
runs-on: ${{ matrix.os }}
1313
steps:
1414
- name: Harden the runner (Audit all outbound calls)

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,6 @@ go.work.sum
2121
# IDE files
2222
*idea
2323
*iml
24+
25+
# vscode
26+
.vscode

v2/client_test.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,3 +189,35 @@ func TestCanDoubleSubscribe(t *testing.T) {
189189

190190
wg.Wait()
191191
}
192+
193+
func TestErrorParsing(t *testing.T) {
194+
server := gobayeuxtest.NewServer(t, gobayeuxtest.WithHandshakeError(true))
195+
if err := server.Start(context.Background()); err != nil {
196+
t.Fatalf("failed to start test server (%v)", err)
197+
}
198+
199+
client, err := gobayeux.NewClient(
200+
"https://example.com",
201+
gobayeux.WithHTTPTransport(server),
202+
)
203+
if err != nil {
204+
t.Fatalf("failed to create client (%v)", err)
205+
}
206+
207+
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
208+
defer cancel()
209+
210+
errs := client.Start(ctx)
211+
err = <-errs
212+
if err == nil {
213+
t.Fatal("expected an error when connecting")
214+
}
215+
216+
if err.Error() != `expected 200 response from bayeux server, got 400 with status 'Bad Request' and body '"{\"error\":\"Invalid request\"}"'` {
217+
t.Errorf("expected different error when connecting; got %v", err)
218+
}
219+
220+
if err := server.Stop(context.Background()); err != nil {
221+
t.Fatalf("failed to stop test server (%v)", err)
222+
}
223+
}

v2/errors.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,9 +153,10 @@ type BadResponseError struct {
153153

154154
func (e BadResponseError) Error() string {
155155
return fmt.Sprintf(
156-
"expected 200 response from bayeux server, got %d with status '%s'",
156+
"expected 200 response from bayeux server, got %d with status '%s' and body '%q'",
157157
e.StatusCode,
158158
e.Status,
159+
e.Body,
159160
)
160161
}
161162

v2/internal/gobayeuxtest/server.go

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,22 @@ type Server struct {
4040
mu sync.Mutex
4141
running bool
4242
subs map[string][]gobayeux.Channel
43+
44+
handshakeError bool
4345
}
4446

45-
func NewServer(logger Logger) *Server {
46-
return &Server{
47+
func NewServer(logger Logger, opts ...ServerOpts) *Server {
48+
server := &Server{
4749
log: logger,
4850
subs: make(map[string][]gobayeux.Channel),
4951
}
52+
53+
for _, opt := range opts {
54+
opt.apply(server)
55+
}
56+
57+
return server
58+
5059
}
5160

5261
func (s *Server) Start(context.Context) error {
@@ -101,6 +110,13 @@ func (s *Server) RoundTrip(req *http.Request) (*http.Response, error) {
101110
for _, msg := range msgs {
102111
switch msg.Channel {
103112
case "/meta/handshake":
113+
if s.handshakeError {
114+
return &http.Response{
115+
StatusCode: http.StatusBadRequest,
116+
Status: http.StatusText(http.StatusBadRequest),
117+
Body: io.NopCloser(bytes.NewReader([]byte(`{"error":"Invalid request"}`))),
118+
}, nil
119+
}
104120
replies = append(replies, &gobayeux.Message{
105121
Channel: "/meta/handshake",
106122
Version: msg.Version,
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package gobayeuxtest
2+
3+
type ServerOpts interface {
4+
apply(s *Server)
5+
}
6+
7+
type serverOptFn func(s *Server)
8+
9+
func (opt serverOptFn) apply(s *Server) {
10+
opt(s)
11+
}
12+
13+
func WithHandshakeError(handshakeError bool) ServerOpts {
14+
return serverOptFn(func(s *Server) {
15+
s.handshakeError = handshakeError
16+
})
17+
}

0 commit comments

Comments
 (0)