Skip to content

assert.NotErrorAs: new assertion - #1137

Open
titusjaka wants to merge 1 commit into
stretchr:masterfrom
titusjaka:feature/noterroras
Open

titusjaka wants to merge 1 commit into
stretchr:masterfrom
titusjaka:feature/noterroras

Conversation

@titusjaka

Copy link
Copy Markdown

Summary

Add NotErrorAs methods to check that none of the errors in err's chain matches target.

Changes

  • add NotErrorAs and NotErrorAsf assertions to assert and require packages

Motivation

Sometimes, it's essential to check that error does not match an exact type.

func TestExample(t *testing.T) {
	err := errors.New("test error")

	var notFoundErr ErrNotFound
	assert.NotErrorAs(t, err, &notFoundErr)

	err = WrapNotFoundError(err)
	assert.ErrorAs(t, err, &notFoundErr)
}

Related issues

Closes #1066

@johanneswuerbach

Copy link
Copy Markdown

I was just searching for this function, to discover it is missing.

Maybe @boyan-soubachov could have a look? :-)

@Antonboom

Copy link
Copy Markdown

+1 for this PR.
and take a look to #1129

@dolmen dolmen added pkg-assert Change related to package testify/assert pkg-require Change related to package testify/require type-error Issue related to comparing values implementing the error interface labels Mar 7, 2024
@dolmen dolmen changed the title feat: Add NotErrorAs method to {assert|require} packages assert.NotErrorAs: new assertion May 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pkg-assert Change related to package testify/assert pkg-require Change related to package testify/require type-error Issue related to comparing values implementing the error interface

Projects

None yet

Development

Successfully merging this pull request may close these issues.

testify {assert|require} packages seem to be missing NotErrorAs methods.

4 participants