go-errors / errors

errors with stacktraces for go
https://godoc.org/github.com/go-errors/errors
MIT License
935 stars 93 forks source link

Enable wrapping errors while prefixing the error message. #2

Closed adrienkohlbecker closed 9 years ago

adrienkohlbecker commented 9 years ago

Hello,

First of all, thanks for this very useful library. Here is a small improvement that I would find useful : It adds a method to wrap errors while providing context (as a prefix to the error message). The use case would be to add meaningful context (in english) to users that may not have access to the source-code (in my case a CLI tool)

Here is an example use case:

func ReadFile() *errors.Error {
  return errors.Errorf("File does not exist")
}

func ConquerTheWorld() *errors.Error {
  err := ReadFile()
  if err != nil {
    return errors.WrapPrefix(err, "Unable to read file", 1)
  }
}

err := ConquerTheWorld()
err.Error() == "Unable to read file: File does not exist"

The wrapped error has the same properties than with Wrap (keeps the stack trace etc) Let me know what you think. Best,

ConradIrwin commented 9 years ago

This looks great, thanks!