0

When I debug in the following code, sometimes it can read data from the body correctly but with EOF error.

func (r *trailerReader) Read(b []byte) (int, error) {
    n, err := r.resp.Body.Read(b)
    if err != nil {
        if e := r.resp.Trailer.Get("X-Stream-Error"); e != "" {
            err = errors.New(e)
        }
    }
    return n, err
}

I called this method in my code:

// FilesRead read a file in a given MFS
func (s *Shell) FilesRead(ctx context.Context, path string, options ...FilesOpt) (io.ReadCloser, error) {
    rb := s.Request("files/read", path)
    for _, opt := range options {
        if err := opt(rb); err != nil {
            return nil, err
        }
    }

    resp, err := rb.Send(ctx)
    if err != nil {
        return nil, err
    }
    if resp.Error != nil {
        return nil, resp.Error
    }

    return resp.Output, nil
}

Any thoughts?

Max Peng
  • 2,879
  • 1
  • 26
  • 43

1 Answers1

0

As Stebalien said in this Github issue, it's a go's expected behavior of Reader.

Refer to the third paragraph of this documentation

When Read encounters an error or end-of-file condition after successfully reading n > 0 bytes, it returns the number of bytes read. It may return the (non-nil) error from the same call or return the error (and n == 0) from a subsequent call. An instance of this general case is that a Reader returning a non-zero number of bytes at the end of the input stream may return either err == EOF or err == nil. The next Read should return 0, EOF.

Max Peng
  • 2,879
  • 1
  • 26
  • 43