fix: use fmt.Errorf() %w verb for error wrapping
This commit is contained in:
parent
d092eaed01
commit
9e69b66289
|
@ -50,12 +50,12 @@ func buildClient(s net.Conn, ctl ControllerSet, serverCh serverChannelSet) (*cli
|
||||||
// 4. Write into socket
|
// 4. Write into socket
|
||||||
_, err := res.Send(s)
|
_, err := res.Send(s)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("Upgrade write error: %s", err)
|
return nil, fmt.Errorf("upgrade write: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if res.GetStatusCode() != 101 {
|
if res.GetStatusCode() != 101 {
|
||||||
s.Close()
|
s.Close()
|
||||||
return nil, fmt.Errorf("Upgrade error (HTTP %d)", res.GetStatusCode())
|
return nil, fmt.Errorf("upgrade failed (HTTP %d)", res.GetStatusCode())
|
||||||
}
|
}
|
||||||
|
|
||||||
/* (2) Initialise client
|
/* (2) Initialise client
|
||||||
|
|
|
@ -38,7 +38,7 @@ func ReadHeader(b []byte) (*Header, error) {
|
||||||
parts := bytes.Split(b, []byte(": "))
|
parts := bytes.Split(b, []byte(": "))
|
||||||
|
|
||||||
if len(parts) != 2 {
|
if len(parts) != 2 {
|
||||||
return nil, fmt.Errorf("Invalid HTTP header format '%s'", b)
|
return nil, fmt.Errorf("invalid HTTP header format '%s'", b)
|
||||||
}
|
}
|
||||||
|
|
||||||
// 2. Create instance
|
// 2. Create instance
|
||||||
|
|
|
@ -67,7 +67,7 @@ func (r *Line) extractHttpMethod(b []byte) error {
|
||||||
// case "DELETE": r.method = DELETE
|
// case "DELETE": r.method = DELETE
|
||||||
|
|
||||||
default:
|
default:
|
||||||
return fmt.Errorf("Invalid HTTP method '%s', expected 'GET'", b)
|
return fmt.Errorf("invalid HTTP method '%s', expected 'GET'", b)
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
@ -98,7 +98,7 @@ func (r *Line) extractHttpVersion(b []byte) error {
|
||||||
extractor := regexp.MustCompile(`^HTTP/([1-9])(?:\.([0-9]))?$`)
|
extractor := regexp.MustCompile(`^HTTP/([1-9])(?:\.([0-9]))?$`)
|
||||||
|
|
||||||
if !extractor.Match(b) {
|
if !extractor.Match(b) {
|
||||||
return fmt.Errorf("HTTP version, expected INT or INT.INT, got '%s'", b)
|
return fmt.Errorf("invalid HTTP version, expected INT or INT.INT, got '%s'", b)
|
||||||
}
|
}
|
||||||
|
|
||||||
// 2. Extract version number
|
// 2. Extract version number
|
||||||
|
|
|
@ -49,7 +49,7 @@ func Parse(r io.Reader) (request *Request, err error) {
|
||||||
// 1. Get chunk reader
|
// 1. Get chunk reader
|
||||||
cr := reader.NewReader(r)
|
cr := reader.NewReader(r)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return req, fmt.Errorf("Error while creating chunk reader: %s", err)
|
return req, fmt.Errorf("create chunk reader: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// 2. Parse header line by line
|
// 2. Parse header line by line
|
||||||
|
@ -139,7 +139,7 @@ func (r *Request) parseHeader(b []byte) error {
|
||||||
head, err := ReadHeader(b)
|
head, err := ReadHeader(b)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
r.code = BadRequest
|
r.code = BadRequest
|
||||||
return fmt.Errorf("Error parsing header: %s", err)
|
return fmt.Errorf("parse header: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// 2. Manage header
|
// 2. Manage header
|
||||||
|
|
|
@ -6,23 +6,6 @@ import (
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
// // 1. Parse request
|
|
||||||
// req, _ := request.Parse(s)
|
|
||||||
|
|
||||||
// // 3. Build response
|
|
||||||
// res := req.BuildResponse()
|
|
||||||
|
|
||||||
// // 4. Write into socket
|
|
||||||
// _, err := res.Send(s)
|
|
||||||
// if err != nil {
|
|
||||||
// return nil, fmt.Errorf("Upgrade write error: %s", err)
|
|
||||||
// }
|
|
||||||
|
|
||||||
// if res.GetStatusCode() != 101 {
|
|
||||||
// s.Close()
|
|
||||||
// return nil, fmt.Errorf("Upgrade error (HTTP %d)\n", res.GetStatusCode())
|
|
||||||
// }
|
|
||||||
|
|
||||||
func TestEOFSocket(t *testing.T) {
|
func TestEOFSocket(t *testing.T) {
|
||||||
|
|
||||||
socket := &bytes.Buffer{}
|
socket := &bytes.Buffer{}
|
||||||
|
|
|
@ -44,7 +44,7 @@ func FromString(s string) (*Scheme, error) {
|
||||||
|
|
||||||
// 1. Manage '/' at the start
|
// 1. Manage '/' at the start
|
||||||
if len(s) < 1 || s[0] != '/' {
|
if len(s) < 1 || s[0] != '/' {
|
||||||
return nil, fmt.Errorf("URI must begin with '/'")
|
return nil, fmt.Errorf("invalid URI; must start with '/'")
|
||||||
}
|
}
|
||||||
|
|
||||||
// 2. Split by '/'
|
// 2. Split by '/'
|
||||||
|
@ -102,7 +102,7 @@ func (s Scheme) GetMatch(n uint8) ([]string, error) {
|
||||||
|
|
||||||
// 1. Index out of range
|
// 1. Index out of range
|
||||||
if n > uint8(len(s)) {
|
if n > uint8(len(s)) {
|
||||||
return nil, fmt.Errorf("Index out of range")
|
return nil, fmt.Errorf("index out of range")
|
||||||
}
|
}
|
||||||
|
|
||||||
// 2. Iterate to find index (exclude strings)
|
// 2. Iterate to find index (exclude strings)
|
||||||
|
@ -125,7 +125,7 @@ func (s Scheme) GetMatch(n uint8) ([]string, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// 3. If nothing found -> return empty set
|
// 3. If nothing found -> return empty set
|
||||||
return nil, fmt.Errorf("Index out of range (max: %d)", ni)
|
return nil, fmt.Errorf("index out of range (max: %d)", ni)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
20
message.go
20
message.go
|
@ -13,25 +13,25 @@ func (c constErr) Error() string { return string(c) }
|
||||||
|
|
||||||
const (
|
const (
|
||||||
// ErrUnmaskedFrame error
|
// ErrUnmaskedFrame error
|
||||||
ErrUnmaskedFrame = constErr("Received unmasked frame")
|
ErrUnmaskedFrame constErr = "Received unmasked frame"
|
||||||
// ErrTooLongControlFrame error
|
// ErrTooLongControlFrame error
|
||||||
ErrTooLongControlFrame = constErr("Received a control frame that is fragmented or too long")
|
ErrTooLongControlFrame constErr = "Received a control frame that is fragmented or too long"
|
||||||
// ErrInvalidFragment error
|
// ErrInvalidFragment error
|
||||||
ErrInvalidFragment = constErr("Received invalid fragmentation")
|
ErrInvalidFragment constErr = "Received invalid fragmentation"
|
||||||
// ErrUnexpectedContinuation error
|
// ErrUnexpectedContinuation error
|
||||||
ErrUnexpectedContinuation = constErr("Received unexpected continuation frame")
|
ErrUnexpectedContinuation constErr = "Received unexpected continuation frame"
|
||||||
// ErrInvalidSize error
|
// ErrInvalidSize error
|
||||||
ErrInvalidSize = constErr("Received invalid payload size")
|
ErrInvalidSize constErr = "Received invalid payload size"
|
||||||
// ErrInvalidPayload error
|
// ErrInvalidPayload error
|
||||||
ErrInvalidPayload = constErr("Received invalid utf8 payload")
|
ErrInvalidPayload constErr = "Received invalid utf8 payload"
|
||||||
// ErrInvalidCloseStatus error
|
// ErrInvalidCloseStatus error
|
||||||
ErrInvalidCloseStatus = constErr("Received invalid close status")
|
ErrInvalidCloseStatus constErr = "Received invalid close status"
|
||||||
// ErrInvalidOpCode error
|
// ErrInvalidOpCode error
|
||||||
ErrInvalidOpCode = constErr("Received invalid OpCode")
|
ErrInvalidOpCode constErr = "Received invalid OpCode"
|
||||||
// ErrReservedBits error
|
// ErrReservedBits error
|
||||||
ErrReservedBits = constErr("Received reserved bits")
|
ErrReservedBits constErr = "Received reserved bits"
|
||||||
// ErrCloseFrame error
|
// ErrCloseFrame error
|
||||||
ErrCloseFrame = constErr("Received close Frame")
|
ErrCloseFrame constErr = "Received close Frame"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Maximum Header Size = Final/OpCode + isMask/Length + Length + Mask
|
// Maximum Header Size = Final/OpCode + isMask/Length + Length + Mask
|
||||||
|
|
|
@ -68,7 +68,7 @@ func (s *Server) Bind(uriStr string, f ControllerFunc) error {
|
||||||
// 1. Build URI parser
|
// 1. Build URI parser
|
||||||
uriScheme, err := uri.FromString(uriStr)
|
uriScheme, err := uri.FromString(uriStr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("Cannot build URI: %s", err)
|
return fmt.Errorf("cannot build URI: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// 2. Create controller
|
// 2. Create controller
|
||||||
|
@ -94,7 +94,7 @@ func (s *Server) Launch() error {
|
||||||
// 2. Bind socket to listen
|
// 2. Bind socket to listen
|
||||||
s.sock, err = net.Listen("tcp", url)
|
s.sock, err = net.Listen("tcp", url)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("Listen socket: %s", err)
|
return fmt.Errorf("listen: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
defer s.sock.Close()
|
defer s.sock.Close()
|
||||||
|
|
Loading…
Reference in New Issue