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": ""}}'