github.com/ezoic/ws@v1.0.4-0.20220713205711-5c1d69e074c5/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(*rejectConnectionError)
     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 *rejectConnectionError) {
    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 *rejectConnectionError) {
    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 *rejectConnectionError) {
    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(rejectConnectionError)
    35  	for _, opt := range options {
    36  		opt(err)
    37  	}
    38  	return err
    39  }
    40  
    41  // rejectConnectionError 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 rejectConnectionError struct {
    46  	reason string
    47  	code   int
    48  	header HandshakeHeader
    49  }
    50  
    51  // Error implements error interface.
    52  func (r *rejectConnectionError) Error() string {
    53  	return r.reason
    54  }