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

Err is nil when filetype is unknown #43

Closed
taylorkline opened this issue Aug 3, 2018 · 2 comments
Closed

Err is nil when filetype is unknown #43

taylorkline opened this issue Aug 3, 2018 · 2 comments

Comments

@taylorkline
Copy link

Expected behavior:

kind, err := filetype.MatchReader(badReader)

We would expect err != nil, but this is not so.

The documentation appears to contradict itself. In one example, we have:

  kind, unknown := filetype.Match(buf)
  if unknown != nil {
    fmt.Printf("Unknown: %s", unknown)
    return
  }

Yet, in another example, we have:

  // Try to match the file
  fooFile := []byte{0x01, 0x02}
  kind, _ := filetype.Match(fooFile)
  if kind == filetype.Unknown {
    fmt.Println("Unknown file type")
  } else {
    fmt.Printf("File type matched: %s\n", kind.Extension)
  }

I would expect the documentation to be consistent and the error to be populated if we have an unknown filetype.

kmanley pushed a commit to kmanley/filetype that referenced this issue Feb 9, 2019
@kmanley
Copy link
Contributor

kmanley commented Feb 9, 2019

The error isn't populated if the filetype is unknown. In that case filetype.Unknown is returned and err==nil. An error is only returned if an error occurs. You're correct that this example is wrong though:

kind, unknown := filetype.Match(buf)
  if unknown != nil {
    fmt.Printf("Unknown: %s", unknown)
    return
  }

I submitted #59 to correct that

h2non added a commit that referenced this issue Feb 9, 2019
@h2non
Copy link
Owner

h2non commented Feb 9, 2019

Should be fixed now. Thanks!

@h2non h2non closed this as completed Feb 9, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants