Skip to content

errors.Is() behavior with oops errors #87

@thejoeejoee

Description

@thejoeejoee

Hi! At first, thanks for this great package, really simplifies error building.

I've noticed some weird behavior when using errors.Is with oops errors.
Currently, oopsError.Is(oopsError) returns false for the same error instance, which is counter-intuitive. I would expect that any error should be considered to be "Is" itself, similar to how the standard errors package works.

Demonstration test case:

package main

import (
	"errors"
	"testing"

	"github.com/samber/oops"
	"github.com/stretchr/testify/assert"
)

var MyErr1 = errors.New("my custom error")

var MyOopsErr = oops.New("my oops error")

func TestErrIs(t *testing.T) {
	// is fine
	assert.ErrorIs(t, MyErr1, MyErr1, "Standard error should be Is itself")

	// fails
	identity := errors.Is(MyOopsErr, MyOopsErr)
	assert.True(t, identity, "Oops error should be Is itself")

	// also fails
	oopsed, _ := oops.AsOops(MyOopsErr)
	assert.True(t, oopsed.Is(MyOopsErr), "Converted Oops error should be Is itself")

	// fails
	assert.ErrorIs(t, MyOopsErr, MyOopsErr, "errors.Is should work with Oops errors")
}

It's a bit inconvenient especially during tests, where testify/assert.ErrorIs is reporting a failure for oops errors.

Is this intentional behavior?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions