What difference between errors.Wrap and errors.WithMessage - go

github.com/pkg/errors
This is Wrap(err error, msg string) https://pkg.go.dev/github.com/pkg/errors#Wrap
This is WithMessage(err error, msg string) https://pkg.go.dev/github.com/pkg/errors#WithMessage
both of these functions implement the interface causer https://pkg.go.dev/github.com/pkg/errors#Cause
This code works with errors.WithMessage, although I thought I should use errors.Wrap for errors.Cause
func main() {
err := errors.New("first")
err1 := errors.WithMessage(err, "tmp")
err2 := errors.WithMessage(err1, "tmp")
err3 := errors.WithMessage(err2, "tmp")
fmt.Printf("%s", errors.Cause(err3))
}
Output: first

The difference is that Wrap includes a call stack. If you click on a function name in the Go documentation, it will take you to the source, and you can see that both these functions are quite short and simple, and there is only one difference; while WithMessage creates a new error with the given message and cause (the error you pass in), Wrap takes this error and wraps it again like so:
&withStack{
err,
callers(),
}
Here err is the new error it just created (exactly like what you'd get from WithMessage) and callers populates a call stack. This is indicated in the documentation (emphasis mine):
Wrap returns an error annotating err with a stack trace at the point Wrap is called
Which is not mentioned in WithMessage as it's not done by that function.

Related

Formatting errors that wrap multiple errors

In Go 1.20, we'll be able to wrap multiple errors. The following is a simple example where two errors, err1 and err2, each wrap two lower level errors:
package main
import (
"errors"
"fmt"
)
func main() {
err1_1 := errors.New("err1.1")
err1_2 := errors.New("err1.2")
err1 := fmt.Errorf("err1: %w", errors.Join(err1_1, err1_2))
err2_1 := errors.New("err2.1")
err2_2 := errors.New("err2.2")
err2 := fmt.Errorf("err2: %w", errors.Join(err2_1, err2_2))
fmt.Printf("%s\n", errors.Join(err1, err2))
}
The output from the above looks as follows:
err1: err1.1
err1.2
err2: err2.1
err2.2
I have to admit that I am not a fan of how this looks because there's no indication that err1.2 is an inner error of err1 and similar for err2.2 with respect to err2. The following is one option that I find would make it clearer:
err1: err1.1
err1.2
err2: err2.1
err2.2
So my question is, do we have any error formatting options in Go 1.20 that I have overlooked? I have come across this proposal related to error formatting for Go2, but I assume we won't see that any time soon.
Edit
As far as I can see, unwrapping the errors myself is not a reliable option because errors may be constructed in different ways. For example, an error may be wrapped as follows:
type CustomError struct {
someValue string
wrappedErr error
}
func (err *CustomError) Error() string {
return fmt.Sprintf("custom error %s: %s", someValue, wrappedErr )
}
func (err *CustomError) Unwrap() error {
return err.wrappedErr
}
Now consider the situation where wrappedErr is a joined error. I can see that it's possible to unwrap CustomError and detect that wrappedErr is a joined/multi error by checking if it implements interface { Unwrap() []error }. I think the issue is when one tries to reconstruct the error message. I can't see how one can get the text from CustomError.Error() while replacing the value of wrapperErr to a custom formatted joined error. Furthermore, while a joined error may have been constructed using errors.Join it could also have been constructed with a custom type that implements interface { Unwrap() []error } where again the formatting of the error message is an unknown.
I guess this issue is the whole purpose of the proposal that I linked above.

Go errors: Is() and As() claim to be recursive, is there any type that implements the error interface and supports this recursion - bug free?

Everywhere I look, the "way" to "wrap" errors in Go is to use fmt.Errof with the %w verb
https://go.dev/blog/go1.13-errors
However, fmt.Errorf does not recursively wrap errors. There is no way to use it to wrap three previously defined errors (Err1, Err2, and Err3) and then check the result by using Is() and get true for each those three errors.
FINAL EDIT:
Thanks to #mkopriva's answer and comments below it, I now have a straightforward way to implement this (although, I am still curious if there is some standard type which does this). In the absence of an example, my attempts at creating one failed. The piece I was missing was adding an Is and As method to my type. Because the custom type needs to contain an error and a pointer to the next error, the custom Is and As methods allows us to compare the error contained in the custom type, rather than the custom type itself.
Here is a working example: https://go.dev/play/p/6BYGgIb728k
Highlights from the above link
type errorChain struct {
err error
next *errorChain
}
//These two functions were the missing ingredient
//Defined this way allows for full functionality even if
//The wrapped errors are also chains or other custom types
func (c errorChain) Is(err error) bool { return errors.Is(c.err, err) }
func (c errorChain) As(target any) bool { return errors.As(c.err, target) }
//Omitting Error and Unwrap methods for brevity
func Wrap(errs ...error) error {
out := errorChain{err: errs[0]}
n := &out
for _, err := range errs[1:] {
n.next = &errorChain{err: err}
n = n.next
}
return out
}
var Err0 = errors.New("error 0")
var Err1 = errors.New("error 1")
var Err2 = errors.New("error 2")
var Err3 = errors.New("error 3")
func main() {
//Check basic Is functionality
errs := Wrap(Err1, Err2, Err3)
fmt.Println(errs) //error 1: error 2: error 3
fmt.Println(errors.Is(errs, Err0)) //false
fmt.Println(errors.Is(errs, Err2)) //true
}
While the Go source specifically mentions the ability to define an Is method, the example does not implement it in a way that can solve my issue and the discussion do not make it immediately clear that it would be needed to utilize the recursive nature of errors.Is.
AND NOW BACK TO THE ORIGINAL POST:
Is there something built into Go where this does work?
I played around with making one of my own (several attempts), but ran into undesirable issues. These issues stem from the fact that errors in Go appear to be compared by address. i.e. if Err1 and Err2 point to the same thing, they are the same.
This causes me issues. I can naively get errors.Is and errors.As to work recursively with a custom error type. It is straightforward.
Make a type that implements the error interface (has an Error() string method)
The type must have a member that represents the wrapped error which is a pointer to its own type.
Implement an Unwrap() error method that returns the wrapped error.
Implement some method which wraps one error with another
It seems good. But there is trouble.
Since errors are pointers, if I make something like myWrappedError = Wrap(Err1, Err2) (in this case assume Err1 is being wrapped by Err2). Not only will errors.Is(myWrappedError, Err1) and errors.Is(myWrappedError, Err2) return true, but so will errors.Is(Err2, Err1)
Should the need arise to make myOtherWrappedError = Wrap(Err3, Err2) and later call errors.Is(myWrappedError, Err1) it will now return false! Making myOtherWrappedError changes myWrappedError.
I tried several approaches, but always ran into related issues.
Is this possible? Is there a Go library which does this?
NOTE: I am more interested in the presumably already existing right way to do this rather than the specific thing that is wrong with my basic attempt
Edit 3: As suggested by one of the answers, the issue in my first code is obviously that I modify global errors. I am aware, but failed to adequately communicate. Below, I will include other broken code which uses no pointers and modifies no globals.
Edit 4: slight modification to make it work more, but it is still broken
See https://go.dev/play/p/bSytCysbujX
type errorGroup struct {
err error
wrappedErr error
}
//...implemention Unwrap and Error excluded for brevity
func Wrap(inside error, outside error) error {
return &errorGroup{outside, inside}
}
var Err1 = errorGroup{errors.New("error 1"), nil}
var Err2 = errorGroup{errors.New("error 2"), nil}
var Err3 = errorGroup{errors.New("error 3"), nil}
func main() {
errs := Wrap(Err1, Err2)
errs = Wrap(errs, Err3)
fmt.Println(errs)//error 3: error 2: error 1
fmt.Println(errors.Is(errs, Err1)) //true
fmt.Println(errors.Is(errs, Err2)) //false <--- a bigger problem
fmt.Println(errors.Is(errs, Err3)) //false <--- a bigger problem
}
Edit 2: playground version shortened
See https://go.dev/play/p/swFPajbMcXA for an example of this.
EDIT 1: A trimmed version of my code focusing on the important parts:
type errorGroup struct {
err error
wrappedErr *errorGroup
}
//...implemention Unwrap and Error excluded for brevity
func Wrap(errs ...*errorGroup) (r *errorGroup) {
r = &errorGroup{}
for _, err := range errs {
err.wrappedErr = r
r = err
}
return
}
var Err0 = &errorGroup{errors.New("error 0"), nil}
var Err1 = &errorGroup{errors.New("error 1"), nil}
var Err2 = &errorGroup{errors.New("error 2"), nil}
var Err3 = &errorGroup{errors.New("error 3"), nil}
func main() {
errs := Wrap(Err1, Err2, Err3)//error 3: error 2: error 1
fmt.Println(errors.Is(errs, Err1)) //true
//Creating another wrapped error using the Err1, Err2, or Err3 breaks the previous wrap, errs.
_ = Wrap(Err0, Err2, Err3)
fmt.Println(errors.Is(errs, Err1)) //false <--- the problem
}
You can use something like this:
type errorChain struct {
err error
next *errorChain
}
func Wrap(errs ...error) error {
out := errorChain{err: errs[0]}
n := &out
for _, err := range errs[1:] {
n.next = &errorChain{err: err}
n = n.next
}
return out
}
func (c errorChain) Is(err error) bool {
return c.err == err
}
func (c errorChain) Unwrap() error {
if c.next != nil {
return c.next
}
return nil
}
https://go.dev/play/p/6oUGefSxhvF
Your code modifies package-global error values, so it is inherently broken. This defect has nothing to do with Go's error handling mechanics.
Per the documentation you linked, there are two error-handling helpers: Is, and As. Is lets you recursively unwrap an error, looking for a specific error value, which is necessarily a package global for this to be useful. As, on the other hand, lets you recursively unwrap an error looking for any wrapped error value of a given type.
How does wrapping work? You wrap error A in a new error value B. A Wrap() helper would necessarily return a new value, as fmt.Errorf does in the examples in the linked documentation. A Wrap helper should never modify the value of the error being wrapped. That value should be considered immutable. In fact, in any normal implementation, the value would be of type error, so that you can wrap any error, rather than just wrapping concentric values of your custom error type in each other; and, in that case, you have no access to the fields of the wrapped error to modify them anyway. Essentially, Wrap should be roughly:
func Wrap(err error) error {
return &errGroup{err}
}
And that's it. That's not very useful, because your implementation of errGroup doesn't really do anything - it provides no details about the error that occurred, it's just a container for other errors. For it to have value, it should have a string error message, or methods like some other error types' IsNotFound, or something that makes it more useful than just using error and fmt.Errorf.
Based on the usage in your example code, it also looks like you're presuming the use case is to say "I want to wrap A in B in C", which I've never seen in the wild and I cannot think of any scenario where that would be needed. The purpose of wrapping is to say "I've recieved error A, I'm going to wrap it in error B to add context, and return it". The caller might wrap that error in error C, and so on, which is what makes recursive wrapping valuable.
For example: https://go.dev/play/p/XeoONx19dgX
Instead of chaining/wrapping, you will "soon" (Go 1.20, as seen in Go 1.20-rc1 in Dec. 2022) be able to return a slice/tree of errors.
(In the meantime, mdobak/go-xerrors is a good alternative)
The release note explains:
Wrapping multiple errors
Go 1.20 expands support for error wrapping to permit an error to wrap
multiple other errors.
An error e can wrap more than one error by providing an Unwrap method
that returns a []error.
The errors.Is and errors.As functions have been updated to inspect
multiply wrapped errors.
The fmt.Errorf function now supports multiple occurrences of the %w
format verb, which will cause it to return an error that wraps all of
those error operands.
The new function errors.Join returns an error wrapping a list of
errors.
That comes from:
proposal: errors: add support for wrapping multiple errors
Background
Since Go 1.13, an error may wrap another by providing an Unwrap method returning the wrapped error.
The errors.Is and errors.As functions operate on chains of wrapped errors.
A common request is for a way to combine a list of errors into a single error.
Proposal
An error wraps multiple errors if its type has the method
Unwrap() []error
Reusing the name Unwrap avoids ambiguity with the existing singular Unwrap method.
Returning a 0-length list from Unwrap means the error doesn't wrap anything.
Callers must not modify the list returned by Unwrap.
The list returned by Unwrap must not contain any nil errors.
We replace the term "error chain" with "error tree".
The errors.Is and errors.As functions are updated to unwrap multiple errors.
Is reports a match if any error in the tree matches.
As finds the first matching error in a inorder preorder traversal of the tree.
The errors.Join function provides a simple implementation of a multierr.
It does not flatten errors.
// Join returns an error that wraps the given errors.
// Any nil error values are discarded.
// The error formats as the text of the given errors, separated by newlines.
// Join returns nil if errs contains no non-nil values.
func Join(errs ...error) error
The fmt.Errorf function permits multiple instances of the %w formatting verb.
The errors.Unwrap function is unaffected: It returns nil when called on an error with an Unwrap() []error method.
Why should this be in the standard library?
This proposal adds something which cannot be provided outside the standard library: Direct support for error trees in errors.Is and errors.As.
Existing combining errors operate by providing Is and As methods which inspect the contained errors, requiring each implementation to duplicate this logic, possibly in incompatible ways.
This is best handled in errors.Is and errors.As, for the same reason those functions handle singular unwrapping.
In addition, this proposal provides a common method for the ecosystem to use to represent combined errors, permitting interoperation between third-party implementations.
So far (Sept. 2022) this proposal seems a likely accept has been accepted!
CL 432575 starts the implementation.
There arr several approaches but there is one thing that you should keep in mind: if you have multiple errors, you may need to handle it as a slice of errors
For instance, imagine you need to check if all errors are the same, or there is at least one error of certain type you can use the snippet below.
You can extend this concept or use some existing library to handle multierrors
type Errors []error
func (errs Errors) String() string {
…
}
func (errs Errors) Any(target error) bool{
for _, err := range errs {
if errors.Is(err,target) {
return true
}
}
return false
}
func (errs Errors) All(target error) bool{
if len(errs) == 0 { return false }
for _, err := range errs {
if !errors.Is(err,target) {
return false
}
}
return true
}

Why a nil error returned from strings.Builder WriteString in golang, is it necessary?

When reviewed my colleague's code, I found that a returned err has been ignored, though we would not do that in general:
b := new(strings.Builder)
b.WriteString("Hello, World!") // ignore err
The source code for WriteString declares it may return an error, but in fact it never will (always returning nil for the error value):
// WriteString appends the contents of s to b's buffer.
// It returns the length of s and a nil error.
func (b *Builder) WriteString(s string) (int, error) {
b.copyCheck()
b.buf = append(b.buf, s...)
return len(s), nil
}
What would the issues be, if any, with removing the error return, as follows?
func (b *Builder) WriteString(s string) int {
b.copyCheck()
b.buf = append(b.buf, s...)
return len(s)
}
The changelist which introduces strings.Builder includes a lot of comments about trying to make this API similar to bytes.Buffer.
For instance,
That's how a bytes.Buffer behaves, after all, and we're supposed to be a subset of a bytes.Buffer.
Looking at the documentation for some bytes.Buffer functions, it mentions
WriteRune appends the UTF-8 encoding of Unicode code point r to the buffer, returning its length and an error, which is always nil but is included to match bufio.Writer's WriteRune.
It looks like they're basically trying to design an API that's similar to other interfaces in Golang's standard library. Even though the always-nil error is redundant, it allows the Builder to match existing interfaces that would accept bytes.Buffer or bufio.Writer. One such interface is io.StringWriter, which looks like
type StringWriter interface {
WriteString(s string) (n int, err error)
}
The err return value here is useful since other StringWriter implementations could possibly return errors.
Go, it's quite common to return a value and error. So you can check the error is not null, if no error then easily use the returned value.
In other words, if it receives an error from a function then it indicates there was a problem with the function called.

Determining what is returned from a go function

Given this function:
func (c *Firehose) PutRecord(input *PutRecordInput) (*PutRecordOutput, error) {
req, out := c.PutRecordRequest(input)
return out, req.Send()
}
I found that this invocation works:
err, _ := svc.PutRecord(putRecordInput)
However I'm still not very clear on what this means in function signature:
(*PutRecordOutput, error)
My question is, can I always determine what is returned from a function by what is specified in the return line, which in this case is:
return out, req.Send()
This part of the function signature is exactly what the function returns.
(*PutRecordOutput, error)
So this one will return a pointer to a PutRecordOutput plus an error (which by convention is returned as nil if no error occurred).
If you look at the source code for the function, return statements will have to be consistent with that, so that can also help you understand how the return values are built.
But, please note that in some cases you could have named output arguments, like:
(output *PutRecordOutput, err error)
In that case, output and err will be valid local variables inside the function, and you might see a plain return statement like this:
return
Just keep in mind that one would implicitly be like doing:
return output, err

Golang: panic: runtime error: invalid memory address or nil pointer dereference

When uploading a file to my go app, I encounter a panic.
panic: runtime error: invalid memory address or nil pointer dereference
/Users/bob/Projects/go/src/github.com/zenazn/goji/web/middleware/recoverer.go:24 (0xbaf5b)
func.006: debug.PrintStack()
/usr/local/go/src/pkg/runtime/panic.c:248 (0x1043d)
panic: runtime·newstackcall(d->fn, (byte*)d->args, d->siz);
/usr/local/go/src/pkg/runtime/panic.c:552 (0x10eed)
panicstring: runtime·panic(err);
/usr/local/go/src/pkg/runtime/os_darwin.c:454 (0xfb8e)
sigpanic: runtime·panicstring("invalid memory address or nil pointer dereference");
/usr/local/go/src/pkg/mime/multipart/multipart.go:223 (0xb6801)
(*Reader).NextPart: if r.currentPart != nil {
/Users/bob/Projects/go/src/github.com/app/controllers/company_sheet_controller.go:32 (0x2ee18)
NewCompanySheet: part, err := mr.NextPart()
/usr/local/go/src/pkg/net/http/server.go:1235 (0x44f00)
HandlerFunc.ServeHTTP: f(w, r)
/Users/bob/Projects/go/src/github.com/zenazn/goji/web/router.go:113 (0x6bc0a)
This method handles an upload from a multipart form, extracting the file contents and boundary data. The r.FormFile method on request is used to set file and header. And in order to pull the additional data from the post, I use r.MultipartReader. From the error description I see r is already declared as ParseMultipartForm when using r.FormFile. When executing the function with the different request methods individually, I receive no errors. r.FormFile and r.MultipartReader work fine isolated. Am I unable to mix the two request methods?
func Upload(r *http.Request) {
file, header, err := r.FormFile("file")
ErrorCheck(err)
mr, err := r.MultipartReader()
ErrorCheck(err)
part, err := mr.NextPart()
ErrorCheck(err)
var b bytes.Buffer
io.CopyN(&b, part, int64(1<<20))
fmt.Println(b.String())
defer file.Close()
}
You are calling FormFile() at the beginning of your function.
This calls ParseMultipartForm() (see Request.FormFile) which populates the MultipartForm field of your http.Request.
Now the documentation for MultipartReader() states that you should use MultipartReader() instead of ParseMultipartForm() if you want to process the data as stream.
Looking at the source MultipartReader() returns an error if the MultipartForm field was already set.
So to answer your question: No, you can't use both functions for the same request.
Also your
defer file.Close()
should be right after you checked for an error from FormFile(), otherwise the file won't be closed before Garbage Collection, when your function panics.

Resources