-
Notifications
You must be signed in to change notification settings - Fork 202
Open
Description
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)
}Metadata
Metadata
Assignees
Labels
No labels