Skip to content

Add Is() methods for error types.#251

Merged
TomWright merged 1 commit intoTomWright:masterfrom
Eun:add-errors-is
Oct 6, 2022
Merged

Add Is() methods for error types.#251
TomWright merged 1 commit intoTomWright:masterfrom
Eun:add-errors-is

Conversation

@Eun
Copy link
Contributor

@Eun Eun commented Oct 6, 2022

Description

This pull requests adds the Is(err error) bool method to the errors.

Problem

Errors are hard to distinguish. It is not possible to follow custom logic in case of error.
Take the following code as an example:

func getName(node *dasel.Node) (string, error) {
	result, err := node.Query(".Name")
	if err != nil {
		if errors.Is(err, &dasel.ValueNotFound{}) {
			return "", errors.New("name not found")
		}
		return "", err
	}
	return result.String(), nil
}

Solution

Implement the Is method so the errors.Is() function can be used.

@TomWright
Copy link
Owner

Thanks for the contribution!

@TomWright TomWright merged commit fdb1a69 into TomWright:master Oct 6, 2022
@Eun Eun deleted the add-errors-is branch October 6, 2022 17:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants

Comments