Error handling

Translating errors at boundaries

A boundary can map internal errors to stable domain errors without leaking implementation details. Preserve the cause only when intended as part of the caller-facing contract; do not discard unexpected errors silently. Example output: 'true false'.

Formal Syntax & Structure

go syntaxgo
if errors.Is(err, internalError) { return publicError }

Working Example

Example Snippetgo
package main
import "fmt"import "errors"
var ErrUnavailable = errors.New("unavailable")var errPrivate = errors.New("internal failure")
func lookup() error {  err := errPrivate  if errors.Is(err, errPrivate) {    return ErrUnavailable  }  return err}func main() {  e := lookup()  fmt.Println(errors.Is(e, ErrUnavailable), errors.Is(e, errPrivate))}
Go Reference