Skip to main content

Go Error Handling Patterns & Custom Errors

Write idiomatic Go error handling: custom error types, wrapping with %w, sentinel errors, and errors.Is/errors.As inspection patterns done right.

Füllen Sie die Platzhalter aus

Edit the values, then copy your finished prompt.

Ihr Prompt
prompt.txt

                                

What this prompt does

This prompt designs a complete, idiomatic Go error-handling system for an application rather than scattered ad-hoc checks. You set [app_type] and the AI builds custom error types across your [error_categories], error wrapping with fmt.Errorf and the %w verb for context propagation, sentinel errors for [sentinel_cases], and proper errors.Is() and errors.As() inspection patterns. Getting sentinel errors, %w wrapping, and errors.As mapping right is the difference between a debuggable Go service and an opaque one that swallows the information you need at 3 a.m.

The variables shape the system to your specific app. [error_categories] defines the taxonomy of validation, not found, unauthorized, conflict, internal, and timeout, which directly drives the HTTP status-code mapping, while [sentinel_cases] names the exact sentinel errors your domain needs callers to branch on. [logger] sets structured error logging with request ID, user ID, and operation. The prompt also covers stack-trace capture without leaking it to clients, error aggregation for batch operations, a fluent error-builder pattern, and explicit anti-patterns like error-string matching and swallowed errors.

When to use it

  • Designing error handling for a new Go API or microservice from the ground up
  • Mapping error categories cleanly to HTTP status codes and safe client messages
  • Standardizing sentinel errors and %w wrapping consistently across a codebase
  • Adding structured error logging that carries request and user context for debugging
  • Aggregating multiple errors cleanly from batch or bulk operations
  • Auditing existing code against well-known Go error-handling anti-patterns

Example output

You get a set of custom error types organized by your [error_categories], wrapping helpers using %w, declared sentinel errors for [sentinel_cases], and clear errors.Is/errors.As inspection examples. It includes an error-to-HTTP mapping, stack-trace capture kept strictly internal, structured logging with context fields via [logger], batch-error aggregation, a fluent error-builder API, unit tests for the error types and wrapping, and a list of anti-patterns to avoid with corrected examples drawn from real scenarios. The HTTP mapping translates each category in [error_categories] to the right status code, so clients get a consistent and predictable error contract.

Pro tips

  • Set [error_categories] to match how your API really responds; the HTTP status mapping depends entirely on it
  • Name [sentinel_cases] after real domain conditions so callers can branch on them cleanly with errors.Is
  • Keep [logger] consistent with the rest of your stack; slog is a solid, dependency-free structured default
  • Use %w wrapping to add context, but avoid over-wrapping; each layer should add genuinely new information
  • Never expose captured stack traces to clients; log them internally and return clean, category-appropriate messages
  • Use the error-aggregation pattern for batch operations so one failed item does not silently mask the others in the run
  • Apply errors.As for typed inspection rather than string matching, which the prompt rightly flags as an anti-pattern

Frequently Asked Questions

What is the difference between errors.Is and errors.As?
errors.Is checks whether an error matches a specific sentinel value anywhere in its wrap chain, while errors.As extracts a concrete error type so you can read its fields. The prompt uses both: errors.Is for sentinel branching and errors.As for typed inspection and HTTP mapping.
Why is the %w verb important when wrapping errors?
The %w verb in fmt.Errorf wraps an error while preserving the original, so errors.Is and errors.As can still inspect it through the chain. Without %w you lose that inspectability, which is why the prompt insists on it for context propagation rather than plain string formatting.
Will it help me avoid common Go error anti-patterns?
Yes. The prompt explicitly calls out anti-patterns like error-string matching, swallowed errors, and over-wrapping, and shows corrected examples. These are real sources of opaque, hard-to-debug services, so the anti-pattern section is one of the more valuable parts of the output.
How are stack traces kept from leaking to clients?
The prompt captures stack traces for internal debugging and logs them with context, while returning clean, category-appropriate messages to clients. This gives you full diagnostic detail in your logs without exposing internal implementation details that could aid attackers or confuse API consumers.
Engr Mejba Ahmed

Need this built for real?

Engr Mejba Ahmed

AI Developer · Software Engineer

I'm Mejba — I design and ship production AI systems, automations, and full-stack apps. If you want this turned into a working solution for your team, let's talk.

Mehr in Rust & Go Prompts

Engr Mejba Ahmed

Engr Mejba Ahmed

Claude Code Expert · Online

👋

Hey there!

Quick Actions

WhatsApp Instant reply

Chat on WhatsApp

+880 1723 741224 · Instant reply

Popular Questions

Engr Mejba Ahmed is connected
Engr Mejba Ahmed is typing...
Engr Mejba Ahmed avatar

✉ Want me to follow up? Drop your email

Engr Mejba Ahmed avatar

📞 Connect Directly

Choose how you'd like to reach me

WhatsApp

+880 1723 741224

Email

[email protected]

✓ Details sent! I'll get back to you shortly.

Powered by OpenAI

335+

Blog Posts

25

AI Courses

63

Projects

Services & Expertise

Pricing & Process

Learning & Resources

Connect & Support