From 9e69b6628941cb9b65f712775b77fba313c35f39 Mon Sep 17 00:00:00 2001 From: xdrm-brackets Date: Tue, 15 Jun 2021 22:13:36 +0200 Subject: [PATCH] fix: use fmt.Errorf() %w verb for error wrapping --- client.go | 4 ++-- internal/http/upgrade/header.go | 2 +- internal/http/upgrade/line.go | 4 ++-- internal/http/upgrade/request.go | 4 ++-- internal/http/upgrade/request_test.go | 17 ----------------- internal/uri/parser.go | 6 +++--- message.go | 20 ++++++++++---------- server.go | 4 ++-- 8 files changed, 22 insertions(+), 39 deletions(-) diff --git a/client.go b/client.go index b9827e4..1443557 100644 --- a/client.go +++ b/client.go @@ -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 diff --git a/internal/http/upgrade/header.go b/internal/http/upgrade/header.go index 1e5f06b..9503c0c 100644 --- a/internal/http/upgrade/header.go +++ b/internal/http/upgrade/header.go @@ -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 diff --git a/internal/http/upgrade/line.go b/internal/http/upgrade/line.go index a463220..842b4a1 100644 --- a/internal/http/upgrade/line.go +++ b/internal/http/upgrade/line.go @@ -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 diff --git a/internal/http/upgrade/request.go b/internal/http/upgrade/request.go index 1a7c0f8..45c1a5f 100644 --- a/internal/http/upgrade/request.go +++ b/internal/http/upgrade/request.go @@ -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 diff --git a/internal/http/upgrade/request_test.go b/internal/http/upgrade/request_test.go index 3d12b30..7af15fa 100644 --- a/internal/http/upgrade/request_test.go +++ b/internal/http/upgrade/request_test.go @@ -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{} diff --git a/internal/uri/parser.go b/internal/uri/parser.go index e35a4ee..e581c23 100644 --- a/internal/uri/parser.go +++ b/internal/uri/parser.go @@ -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) } diff --git a/message.go b/message.go index 9ddc43e..f00f757 100644 --- a/message.go +++ b/message.go @@ -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 diff --git a/server.go b/server.go index 9102b25..f9a43a6 100644 --- a/server.go +++ b/server.go @@ -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()