File: errors.go

package info (click to toggle)
golang-github-gobwas-ws 1.4.0-3
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 560 kB
  • sloc: sh: 98; makefile: 48
file content (59 lines) | stat: -rw-r--r-- 1,574 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package ws

// RejectOption represents an option used to control the way connection is
// rejected.
type RejectOption func(*ConnectionRejectedError)

// RejectionReason returns an option that makes connection to be rejected with
// given reason.
func RejectionReason(reason string) RejectOption {
	return func(err *ConnectionRejectedError) {
		err.reason = reason
	}
}

// RejectionStatus returns an option that makes connection to be rejected with
// given HTTP status code.
func RejectionStatus(code int) RejectOption {
	return func(err *ConnectionRejectedError) {
		err.code = code
	}
}

// RejectionHeader returns an option that makes connection to be rejected with
// given HTTP headers.
func RejectionHeader(h HandshakeHeader) RejectOption {
	return func(err *ConnectionRejectedError) {
		err.header = h
	}
}

// RejectConnectionError constructs an error that could be used to control the
// way handshake is rejected by Upgrader.
func RejectConnectionError(options ...RejectOption) error {
	err := new(ConnectionRejectedError)
	for _, opt := range options {
		opt(err)
	}
	return err
}

// ConnectionRejectedError represents a rejection of connection during
// WebSocket handshake error.
//
// It can be returned by Upgrader's On* hooks to indicate that WebSocket
// handshake should be rejected.
type ConnectionRejectedError struct {
	reason string
	code   int
	header HandshakeHeader
}

// Error implements error interface.
func (r *ConnectionRejectedError) Error() string {
	return r.reason
}

func (r *ConnectionRejectedError) StatusCode() int {
	return r.code
}