errors.go 1.5 KB

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