basic-errors

command
v1.13.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 2, 2026 License: Apache-2.0 Imports: 14 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

This sample demonstrates error handling with the core/status package.

The pattern: classify a failure once, where its meaning is known, with status.Errorf and a sentinel (PublicErrorf when the message is safe to show a client); add context with fmt.Errorf and %w, which keeps the classification; branch with errors.Is, never on message text. At the HTTP boundary the status picks the response code, and only public messages reach the client.

  • cookbookFlow produces classified errors and lets them propagate.
  • improviseFlow consumes them, recovering from each with errors.Is.
  • leakyFlow fails unclassified, so you can watch the boundary redact it.

Run it:

go run .

Or with the Dev UI, to call the flows from a browser and read a trace of every run at http://localhost:4000/traces:

curl -sL cli.genkit.dev | bash    # install the Genkit CLI, once
genkit start -- go run .

Or over HTTP, where the status becomes the response code:

# 400, public message: dish must not be empty.
curl -X POST http://localhost:8080/cookbookFlow \
  -H "Content-Type: application/json" -d '{"data": {"dish": ""}}'

# 404, public message: no recipe for "lasagna" in the cookbook.
curl -X POST http://localhost:8080/cookbookFlow \
  -H "Content-Type: application/json" -d '{"data": {"dish": "lasagna"}}'

# 200: the same miss, recovered from by improvising a recipe.
curl -X POST http://localhost:8080/improviseFlow \
  -H "Content-Type: application/json" -d '{"data": {"dish": "lasagna"}}'

# 500, generic message: the real text and its fake credentials stay in
# the server log and never reach the client.
curl -X POST http://localhost:8080/leakyFlow \
  -H "Content-Type: application/json" -d '{"data": null}'

Both recipe flows stream. Asking for the stream changes where a failure lands, since the response has answered 200 before the flow runs: the status and the public message arrive in the body rather than on the status line.

# 200, with {"error":{"status":"INVALID_ARGUMENT", ...}} in the stream.
curl -N -X POST 'http://localhost:8080/cookbookFlow?stream=true' \
  -H "Content-Type: application/json" -d '{"data": {"dish": ""}}'

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL