typescript-utility-types
Use when typeScript utility types, mapped types, and advanced type manipulation. Use when creating flexible, type-safe TypeScript code.
go-error-handling
Use when Go error handling with error wrapping, sentinel errors, and custom error types. Use when handling errors in Go applications.
Full skill instructions
Master Go's error handling patterns including error wrapping, sentinel errors, custom error types, and the errors package for robust applications.
Creating and returning errors:
package main
import (
"errors"
"fmt"
)
func divide(a, b float64) (float64, error) {
if b == 0 {
return 0, errors.New("division by zero")
}
return a / b, nil
}
func main() {
result, err := divide(10, 0)
if err != nil {
fmt.Println("Error:", err)
return
}
fmt.Println("Result:", result)
}
Using fmt.Errorf:
func processFile(filename string) error {
if filename == "" {
return fmt.Errorf("filename cannot be empty")
}
// Process file...
return nil
}
Wrapping errors with context (Go 1.13+):
import (
"errors"
"fmt"
"os"
)
func readConfig(path string) error {
_, err := os.Open(path)
if err != nil {
return fmt.Errorf("failed to read config: %w", err)
}
return nil
}
func main() {
err := readConfig("config.json")
if err != nil {
fmt.Println(err)
// Output: failed to read config: open config.json: no such file
}
}
Unwrapping errors:
func handleError(err error) {
// Unwrap one level
unwrapped := errors.Unwrap(err)
if unwrapped != nil {
fmt.Println("Unwrapped:", unwrapped)
}
// Check if specific error is in chain
if errors.Is(err, os.ErrNotExist) {
fmt.Println("File does not exist")
}
}
Defining and using sentinel errors:
package main
import (
"errors"
"fmt"
)
var (
ErrNotFound = errors.New("resource not found")
ErrUnauthorized = errors.New("unauthorized access")
ErrInvalidInput = errors.New("invalid input")
)
func getUser(id int) (string, error) {
if id < 0 {
return "", ErrInvalidInput
}
if id == 0 {
return "", ErrNotFound
}
return fmt.Sprintf("user-%d", id), nil
}
func main() {
_, err := getUser(0)
if errors.Is(err, ErrNotFound) {
fmt.Println("User not found")
}
}
Implementing error interface:
type ValidationError struct {
Field string
Message string
}
func (e *ValidationError) Error() string {
return fmt.Sprintf("validation error on %s: %s", e.Field, e.Message)
}
func validateAge(age int) error {
if age < 0 {
return &ValidationError{
Field: "age",
Message: "must be positive",
}
}
if age > 150 {
return &ValidationError{
Field: "age",
Message: "must be less than 150",
}
}
return nil
}
func main() {
err := validateAge(-5)
if err != nil {
fmt.Println(err)
}
}
Type assertions with errors.As:
func handleValidation(err error) {
var validationErr *ValidationError
if errors.As(err, &validationErr) {
fmt.Printf("Field '%s' failed: %s\n",
validationErr.Field,
validationErr.Message,
)
}
}
Collecting multiple errors:
type MultiError struct {
Errors []error
}
func (m *MultiError) Error() string {
if len(m.Errors) == 0 {
return "no errors"
}
if len(m.Errors) == 1 {
return m.Errors[0].Error()
}
return fmt.Sprintf("%d errors occurred: %v", len(m.Errors), m.Errors)
}
func (m *MultiError) Add(err error) {
if err != nil {
m.Errors = append(m.Errors, err)
}
}
func validateUser(name, email string, age int) error {
errs := &MultiError{}
if name == "" {
errs.Add(errors.New("name is required"))
}
if email == "" {
errs.Add(errors.New("email is required"))
}
if age < 0 {
errs.Add(errors.New("age must be positive"))
}
if len(errs.Errors) > 0 {
return errs
}
return nil
}
When to use panic:
// Panic for unrecoverable errors
func mustConnect(dsn string) *DB {
db, err := connect(dsn)
if err != nil {
panic(fmt.Sprintf("failed to connect to database: %v", err))
}
return db
}
// Recover from panics
func safeExecute(fn func()) (err error) {
defer func() {
if r := recover(); r != nil {
err = fmt.Errorf("panic recovered: %v", r)
}
}()
fn()
return nil
}
Early return pattern:
func processRequest(id int) error {
user, err := fetchUser(id)
if err != nil {
return fmt.Errorf("fetch user: %w", err)
}
if err := validateUser(user); err != nil {
return fmt.Errorf("validate user: %w", err)
}
if err := saveUser(user); err != nil {
return fmt.Errorf("save user: %w", err)
}
return nil
}
Error variable naming:
// Good: specific error names
errDB := connectDB()
if errDB != nil {
return fmt.Errorf("db connection: %w", errDB)
}
errCache := connectCache()
if errCache != nil {
return fmt.Errorf("cache connection: %w", errCache)
}
// Avoid: reusing 'err' everywhere makes debugging harder
Using github.com/pkg/errors:
import (
"github.com/pkg/errors"
)
func loadConfig() error {
_, err := os.Open("config.json")
if err != nil {
return errors.Wrap(err, "failed to load config")
}
return nil
}
func init() {
if err := loadConfig(); err != nil {
// Print stack trace
fmt.Printf("%+v\n", err)
}
}
Structured error logging:
import (
"log/slog"
)
func processOrder(orderID string) error {
order, err := fetchOrder(orderID)
if err != nil {
slog.Error("failed to fetch order",
"orderID", orderID,
"error", err,
)
return fmt.Errorf("fetch order %s: %w", orderID, err)
}
if err := validateOrder(order); err != nil {
slog.Warn("order validation failed",
"orderID", orderID,
"error", err,
)
return fmt.Errorf("validate order: %w", err)
}
return nil
}
Handling HTTP errors:
import (
"encoding/json"
"net/http"
)
type APIError struct {
Code int `json:"code"`
Message string `json:"message"`
}
func (e *APIError) Error() string {
return e.Message
}
func writeError(w http.ResponseWriter, err error) {
var apiErr *APIError
if errors.As(err, &apiErr) {
w.WriteHeader(apiErr.Code)
json.NewEncoder(w).Encode(apiErr)
return
}
// Default error
w.WriteHeader(http.StatusInternalServerError)
json.NewEncoder(w).Encode(APIError{
Code: http.StatusInternalServerError,
Message: "Internal server error",
})
}
func handler(w http.ResponseWriter, r *http.Request) {
err := processRequest(r)
if err != nil {
writeError(w, err)
return
}
w.WriteHeader(http.StatusOK)
}
Adding context to errors:
type ContextError struct {
Op string // Operation
Path string // File path, URL, etc.
Err error // Underlying error
}
func (e *ContextError) Error() string {
return fmt.Sprintf("%s %s: %v", e.Op, e.Path, e.Err)
}
func (e *ContextError) Unwrap() error {
return e.Err
}
func readFile(path string) error {
_, err := os.ReadFile(path)
if err != nil {
return &ContextError{
Op: "read",
Path: path,
Err: err,
}
}
return nil
}
Testing error conditions:
package main
import (
"errors"
"testing"
)
func TestDivideByZero(t *testing.T) {
_, err := divide(10, 0)
if err == nil {
t.Fatal("expected error, got nil")
}
expected := "division by zero"
if err.Error() != expected {
t.Errorf("expected %q, got %q", expected, err.Error())
}
}
func TestErrorWrapping(t *testing.T) {
err := readConfig("missing.json")
if err == nil {
t.Fatal("expected error")
}
if !errors.Is(err, os.ErrNotExist) {
t.Error("expected wrapped ErrNotExist")
}
}
func TestCustomError(t *testing.T) {
err := validateAge(-1)
var validationErr *ValidationError
if !errors.As(err, &validationErr) {
t.Fatal("expected ValidationError")
}
if validationErr.Field != "age" {
t.Errorf("expected field 'age', got %q", validationErr.Field)
}
}
Use go-error-handling when you need to:
Use when typeScript utility types, mapped types, and advanced type manipulation. Use when creating flexible, type-safe TypeScript code.
Use when writing shell scripts that need to run across different systems, shells, or environments. Covers POSIX compatibility and platform differences.
Use when optimizing Tailwind CSS for production, reducing bundle size, and improving performance. Covers PurgeCSS, JIT mode, and build optimization.
Generate production-ready code from a Figma component or frame using the Figma MCP server
Use when integrating Dialyzer into development workflows and CI/CD pipelines for Erlang/Elixir projects.
Use when ruboCop configuration including YAML config, departments, inheritance, and customization.
Use when Elixir OTP patterns including GenServer, Supervisor, Agent, and Task. Use when building concurrent, fault-tolerant Elixir applications.
Use when creating custom Ameba rules for Crystal code analysis including rule development, AST traversal, issue reporting, and rule testing.
Use when swift's optional handling patterns including optional binding, chaining, nil coalescing, and modern approaches to safely working with optional values while avoiding common pitfalls and force unwrapping.
Use when annotating code with structured metadata, tags, and markers for AI-assisted development workflows. Covers annotation formats, semantic tags, and integration with development tools.
Use when scala collections including immutable/mutable variants, List, Vector, Set, Map operations, collection transformations, lazy evaluation with views, parallel collections, and custom collection builders for efficient data processing.
Use when creating custom React Flow nodes, edges, and handles. Covers custom node components, resizable nodes, toolbars, and advanced customization.
Generate, edit, and beat-sync AI video with leading models in one workspace.
The world's fastest calendar for remote work
Transform Your Design with AI Designer by ImgCreator.ai
Revolutionizing Video Production with AI-Powered Creativity
Extend an image past the frame and let AI fill the new aspect ratio.
Discover your celebrity doppelgänger with StarByFace!
ChainClarity explains 700+ crypto whitepapers in plain English, with layered summaries, comparisons, research tools, alerts, and a $4.99 Pro plan.
Opus.ai: Revolutionize Your Web Experience