Skip to content

PanicsWithError does not report error message for complex errors #1399

@olivergondza

Description

@olivergondza

When PanicsWithError fails with a message different from the expected one, the message might not indicate very well what the error was, and what the string mismatch is exactly.

It does work in simple cases:

assert.PanicsWithError("expected message", func() {
	panic(errors.New("surprising message"))
})

Fails with:

func (assert.PanicTestFunc)(0x84d480) should panic with error message:	"expected message"
        	       Panic value:	&errors.errorString{s:"surprising message"}
        	       Panic stack:	goroutine 19 [running]:

It does not indicate the actual error message in case the error message is not in the fields:

assert.PanicsWithError("expected message", func() {
	panic(errors.Join(errors.New("surprising message")))
})

Fails with:

func (assert.PanicTestFunc)(0x84d4e0) should panic with error message:	"expected message"
           		Panic value:	&errors.joinError{errs:[]error{(*errors.errorString)(0xc000119570)}}
            		Panic stack:	goroutine 19 [running]:

A real world example here is https://pkg.go.dev/text/template#ExecError.


Since the assertion expects this to be an error and it checks on the error message only, it would suffice to replace the listing of entire "Panic value" with panicErr.Error().

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions