errors.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. package ws
  2. // RejectOption represents an option used to control the way connection is
  3. // rejected.
  4. type RejectOption func(*rejectConnectionError)
  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 *rejectConnectionError) {
  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 *rejectConnectionError) {
  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 *rejectConnectionError) {
  23. err.header = h
  24. }
  25. }
  26. // RejectConnectionError constructs an error that could be used to control the way
  27. // handshake is rejected by Upgrader.
  28. func RejectConnectionError(options ...RejectOption) error {
  29. err := new(rejectConnectionError)
  30. for _, opt := range options {
  31. opt(err)
  32. }
  33. return err
  34. }
  35. // rejectConnectionError represents a rejection of upgrade error.
  36. //
  37. // It can be returned by Upgrader's On* hooks to control the way WebSocket
  38. // handshake is rejected.
  39. type rejectConnectionError struct {
  40. reason string
  41. code int
  42. header HandshakeHeader
  43. }
  44. // Error implements error interface.
  45. func (r *rejectConnectionError) Error() string {
  46. return r.reason
  47. }