A comprehensive and flexible error handling package designed for Go applications that need structured, traceable, and contextual error management. This package provides a robust foundation for building reliable services with proper error propagation, logging, and debugging capabilities.
App Error is built around the principle that effective error handling requires more than just error messages. Modern applications need structured errors that carry context, support tracing, provide HTTP status mappings, and enable proper debugging workflows. This package delivers all of these capabilities while maintaining simplicity and performance.
- Hierarchical Error Structure: Combines actual errors with custom error definitions and metadata
- Error Code Management: Support for primary error codes and error code chains
- HTTP Status Integration: Automatic mapping between errors and appropriate HTTP status codes
- Flexible Metadata: Attach arbitrary data to errors for debugging and logging purposes
- Trace Logging: Automatic trace collection as errors propagate through your application
- Context Integration: Seamless integration with Go's context package for request tracing
- Error Evolution Tracking: Monitor how errors change as they move through different layers
- Predefined Error Types: Create reusable error definitions with codes, messages, and retry policies
- Dynamic Error Creation: Build errors on-the-fly when predefined types aren't sufficient
- Retry Policy Support: Mark errors as retryable or non-retryable for client guidance
- Method Chaining: Fluent interface for building complex error structures
- Immutable Operations: Safe error manipulation without side effects
- Type Safety: Strong typing throughout the API to prevent common mistakes
The central error type that encapsulates all error information including the underlying error, custom error details, error code chains, HTTP status codes, and additional metadata.
Represents predefined error templates with error codes, human-readable messages, and retry policies. These serve as blueprints for creating consistent error responses across your application.
Manages error tracing information including trace logs, error evolution history, and identifier mappings for debugging and monitoring purposes.
Add the package to your Go module:
go get github.com/piyushkumar96/app-error
Import the package in your Go files:
import ae "github.com/piyushkumar96/app-error"
GetCustomErr Function Creates a new custom error definition with the specified error code, message, and retry policy. This function is typically used to define error constants that can be reused throughout your application.
Parameters:
code
: Unique error identifier (string)message
: Human-readable error description (string)retryable
: Whether the error condition can be retried (boolean)
Returns a CustomErr pointer that can be used with GetAppErr.
GetAppErr Function Constructs a complete application error by combining an actual error with custom error details, HTTP status code, and optional metadata.
Parameters:
ctx
: Request context for tracing (context.Context)err
: The underlying error that occurred (error)customErr
: Custom error definition (*CustomErr)httpCode
: Appropriate HTTP status code (int)meta
: Optional metadata (variadic interface{})
Returns an AppError pointer with all error information properly structured.
Error Retrieval Methods
Error()
: Returns the underlying error message (implements error interface)GetErr()
: Retrieves the actual underlying errorGetMsg()
: Returns the custom error messageGetErrCode()
: Gets the primary error codeGetErrCodes()
: Returns all error codes in the chainGetHTTPCode()
: Retrieves the HTTP status codeGetData()
: Accesses attached metadata
Error Modification Methods
SetErr(error)
: Updates the underlying errorSetMsg(string)
: Modifies the custom error messageSetErrCode(string)
: Changes the primary error codeSetHTTPCode(int)
: Updates the HTTP status codeSetData(interface{})
: Attaches or updates metadataAddErrCode(string)
: Appends an error code to the chain
All modification methods return the AppError instance to enable method chaining.
AddTraceLog Function Adds error information to the trace log stored in the request context. This function automatically captures error details for debugging and monitoring purposes.
Parameters:
ctx
: Request context containing trace information (context.Context)errorMsg
: Error message to add to the trace (string)
Returns the updated TraceMeta or nil if context is invalid.
Start with a simple error and enhance it with custom error details and appropriate HTTP status codes. This pattern is suitable for straightforward error scenarios where you need structured error information.
Begin with a general error and progressively add more specific error codes as your understanding of the problem improves. This pattern is excellent for debugging complex issues and providing detailed error context.
Define common errors as package-level constants to ensure consistency across your application. This approach reduces duplication and provides a centralized error catalog.
Include additional debugging information such as request parameters, system state, or configuration details that can help with troubleshooting.
Use the retry policy feature to guide client behavior, especially in distributed systems where temporary failures are common.
- Use consistent prefixes for different service layers or modules
- Include severity levels in error codes when appropriate
- Maintain a centralized registry of error codes to prevent conflicts
- Use semantic versioning principles for error code evolution
- Write clear, actionable error messages for end users
- Include technical details in metadata rather than user-facing messages
- Use consistent language and terminology across all error messages
- Avoid exposing internal implementation details in error messages
- Follow HTTP status code specifications strictly
- Use 4xx codes for client errors and 5xx codes for server errors
- Be consistent in status code usage across similar error types
- Consider the impact on API consumers when choosing status codes
- Always pass context through your error handling chain
- Use context cancellation to prevent resource leaks in error scenarios
- Store request-specific information in context for error tracing
- Avoid storing large objects in context to prevent memory issues
- Include only essential debugging information in metadata
- Use structured data formats for complex metadata
- Consider the performance impact of large metadata objects
- Sanitize sensitive information before including in metadata
- Test error scenarios as thoroughly as success cases
- Verify error code consistency across different code paths
- Validate HTTP status code mappings in integration tests
- Mock external dependencies to test error handling paths
The package is designed to minimize memory allocations during error creation and manipulation. Error objects are lightweight and metadata is stored efficiently.
All operations on error objects are safe for concurrent access. Context-based tracing handles concurrent requests appropriately without race conditions.
Error tracing adds minimal overhead to request processing. Trace data is collected efficiently and stored in a compact format.
Structure your application in layers with each layer adding appropriate context to errors as they propagate upward. Lower layers focus on technical details while upper layers add business context.
Use the retry policy feature to integrate with circuit breakers and other reliability patterns. Mark errors as retryable or non-retryable based on the underlying failure type.
Design error responses that allow clients to degrade gracefully when certain features are unavailable. Use appropriate HTTP status codes and error messages to guide client behavior.
Leverage error codes and metadata for monitoring system health. Set up alerting based on error code patterns and frequencies.
We welcome contributions to improve this package. Please follow these guidelines:
- Ensure Go 1.20 or later is installed
- Run tests before submitting changes
- Follow Go coding conventions and best practices
- Add appropriate documentation for new features
- Write unit tests for all new functionality
- Ensure existing tests pass after changes
- Add integration tests for complex features
- Include edge case testing for error scenarios
- Update this README for significant changes
- Add inline code documentation for new functions
- Include usage examples for new features
- Update API documentation as needed
This project is licensed under the terms specified in the LICENSE file. Please review the license before using this package in your projects.
For questions, bug reports, or feature requests, please use the appropriate channels provided by your organization. We value your feedback and contributions to making this package better.
This package follows semantic versioning principles. Check the version tags for compatibility information and upgrade guidance.
Significant changes and new features are documented in version releases. Review the changelog when upgrading to understand breaking changes and new capabilities.