Typed error

A struct that implements error and carries fields, such as ValidationError{Field, Reason}. Callers extract it from a wrapped chain with errors.As, passing a pointer to a variable of the same type the Error method is defined on.

Minimal example

go
var ve *ValidationError
if errors.As(err, &ve) {
	log.Println(ve.Field, ve.Reason)
}

Learn this concept

Lesson 2.13 · Error classification in Go

Used in exercises

Exercise 2.14 · POST /v1/menu/price: price a configured item
Prerequisite conceptsError wrapping