typeover
curriculum

Curriculum Errors & packaging errors.Is and errors.As exercise 2 · mcq

errors.Is and errors.As

When the error you want to handle is a CUSTOM TYPE (you want to read its fields), not a sentinel — you need to EXTRACT the concrete type out of the interface, possibly through wrap layers. That's errors.As's job. Given: ``go type PathError struct { Op, Path string; Err error } func (e *PathError) Error() string { return e.Op + " " + e.Path + ": " + e.Err.Error() } err := fmt.Errorf("loading: %w", &PathError{Op: "open", Path: "x.txt", ...}) ` Which expression correctly extracts the *PathError from err, with pe` bound to the value when matched?

TypeScript reference
Pick the idiomatic Go translation

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.