github.com/simonmittag/ws@v1.1.0-rc.5.0.20210419231947-82b846128245/errors.go (about) 1 package ws 2 3 // RejectOption represents an option used to control the way connection is 4 // rejected. 5 type RejectOption func(*RejectConnectionErrorType) 6 7 // RejectionReason returns an option that makes connection to be rejected with 8 // given reason. 9 func RejectionReason(reason string) RejectOption { 10 return func(err *RejectConnectionErrorType) { 11 err.reason = reason 12 } 13 } 14 15 // RejectionStatus returns an option that makes connection to be rejected with 16 // given HTTP status code. 17 func RejectionStatus(code int) RejectOption { 18 return func(err *RejectConnectionErrorType) { 19 err.code = code 20 } 21 } 22 23 // RejectionHeader returns an option that makes connection to be rejected with 24 // given HTTP headers. 25 func RejectionHeader(h HandshakeHeader) RejectOption { 26 return func(err *RejectConnectionErrorType) { 27 err.header = h 28 } 29 } 30 31 // RejectConnectionError constructs an error that could be used to control the way 32 // handshake is rejected by Upgrader. 33 func RejectConnectionError(options ...RejectOption) error { 34 err := new(RejectConnectionErrorType) 35 for _, opt := range options { 36 opt(err) 37 } 38 return err 39 } 40 41 // RejectConnectionErrorType represents a rejection of upgrade error. 42 // 43 // It can be returned by Upgrader's On* hooks to control the way WebSocket 44 // handshake is rejected. 45 type RejectConnectionErrorType struct { 46 reason string 47 code int 48 header HandshakeHeader 49 } 50 51 // Error implements error interface. 52 func (r *RejectConnectionErrorType) Error() string { 53 return r.reason 54 } 55 56 func (r *RejectConnectionErrorType) Code() int { 57 return r.code 58 }