fix: use fmt.Errorf() %w verb for error wrapping

This commit is contained in:
Adrien Marquès 2021-06-15 22:13:36 +02:00
parent d092eaed01
commit 9e69b66289
Signed by: xdrm-brackets
GPG Key ID: D75243CA236D825E
8 changed files with 22 additions and 39 deletions

View File

@ -50,12 +50,12 @@ func buildClient(s net.Conn, ctl ControllerSet, serverCh serverChannelSet) (*cli
// 4. Write into socket
_, err := res.Send(s)
if err != nil {
return nil, fmt.Errorf("Upgrade write error: %s", err)
return nil, fmt.Errorf("upgrade write: %w", err)
}
if res.GetStatusCode() != 101 {
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

View File

@ -38,7 +38,7 @@ func ReadHeader(b []byte) (*Header, error) {
parts := bytes.Split(b, []byte(": "))
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

View File

@ -67,7 +67,7 @@ func (r *Line) extractHttpMethod(b []byte) error {
// case "DELETE": r.method = DELETE
default:
return fmt.Errorf("Invalid HTTP method '%s', expected 'GET'", b)
return fmt.Errorf("invalid HTTP method '%s', expected 'GET'", b)
}
return nil
@ -98,7 +98,7 @@ func (r *Line) extractHttpVersion(b []byte) error {
extractor := regexp.MustCompile(`^HTTP/([1-9])(?:\.([0-9]))?$`)
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

View File

@ -49,7 +49,7 @@ func Parse(r io.Reader) (request *Request, err error) {
// 1. Get chunk reader
cr := reader.NewReader(r)
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
@ -139,7 +139,7 @@ func (r *Request) parseHeader(b []byte) error {
head, err := ReadHeader(b)
if err != nil {
r.code = BadRequest
return fmt.Errorf("Error parsing header: %s", err)
return fmt.Errorf("parse header: %w", err)
}
// 2. Manage header

View File

@ -6,23 +6,6 @@ import (
"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) {
socket := &bytes.Buffer{}

View File

@ -44,7 +44,7 @@ func FromString(s string) (*Scheme, error) {
// 1. Manage '/' at the start
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 '/'
@ -102,7 +102,7 @@ func (s Scheme) GetMatch(n uint8) ([]string, error) {
// 1. Index out of range
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)
@ -125,7 +125,7 @@ func (s Scheme) GetMatch(n uint8) ([]string, error) {
}
// 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)
}

View File

@ -13,25 +13,25 @@ func (c constErr) Error() string { return string(c) }
const (
// ErrUnmaskedFrame error
ErrUnmaskedFrame = constErr("Received unmasked frame")
ErrUnmaskedFrame constErr = "Received unmasked frame"
// 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 = constErr("Received invalid fragmentation")
ErrInvalidFragment constErr = "Received invalid fragmentation"
// ErrUnexpectedContinuation error
ErrUnexpectedContinuation = constErr("Received unexpected continuation frame")
ErrUnexpectedContinuation constErr = "Received unexpected continuation frame"
// ErrInvalidSize error
ErrInvalidSize = constErr("Received invalid payload size")
ErrInvalidSize constErr = "Received invalid payload size"
// ErrInvalidPayload error
ErrInvalidPayload = constErr("Received invalid utf8 payload")
ErrInvalidPayload constErr = "Received invalid utf8 payload"
// ErrInvalidCloseStatus error
ErrInvalidCloseStatus = constErr("Received invalid close status")
ErrInvalidCloseStatus constErr = "Received invalid close status"
// ErrInvalidOpCode error
ErrInvalidOpCode = constErr("Received invalid OpCode")
ErrInvalidOpCode constErr = "Received invalid OpCode"
// ErrReservedBits error
ErrReservedBits = constErr("Received reserved bits")
ErrReservedBits constErr = "Received reserved bits"
// ErrCloseFrame error
ErrCloseFrame = constErr("Received close Frame")
ErrCloseFrame constErr = "Received close Frame"
)
// Maximum Header Size = Final/OpCode + isMask/Length + Length + Mask

View File

@ -68,7 +68,7 @@ func (s *Server) Bind(uriStr string, f ControllerFunc) error {
// 1. Build URI parser
uriScheme, err := uri.FromString(uriStr)
if err != nil {
return fmt.Errorf("Cannot build URI: %s", err)
return fmt.Errorf("cannot build URI: %w", err)
}
// 2. Create controller
@ -94,7 +94,7 @@ func (s *Server) Launch() error {
// 2. Bind socket to listen
s.sock, err = net.Listen("tcp", url)
if err != nil {
return fmt.Errorf("Listen socket: %s", err)
return fmt.Errorf("listen: %w", err)
}
defer s.sock.Close()