example_errors

package
v1.5.0 Latest Latest
Warning

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

Go to latest
Published: Jun 24, 2025 License: MIT Imports: 9 Imported by: 0

README

Error Handling Examples

This directory contains examples demonstrating how to use the errors package, which provides structured error types and handling with rich context information for Go applications. The package extends Go's standard error handling with features like error categorization, error wrapping with context, and error codes.

Examples

1. Basic Error Creation Example

basic_error_creation_example.go

Demonstrates how to create different types of errors.

Key concepts:

  • Creating simple errors with errors.New
  • Creating errors with codes using errors.NewWithCode
  • Creating domain-specific errors with errors.NewDomainError
  • Creating infrastructure errors with errors.NewInfrastructureError
  • Creating application errors with errors.NewApplicationError
  • Creating validation errors with errors.NewValidationError
2. Error Wrapping Example

error_wrapping_example.go

Shows how to wrap errors to add context as they propagate up the call stack.

Key concepts:

  • Wrapping errors with errors.Wrap
  • Wrapping errors with codes using errors.WrapWithCode
  • Unwrapping errors with errors.Unwrap
  • Checking error types with errors.Is
3. Error Context Example

error_context_example.go

Demonstrates how to add and retrieve context information from errors.

Key concepts:

  • Creating errors with context using errors.NewWithContext
  • Getting context from errors with errors.GetContext
  • Getting specific context values with errors.GetContextValue
  • Adding context to existing errors with errors.AddContext
4. Error Categorization Example

error_categorization_example.go

Shows how to categorize and handle different types of errors.

Key concepts:

  • Checking error categories with errors.IsNotFound, errors.IsValidation, etc.
  • Handling errors based on their category
  • Creating custom error categories
5. Stack Traces Example

stack_traces_example.go

Demonstrates how to include and retrieve stack traces from errors.

Key concepts:

  • Creating errors with stack traces using errors.NewWithStack
  • Retrieving stack traces with errors.StackTrace
  • Understanding stack trace output
6. Localized Error Messages Example

localized_messages_example.go

Shows how to create and retrieve localized error messages.

Key concepts:

  • Creating errors with localized messages using errors.NewLocalized
  • Retrieving localized messages with errors.GetLocalizedMessage
  • Using fallback languages with errors.GetLocalizedMessageWithFallback

Running the Examples

To run any of the examples, use the go run command:

go run examples/errors/basic_error_creation_example.go

Additional Resources

For more information about the errors package, see the errors package documentation.

Documentation

Overview

This example demonstrates how to create different types of errors using the errors package.

This example demonstrates how to categorize and handle different types of errors.

This example demonstrates how to add and retrieve context information from errors.

This example demonstrates how to wrap errors to add context as they propagate up the call stack.

This example demonstrates how to include and retrieve caller information from errors.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type User

type User struct {
	ID       string
	Username string
	Email    string
	Age      int
}

User represents a user in the system

type UserAPI

type UserAPI struct {
	// contains filtered or unexported fields
}

UserAPI simulates an API that uses the service and categorizes errors

func NewUserAPI

func NewUserAPI() *UserAPI

NewUserAPI creates a new UserAPI

func (*UserAPI) HandleGetUser

func (a *UserAPI) HandleGetUser(id string) (int, string)

HandleGetUser handles a user request and categorizes errors for appropriate responses

type UserCategoryService

type UserCategoryService struct {
	// contains filtered or unexported fields
}

UserCategoryService uses the repository and handles errors

func NewUserCategoryService

func NewUserCategoryService() *UserCategoryService

NewUserCategoryService creates a new UserCategoryService

func (*UserCategoryService) GetUser

func (s *UserCategoryService) GetUser(id string) (string, error)

GetUser gets a user by ID and handles different error categories

type UserRepository

type UserRepository struct{}

UserRepository simulates a user repository with potential errors

func (*UserRepository) GetUserByID

func (r *UserRepository) GetUserByID(id string) (string, error)

GetUserByID simulates getting a user by ID from a database

type UserService

type UserService struct{}

UserService is a simple service that demonstrates error context

func (*UserService) CreateUser

func (s *UserService) CreateUser(user *User) error

CreateUser creates a user and demonstrates error context propagation

func (*UserService) ValidateUser

func (s *UserService) ValidateUser(user *User) error

ValidateUser validates a user and returns an error with context if validation fails

Jump to

Keyboard shortcuts

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