Skip to content

Commit 272ac76

Browse files
authored
errors: allow retrieve rejection status code (#139)
Fixes #136
1 parent d2c3dba commit 272ac76

File tree

2 files changed

+19
-15
lines changed

2 files changed

+19
-15
lines changed

errors.go

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,54 +2,58 @@ package ws
22

33
// RejectOption represents an option used to control the way connection is
44
// rejected.
5-
type RejectOption func(*rejectConnectionError)
5+
type RejectOption func(*ConnectionRejectedError)
66

77
// RejectionReason returns an option that makes connection to be rejected with
88
// given reason.
99
func RejectionReason(reason string) RejectOption {
10-
return func(err *rejectConnectionError) {
10+
return func(err *ConnectionRejectedError) {
1111
err.reason = reason
1212
}
1313
}
1414

1515
// RejectionStatus returns an option that makes connection to be rejected with
1616
// given HTTP status code.
1717
func RejectionStatus(code int) RejectOption {
18-
return func(err *rejectConnectionError) {
18+
return func(err *ConnectionRejectedError) {
1919
err.code = code
2020
}
2121
}
2222

2323
// RejectionHeader returns an option that makes connection to be rejected with
2424
// given HTTP headers.
2525
func RejectionHeader(h HandshakeHeader) RejectOption {
26-
return func(err *rejectConnectionError) {
26+
return func(err *ConnectionRejectedError) {
2727
err.header = h
2828
}
2929
}
3030

31-
// RejectConnectionError constructs an error that could be used to control the way
32-
// handshake is rejected by Upgrader.
31+
// RejectConnectionError constructs an error that could be used to control the
32+
// way handshake is rejected by Upgrader.
3333
func RejectConnectionError(options ...RejectOption) error {
34-
err := new(rejectConnectionError)
34+
err := new(ConnectionRejectedError)
3535
for _, opt := range options {
3636
opt(err)
3737
}
3838
return err
3939
}
4040

41-
// rejectConnectionError represents a rejection of connection during WebSocket
42-
// handshake error.
41+
// ConnectionRejectedError represents a rejection of connection during
42+
// WebSocket handshake error.
4343
//
44-
// It can be returned by Upgrader's On* hooks to control the way WebSocket
45-
// handshake is rejected.
46-
type rejectConnectionError struct {
44+
// It can be returned by Upgrader's On* hooks to indicate that WebSocket
45+
// handshake should be rejected.
46+
type ConnectionRejectedError struct {
4747
reason string
4848
code int
4949
header HandshakeHeader
5050
}
5151

5252
// Error implements error interface.
53-
func (r *rejectConnectionError) Error() string {
53+
func (r *ConnectionRejectedError) Error() string {
5454
return r.reason
5555
}
56+
57+
func (r *ConnectionRejectedError) StatusCode() int {
58+
return r.code
59+
}

server.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ func (u HTTPUpgrader) Upgrade(r *http.Request, w http.ResponseWriter) (conn net.
251251
err = rw.Writer.Flush()
252252
} else {
253253
var code int
254-
if rej, ok := err.(*rejectConnectionError); ok {
254+
if rej, ok := err.(*ConnectionRejectedError); ok {
255255
code = rej.code
256256
header[1] = rej.header
257257
}
@@ -630,7 +630,7 @@ func (u Upgrader) Upgrade(conn io.ReadWriter) (hs Handshake, err error) {
630630
}
631631
if err != nil {
632632
var code int
633-
if rej, ok := err.(*rejectConnectionError); ok {
633+
if rej, ok := err.(*ConnectionRejectedError); ok {
634634
code = rej.code
635635
header[1] = rej.header
636636
}

0 commit comments

Comments
 (0)