Curriculum Errors & packaging errors.Is and errors.As exercise 1 · mcq
errors.Is and errors.As
Theme 5.1 exercise 9 introduced errors.Is for matching a
WRAPPED error against a sentinel. This exercise recaps the
CONTRACT.
Given:
``go
var ErrNotFound = errors.New("not found")
err := fmt.Errorf("loading user: %w", ErrNotFound)
`
Which expression correctly checks whether err` represents an
"ErrNotFound" condition (possibly wrapped one or more layers)?
TypeScript reference
About this theme
Inspecting errors without breaking the wrap chain. errors.Is(err, target) walks the wrap chain looking for a match against a sentinel. errors.As(err, &target) walks looking for a type match, populating target if found. This is the modern, type-safe replacement for instanceof checks on errors.