Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use %w to wrap errors #1175

Merged
merged 1 commit into from Dec 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 4 additions & 4 deletions brotli_test.go
Expand Up @@ -42,7 +42,7 @@ func testBrotliBytesSingleCase(s string) error {

unbrotliedS, err := AppendUnbrotliBytes(prefix, brotlipedS[len(prefix):])
if err != nil {
return fmt.Errorf("unexpected error when uncompressing %q: %s", s, err)
return fmt.Errorf("unexpected error when uncompressing %q: %w", s, err)
}
if !bytes.Equal(unbrotliedS[:len(prefix)], prefix) {
return fmt.Errorf("unexpected prefix when uncompressing %q: %q. Expecting %q", s, unbrotliedS[:len(prefix)], prefix)
Expand Down Expand Up @@ -83,17 +83,17 @@ func testBrotliCompressSingleCase(s string) error {
var buf bytes.Buffer
zw := acquireStacklessBrotliWriter(&buf, CompressDefaultCompression)
if _, err := zw.Write([]byte(s)); err != nil {
return fmt.Errorf("unexpected error: %s. s=%q", err, s)
return fmt.Errorf("unexpected error: %w. s=%q", err, s)
}
releaseStacklessBrotliWriter(zw, CompressDefaultCompression)

zr, err := acquireBrotliReader(&buf)
if err != nil {
return fmt.Errorf("unexpected error: %s. s=%q", err, s)
return fmt.Errorf("unexpected error: %w. s=%q", err, s)
}
body, err := ioutil.ReadAll(zr)
if err != nil {
return fmt.Errorf("unexpected error: %s. s=%q", err, s)
return fmt.Errorf("unexpected error: %w. s=%q", err, s)
}
if string(body) != s {
return fmt.Errorf("unexpected string after decompression: %q. Expecting %q", body, s)
Expand Down
4 changes: 2 additions & 2 deletions bytesconv.go
Expand Up @@ -98,7 +98,7 @@ func ParseIPv4(dst net.IP, ipStr []byte) (net.IP, error) {
}
v, err := ParseUint(b[:n])
if err != nil {
return dst, fmt.Errorf("cannot parse ipStr %q: %s", ipStr, err)
return dst, fmt.Errorf("cannot parse ipStr %q: %w", ipStr, err)
}
if v > 255 {
return dst, fmt.Errorf("cannot parse ipStr %q: ip part cannot exceed 255: parsed %d", ipStr, v)
Expand All @@ -108,7 +108,7 @@ func ParseIPv4(dst net.IP, ipStr []byte) (net.IP, error) {
}
v, err := ParseUint(b)
if err != nil {
return dst, fmt.Errorf("cannot parse ipStr %q: %s", ipStr, err)
return dst, fmt.Errorf("cannot parse ipStr %q: %w", ipStr, err)
}
if v > 255 {
return dst, fmt.Errorf("cannot parse ipStr %q: ip part cannot exceed 255: parsed %d", ipStr, v)
Expand Down
14 changes: 9 additions & 5 deletions client_test.go
Expand Up @@ -626,6 +626,10 @@ func TestClientHeaderCase(t *testing.T) {
}

func TestClientReadTimeout(t *testing.T) {
if runtime.GOOS == "windows" {
t.SkipNow()
}

t.Parallel()

ln := fasthttputil.NewInmemoryListener()
Expand Down Expand Up @@ -814,14 +818,14 @@ func TestClientDoWithCustomHeaders(t *testing.T) {
go func() {
conn, err := ln.Accept()
if err != nil {
ch <- fmt.Errorf("cannot accept client connection: %s", err)
ch <- fmt.Errorf("cannot accept client connection: %w", err)
return
}
br := bufio.NewReader(conn)

var req Request
if err = req.Read(br); err != nil {
ch <- fmt.Errorf("cannot read client request: %s", err)
ch <- fmt.Errorf("cannot read client request: %w", err)
return
}
if string(req.Header.Method()) != MethodPost {
Expand Down Expand Up @@ -854,11 +858,11 @@ func TestClientDoWithCustomHeaders(t *testing.T) {
var resp Response
bw := bufio.NewWriter(conn)
if err = resp.Write(bw); err != nil {
ch <- fmt.Errorf("cannot send response: %s", err)
ch <- fmt.Errorf("cannot send response: %w", err)
return
}
if err = bw.Flush(); err != nil {
ch <- fmt.Errorf("cannot flush response: %s", err)
ch <- fmt.Errorf("cannot flush response: %w", err)
return
}

Expand Down Expand Up @@ -1221,7 +1225,7 @@ func TestHostClientPendingRequests(t *testing.T) {
resp := AcquireResponse()

if err := c.DoTimeout(req, resp, 10*time.Second); err != nil {
resultCh <- fmt.Errorf("unexpected error: %s", err)
resultCh <- fmt.Errorf("unexpected error: %w", err)
return
}

Expand Down
18 changes: 9 additions & 9 deletions compress_test.go
Expand Up @@ -78,7 +78,7 @@ func testGzipBytesSingleCase(s string) error {

gunzippedS, err := AppendGunzipBytes(prefix, gzippedS[len(prefix):])
if err != nil {
return fmt.Errorf("unexpected error when uncompressing %q: %s", s, err)
return fmt.Errorf("unexpected error when uncompressing %q: %w", s, err)
}
if !bytes.Equal(gunzippedS[:len(prefix)], prefix) {
return fmt.Errorf("unexpected prefix when uncompressing %q: %q. Expecting %q", s, gunzippedS[:len(prefix)], prefix)
Expand All @@ -99,7 +99,7 @@ func testDeflateBytesSingleCase(s string) error {

inflatedS, err := AppendInflateBytes(prefix, deflatedS[len(prefix):])
if err != nil {
return fmt.Errorf("unexpected error when uncompressing %q: %s", s, err)
return fmt.Errorf("unexpected error when uncompressing %q: %w", s, err)
}
if !bytes.Equal(inflatedS[:len(prefix)], prefix) {
return fmt.Errorf("unexpected prefix when uncompressing %q: %q. Expecting %q", s, inflatedS[:len(prefix)], prefix)
Expand Down Expand Up @@ -165,17 +165,17 @@ func testGzipCompressSingleCase(s string) error {
var buf bytes.Buffer
zw := acquireStacklessGzipWriter(&buf, CompressDefaultCompression)
if _, err := zw.Write([]byte(s)); err != nil {
return fmt.Errorf("unexpected error: %s. s=%q", err, s)
return fmt.Errorf("unexpected error: %w. s=%q", err, s)
}
releaseStacklessGzipWriter(zw, CompressDefaultCompression)

zr, err := acquireGzipReader(&buf)
if err != nil {
return fmt.Errorf("unexpected error: %s. s=%q", err, s)
return fmt.Errorf("unexpected error: %w. s=%q", err, s)
}
body, err := ioutil.ReadAll(zr)
if err != nil {
return fmt.Errorf("unexpected error: %s. s=%q", err, s)
return fmt.Errorf("unexpected error: %w. s=%q", err, s)
}
if string(body) != s {
return fmt.Errorf("unexpected string after decompression: %q. Expecting %q", body, s)
Expand All @@ -188,17 +188,17 @@ func testFlateCompressSingleCase(s string) error {
var buf bytes.Buffer
zw := acquireStacklessDeflateWriter(&buf, CompressDefaultCompression)
if _, err := zw.Write([]byte(s)); err != nil {
return fmt.Errorf("unexpected error: %s. s=%q", err, s)
return fmt.Errorf("unexpected error: %w. s=%q", err, s)
}
releaseStacklessDeflateWriter(zw, CompressDefaultCompression)

zr, err := acquireFlateReader(&buf)
if err != nil {
return fmt.Errorf("unexpected error: %s. s=%q", err, s)
return fmt.Errorf("unexpected error: %w. s=%q", err, s)
}
body, err := ioutil.ReadAll(zr)
if err != nil {
return fmt.Errorf("unexpected error: %s. s=%q", err, s)
return fmt.Errorf("unexpected error: %w. s=%q", err, s)
}
if string(body) != s {
return fmt.Errorf("unexpected string after decompression: %q. Expecting %q", body, s)
Expand All @@ -213,7 +213,7 @@ func testConcurrent(concurrency int, f func() error) error {
go func(idx int) {
err := f()
if err != nil {
ch <- fmt.Errorf("error in goroutine %d: %s", idx, err)
ch <- fmt.Errorf("error in goroutine %d: %w", idx, err)
}
ch <- nil
}(i)
Expand Down
2 changes: 1 addition & 1 deletion expvarhandler/expvar.go
Expand Up @@ -58,7 +58,7 @@ func getExpvarRegexp(ctx *fasthttp.RequestCtx) (*regexp.Regexp, error) {
}
rr, err := regexp.Compile(r)
if err != nil {
return nil, fmt.Errorf("cannot parse r=%q: %s", r, err)
return nil, fmt.Errorf("cannot parse r=%q: %w", r, err)
}
return rr, nil
}
6 changes: 3 additions & 3 deletions fasthttpproxy/proxy_env.go
Expand Up @@ -49,7 +49,7 @@ func FasthttpProxyHTTPDialerTimeout(timeout time.Duration) fasthttp.DialFunc {

port, _, err := net.SplitHostPort(addr)
if err != nil {
return nil, fmt.Errorf("unexpected addr format: %v", err)
return nil, fmt.Errorf("unexpected addr format: %w", err)
}

reqURL := &url.URL{Host: addr, Scheme: httpScheme}
Expand Down Expand Up @@ -108,14 +108,14 @@ func FasthttpProxyHTTPDialerTimeout(timeout time.Duration) fasthttp.DialFunc {

if err := res.Read(bufio.NewReader(conn)); err != nil {
if connErr := conn.Close(); connErr != nil {
return nil, fmt.Errorf("conn close err %v followed by read conn err %v", connErr, err)
return nil, fmt.Errorf("conn close err %v precede by read conn err %w", connErr, err)
}
return nil, err
}
if res.Header.StatusCode() != 200 {
if connErr := conn.Close(); connErr != nil {
return nil, fmt.Errorf(
"conn close err %v followed by connect to proxy: code: %d body %s",
"conn close err %w precede by connect to proxy: code: %d body %s",
connErr, res.StatusCode(), string(res.Body()))
}
return nil, fmt.Errorf("could not connect to proxy: code: %d body %s", res.StatusCode(), string(res.Body()))
Expand Down
4 changes: 2 additions & 2 deletions fasthttputil/pipeconns_test.go
Expand Up @@ -164,7 +164,7 @@ func testPipeConnsCloseWhileReadWrite(t *testing.T) {
var err error
if _, err = io.Copy(ioutil.Discard, c1); err != nil {
if err != errConnectionClosed {
err = fmt.Errorf("unexpected error: %s", err)
err = fmt.Errorf("unexpected error: %w", err)
} else {
err = nil
}
Expand All @@ -178,7 +178,7 @@ func testPipeConnsCloseWhileReadWrite(t *testing.T) {
for {
if _, err = c2.Write([]byte("foobar")); err != nil {
if err != errConnectionClosed {
err = fmt.Errorf("unexpected error: %s", err)
err = fmt.Errorf("unexpected error: %w", err)
} else {
err = nil
}
Expand Down
22 changes: 11 additions & 11 deletions fs.go
Expand Up @@ -524,7 +524,7 @@ func (ff *fsFile) bigFileReader() (io.Reader, error) {

f, err := os.Open(ff.f.Name())
if err != nil {
return nil, fmt.Errorf("cannot open already opened file: %s", err)
return nil, fmt.Errorf("cannot open already opened file: %w", err)
}
return &bigFileReader{
f: f,
Expand Down Expand Up @@ -981,7 +981,7 @@ func (h *fsHandler) openIndexFile(ctx *RequestCtx, dirPath string, mustCompress
return ff, nil
}
if !os.IsNotExist(err) {
return nil, fmt.Errorf("cannot open file %q: %s", indexFilePath, err)
return nil, fmt.Errorf("cannot open file %q: %w", indexFilePath, err)
}
}

Expand Down Expand Up @@ -1100,7 +1100,7 @@ func (h *fsHandler) compressAndOpenFSFile(filePath string, fileEncoding string)
fileInfo, err := f.Stat()
if err != nil {
f.Close()
return nil, fmt.Errorf("cannot obtain info for file %q: %s", filePath, err)
return nil, fmt.Errorf("cannot obtain info for file %q: %w", filePath, err)
}

if fileInfo.IsDir() {
Expand Down Expand Up @@ -1146,7 +1146,7 @@ func (h *fsHandler) compressFileNolock(f *os.File, fileInfo os.FileInfo, filePat
if err != nil {
f.Close()
if !os.IsPermission(err) {
return nil, fmt.Errorf("cannot create temporary file %q: %s", tmpFilePath, err)
return nil, fmt.Errorf("cannot create temporary file %q: %w", tmpFilePath, err)
}
return nil, errNoCreatePermission
}
Expand All @@ -1168,27 +1168,27 @@ func (h *fsHandler) compressFileNolock(f *os.File, fileInfo os.FileInfo, filePat
zf.Close()
f.Close()
if err != nil {
return nil, fmt.Errorf("error when compressing file %q to %q: %s", filePath, tmpFilePath, err)
return nil, fmt.Errorf("error when compressing file %q to %q: %w", filePath, tmpFilePath, err)
}
if err = os.Chtimes(tmpFilePath, time.Now(), fileInfo.ModTime()); err != nil {
return nil, fmt.Errorf("cannot change modification time to %s for tmp file %q: %s",
fileInfo.ModTime(), tmpFilePath, err)
}
if err = os.Rename(tmpFilePath, compressedFilePath); err != nil {
return nil, fmt.Errorf("cannot move compressed file from %q to %q: %s", tmpFilePath, compressedFilePath, err)
return nil, fmt.Errorf("cannot move compressed file from %q to %q: %w", tmpFilePath, compressedFilePath, err)
}
return h.newCompressedFSFile(compressedFilePath, fileEncoding)
}

func (h *fsHandler) newCompressedFSFile(filePath string, fileEncoding string) (*fsFile, error) {
f, err := os.Open(filePath)
if err != nil {
return nil, fmt.Errorf("cannot open compressed file %q: %s", filePath, err)
return nil, fmt.Errorf("cannot open compressed file %q: %w", filePath, err)
}
fileInfo, err := f.Stat()
if err != nil {
f.Close()
return nil, fmt.Errorf("cannot obtain info for compressed file %q: %s", filePath, err)
return nil, fmt.Errorf("cannot obtain info for compressed file %q: %w", filePath, err)
}
return h.newFSFile(f, fileInfo, true, fileEncoding)
}
Expand All @@ -1210,7 +1210,7 @@ func (h *fsHandler) openFSFile(filePath string, mustCompress bool, fileEncoding
fileInfo, err := f.Stat()
if err != nil {
f.Close()
return nil, fmt.Errorf("cannot obtain info for file %q: %s", filePath, err)
return nil, fmt.Errorf("cannot obtain info for file %q: %w", filePath, err)
}

if fileInfo.IsDir() {
Expand All @@ -1226,7 +1226,7 @@ func (h *fsHandler) openFSFile(filePath string, mustCompress bool, fileEncoding
fileInfoOriginal, err := os.Stat(filePathOriginal)
if err != nil {
f.Close()
return nil, fmt.Errorf("cannot obtain info for original file %q: %s", filePathOriginal, err)
return nil, fmt.Errorf("cannot obtain info for original file %q: %w", filePathOriginal, err)
}

// Only re-create the compressed file if there was more than a second between the mod times.
Expand Down Expand Up @@ -1257,7 +1257,7 @@ func (h *fsHandler) newFSFile(f *os.File, fileInfo os.FileInfo, compressed bool,
if len(contentType) == 0 {
data, err := readFileHeader(f, compressed, fileEncoding)
if err != nil {
return nil, fmt.Errorf("cannot read header of the file %q: %s", f.Name(), err)
return nil, fmt.Errorf("cannot read header of the file %q: %w", f.Name(), err)
}
contentType = http.DetectContentType(data)
}
Expand Down
24 changes: 12 additions & 12 deletions header.go
Expand Up @@ -1797,11 +1797,11 @@ func (h *ResponseHeader) tryRead(r *bufio.Reader, n int) error {
}
}
return &ErrSmallBuffer{
error: fmt.Errorf("error when reading response headers: %s", errSmallBuffer),
error: fmt.Errorf("error when reading response headers: %w", errSmallBuffer),
}
}

return fmt.Errorf("error when reading response headers: %s", err)
return fmt.Errorf("error when reading response headers: %w", err)
}
b = mustPeekBuffered(r)
headersLen, errParse := h.parse(b)
Expand Down Expand Up @@ -1849,11 +1849,11 @@ func (h *ResponseHeader) tryReadTrailer(r *bufio.Reader, n int) error {
}
}
return &ErrSmallBuffer{
error: fmt.Errorf("error when reading response trailer: %s", errSmallBuffer),
error: fmt.Errorf("error when reading response trailer: %w", errSmallBuffer),
}
}

return fmt.Errorf("error when reading response trailer: %s", err)
return fmt.Errorf("error when reading response trailer: %w", err)
}
b = mustPeekBuffered(r)
headersLen, errParse := h.parseTrailer(b)
Expand Down Expand Up @@ -1891,9 +1891,9 @@ func headerError(typ string, err, errParse error, b []byte, secureErrorLogMessag

func headerErrorMsg(typ string, err error, b []byte, secureErrorLogMessage bool) error {
if secureErrorLogMessage {
return fmt.Errorf("error when reading %s headers: %s. Buffer size=%d", typ, err, len(b))
return fmt.Errorf("error when reading %s headers: %w. Buffer size=%d", typ, err, len(b))
}
return fmt.Errorf("error when reading %s headers: %s. Buffer size=%d, contents: %s", typ, err, len(b), bufferSnippet(b))
return fmt.Errorf("error when reading %s headers: %w. Buffer size=%d, contents: %s", typ, err, len(b), bufferSnippet(b))
}

// Read reads request header from r.
Expand Down Expand Up @@ -1958,11 +1958,11 @@ func (h *RequestHeader) tryReadTrailer(r *bufio.Reader, n int) error {
}
}
return &ErrSmallBuffer{
error: fmt.Errorf("error when reading request trailer: %s", errSmallBuffer),
error: fmt.Errorf("error when reading request trailer: %w", errSmallBuffer),
}
}

return fmt.Errorf("error when reading request trailer: %s", err)
return fmt.Errorf("error when reading request trailer: %w", err)
}
b = mustPeekBuffered(r)
headersLen, errParse := h.parseTrailer(b)
Expand Down Expand Up @@ -1991,7 +1991,7 @@ func (h *RequestHeader) tryRead(r *bufio.Reader, n int) error {
// This is for go 1.6 bug. See https://github.com/golang/go/issues/14121 .
if err == bufio.ErrBufferFull {
return &ErrSmallBuffer{
error: fmt.Errorf("error when reading request headers: %s", errSmallBuffer),
error: fmt.Errorf("error when reading request headers: %w", errSmallBuffer),
}
}

Expand All @@ -2001,7 +2001,7 @@ func (h *RequestHeader) tryRead(r *bufio.Reader, n int) error {
return ErrNothingRead{err}
}

return fmt.Errorf("error when reading request headers: %s", err)
return fmt.Errorf("error when reading request headers: %w", err)
}
b = mustPeekBuffered(r)
headersLen, errParse := h.parse(b)
Expand Down Expand Up @@ -2499,9 +2499,9 @@ func (h *ResponseHeader) parseFirstLine(buf []byte) (int, error) {
h.statusCode, n, err = parseUintBuf(b)
if err != nil {
if h.secureErrorLogMessage {
return 0, fmt.Errorf("cannot parse response status code: %s", err)
return 0, fmt.Errorf("cannot parse response status code: %w", err)
}
return 0, fmt.Errorf("cannot parse response status code: %s. Response %q", err, buf)
return 0, fmt.Errorf("cannot parse response status code: %w. Response %q", err, buf)
}
if len(b) > n && b[n] != ' ' {
if h.secureErrorLogMessage {
Expand Down