Skip to content

[Proposal] No way to check the cause if using a mix of go-errors and standard errors #12

Open
@erizocosmico

Description

@erizocosmico

Let's imagine we are using a mix of this library and standard errors created with errors.New or fmt.Errorf.

Consider the following snippet:

var errStd = stderrors.New("something")

var errKind = errors.NewKind("something: %s")

var myErr = errKind.Wrap(errStd, "foo")

If we want to check if myErr has errStd as its cause, we need to do the following:

 err, ok := myErr.(*errors.Error)
if !ok {
  // handle
}
if err.Cause() == errStd {
}

This is potentially more cumbersome if the original error is deeply nested.
Perhaps we could have a function IsCausedBy(err, cause).

In this case, it would become:

if errors.Is(myErr, errStd) {
  // do something
}

Implementation could look like this:

func Is(err, cause error) bool {
    e, ok := err.(*Error)
    if !ok {
        return false
    }
    
    if e, ok := e.cause.(*Error); ok {
        return Is(e, cause)
    }

    return e.cause == cause
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions