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

"Returning Defined Errors" section outdated #39

Open
zolia opened this issue Mar 24, 2021 · 1 comment
Open

"Returning Defined Errors" section outdated #39

zolia opened this issue Mar 24, 2021 · 1 comment

Comments

@zolia
Copy link

zolia commented Mar 24, 2021

Error defining/handling examples mostly outdated since go 1.13 as there was %w directive introduced for fmt.Errorf() function.

Something like this can be used if you want to introduce details into the error itself. No need to define yet another struct+interface for that:

import (
	"errors"
	"fmt"
)

var ErrSomeStaticError = errors.New("static error")

func main() {
	err := ReturnErrorWithDetails()
	if errors.Is(err, ErrSomeStaticError){
		fmt.Printf("found static error with details: %v\n", err)
		fmt.Printf("just static error: %v\n", errors.Unwrap(err))
	} else {
		fmt.Print("static error not found")
	}
}

func ReturnErrorWithDetails() error {
	return fmt.Errorf("some details here, item count: %d, vector name: %s: %w", 10, "infinity", ErrSomeStaticError)
}
@Pungyeon
Copy link
Owner

Hi @zolia, I agree that the article should be changed to including the above instead 🙇

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

2 participants