Skip to content

proposal: errors: add Ignore #74264

Open
Open
@dsnet

Description

@dsnet

Proposal Details

Ignoring certain errors is a relatively common operation.

Some examples include:

var tr *tar.Reader = ...
for {
    th, err := tr.Next()
    if err := nil {
        if err == io.EOF {
            return nil
        }
        return err
    }
    ...
}
var tx *sql.Tx = ...
if err := tx.QueryRow(ctx, query, &res); err != nil {
    if errors.Is(err, sql.ErrNoRows) {
        return nil
    }
    return err
}
... // use res

I propose the addition of the following helper function:

// Ignore returns nil if err matches any error in targets,
// otherwise it returns err as is.
func Ignore(err error, targets error...) error {
    for _, target := range targets {
        if Is(err, target) {
            return nil
        }
    }
    return err
}

While this signature doesn't statically prevent accidental swapping of the arguments, it follows a similar pattern as errors.Is, which has exactly the same problem.

With this helper, the above examples could be simplified as:

var tr *tar.Reader = ...
for {
    th, err := tr.Next()
    if err := nil {
        return errors.Ignore(err, io.EOF)
    }
    ...
}
var tx *sql.Tx = ...
if err := tx.QueryRow(ctx, query, &res); err != nil {
    return errors.Ignore(err, sql.ErrNoRows)
}
... // use res

Some more evidence of the utility of this:

Metadata

Metadata

Assignees

No one assigned

    Labels

    LibraryProposalIssues describing a requested change to the Go standard library or x/ libraries, but not to a toolProposal

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions