Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return specific error types #26

Open
cjmarkham opened this issue Nov 22, 2023 · 0 comments
Open

Return specific error types #26

cjmarkham opened this issue Nov 22, 2023 · 0 comments

Comments

@cjmarkham
Copy link
Contributor

Given the following section of code in the GetAst method:

if err != nil {
	return nil, fmt.Errorf("could not parse filter:%w", err)
} else if err := astNode.checkValid(); err != nil {
	return nil, fmt.Errorf("error validating filter:%w", err)
} else {
	return astNode, nil
}

It returns an error using fmt.Errorf. This makes it hard to determine the type of error (either parsing or validation error).

If it were replaced with specific error types, we could handle these errors in the calling functions.

For example:

type ParsingErr struct {
	err error
}
func (pe ParsingErr) Error() string {
	return pe.err.Error()
}
type ValidationErr struct {
	err error
}
func (ve ValidationErr) Error() string {
	return ve.err.Error()
}

func CouldNotParse(err error) ParsingErr {
	return ParsingErr{
		err: fmt.Errorf("could not parse filter: %w", err),
	}
}

func CouldNotValidate(err error) ValidationErr {
	return ValidationErr{
		err: fmt.Errorf("error validating filter:%w", err),
	}
}

And then the caller could use something like

ast, err = epsearchastv3.GetAst(ast)
switch {
case errors.As(err, &epsearchastv3.ParsingErr{}):
	// Handle validation err
case err != nil:
	// handle other err
}
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

No branches or pull requests

1 participant