Skip to content

"Returning Defined Errors" section outdated #39

Description

@zolia

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)
}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions