-
Notifications
You must be signed in to change notification settings - Fork 0
/
errors.go
90 lines (78 loc) · 2.42 KB
/
errors.go
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package websocket
import (
"encoding/binary"
"errors"
"fmt"
)
// CloseError represents errors related to the websocket closing handshake.
type CloseError struct {
Code int
Reason string
}
// Error implements the built in error interface.
func (c *CloseError) Error() string {
return fmt.Sprintf("Close Error: %d %s", c.Code, c.Reason)
}
// ToBytes returns the representation of a CloseError instance in a []bytes
// that conforms with the way the websocket rfc expects the payload data of
// CLOSE FRAMES to be.
//
// While generating the []bytes, if the CloseError instance has an invalid
// error code, it will instead create the representation of a 'No Status
// Received Error' (i.e. 1005).
//
// Ref Spec: https://tools.ietf.org/html/rfc6455#section-5.5.1
func (c *CloseError) ToBytes() ([]byte, error) {
// Validate Error Code
if !closeErrorExist(c.Code) {
// If it is not valid, return bytes for No Status Received error.
n := &CloseError{
Code: CloseNoStatusReceived,
Reason: "no status recieved",
}
b, _ := n.ToBytes()
return b, errors.New("invalid error code")
}
return append(c.toBytesCode(), []byte(c.Reason)...), nil
}
// toBytesCode is used to get a representation of the CloseError instance
// status code in []bytes.
func (c *CloseError) toBytesCode() []byte {
b := make([]byte, 2)
binary.BigEndian.PutUint16(b, uint16(c.Code))
return b
}
// NewCloseError is used to create a new CloseError instance by parsing 'b'. In
// order for this to happen the []bytes needs to conform with the way the
// websocket rfc expects the payload data of CLOSE FRAMES to be.
//
// While parsing if the error code (i.e. first two bytes) is invalid, it will
// default the CloseError instance returned to represent a 'No Status Received
// Error' (i.e. 1005).
//
// Ref Spec: https://tools.ietf.org/html/rfc6455#section-5.5.1
func NewCloseError(b []byte) (*CloseError, error) {
var c int
if len(b) >= 2 {
cb := b[:2]
c = int(binary.BigEndian.Uint16(cb))
}
if !closeErrorExist(c) {
return &CloseError{
Code: CloseNoStatusReceived,
Reason: "no status recieved",
}, errors.New("invalid error code")
}
return &CloseError{
Code: c,
Reason: string(b[2:]),
}, nil
}
// OpenError represents errors related to the websocket opening handshake.
type OpenError struct {
Reason string
}
// Error implements the built in error interface.
func (h *OpenError) Error() string {
return "Handshake Error: " + h.Reason
}