Skip to content

Instantly share code, notes, and snippets.

@mdouchement
Last active June 19, 2017 15:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mdouchement/d2527f67584bb3418aa2db518687d23d to your computer and use it in GitHub Desktop.
Save mdouchement/d2527f67584bb3418aa2db518687d23d to your computer and use it in GitHub Desktop.
Golang gzip reader does not returns the right error.
package main
import (
"bytes"
"compress/gzip"
"errors"
"fmt"
"io"
)
// When N < 10 ==> underlying error
// When N >= 10 ==> gzip: invalid header
var N = 10
func main() {
data := bytes.NewBufferString("some fake data")
r1 := newUnderlyingReader(data)
r2, err := gzip.NewReader(r1)
if err != nil {
panic(err)
}
p := make([]byte, 4096)
_, err = r2.Read(p)
fmt.Println(err)
}
type underlyingReader struct {
r io.Reader
}
func newUnderlyingReader(r io.Reader) *underlyingReader {
return &underlyingReader{r}
}
func (r *underlyingReader) Read(p []byte) (n int, err error) {
n = N
err = errors.New("underlying error")
return
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment