{"modulePath":"github.com/origadmin/runtime","version":"v0.2.20","isLatest":true,"isStandardLibrary":false,"goos":"all","goarch":"all","docs":"# package errors\n\nPackage errors provides enhanced error handling with module support, error codes, and metadata.\n\nThis package extends the standard error handling with additional features and is divided into two main parts:\n\n1\\. API interface unified error handling (integration with Kratos errors) 2. Internal module error creation and handling\n\nThe package also provides utilities for working with error metadata and compatibility with the standard errors package.\n\nBasic Usage:\n\n\t// Create a new structured error for internal module use\n\terr := errors.New(\"auth\", \"INVALID_TOKEN\", \"invalid authentication token\")\n\n\t// Add operation context\n\terr = err.WithCaller() // Automatically gets the caller function name\n\t// or\n\terr = err.WithOperation(\"CheckToken\") // Manually set operation name\n\n\t// Add metadata\n\terr = err.WithField(\"user_id\", 123)\n\n\t// Wrap an existing error\n\terr = errors.Wrap(err, \"db\", \"QUERY_FAILED\", \"failed to query user\")\n\n\t// Convert to Kratos error for API responses\n\tkratosErr := errors.ToKratos(err, commonv1.ErrorReason_UNAUTHENTICATED)\n\n\t// Check error type\n\tif errors.Is(err, \u0026errors.Structured{Module: \"auth\", Code: \"INVALID_TOKEN\"}) {\n\t\t// Handle invalid token\n\t}\n\n\t// Working with error metadata\n\tif val, ok := errors.LookupMeta(kratosErr, \"user_id\"); ok {\n\t\t// Use the metadata\n\t}\n\nPackage errors provides a centralized hub for handling, converting, and rendering errors. It uses a proto-defined enum for standard error reasons and adapts the Kratos error package.\n\nPackage errors contains generated code by adptool.\n\nPackage errors provides a centralized hub for handling, converting, and rendering errors. It provides custom error handlers/encoders for HTTP and gRPC that integrate with the Kratos ecosystem while providing centralized logging and error conversion.\n\nPackage errors provides a centralized hub for handling, converting, and rendering errors.\n\n## Constants\n\n```go\nconst (\n\tSupportPackageIsVersion1 = kerrors.SupportPackageIsVersion1\n\tUnknownCode              = kerrors.UnknownCode\n\tUnknownReason            = kerrors.UnknownReason\n)\n```\n\n## Variables\n\n```go\nvar (\n\tE_Code                   = kerrors.E_Code\n\tE_DefaultCode            = kerrors.E_DefaultCode\n\tFile_errors_errors_proto = kerrors.File_errors_errors_proto\n)\n```\n\n## Functions\n\n```go\nfunc As(err error, target any) bool\n```\n\n```go\nfunc BadRequest(reason, message string) *kerrors.Error\n```\n\n```go\nfunc ClientClosed(reason, message string) *kerrors.Error\n```\n\n```go\nfunc Clone(err *kerrors.Error) *kerrors.Error\n```\n\n```go\nfunc Code(err error) int\n```\n\n```go\nfunc Conflict(reason, message string) *kerrors.Error\n```\n\n```go\nfunc Convert(err error) *kerrors.Error\n```\nConvert takes any standard Go error and converts it into a structured Kratos error.\n\n```go\nfunc Errorf(code int, reason, format string, a ...any) error\n```\n\n```go\nfunc Forbidden(reason, message string) *kerrors.Error\n```\n\n```go\nfunc FromError(err error) *kerrors.Error\n```\n\n```go\nfunc FromReason(reason commonv1.ErrorReason) *kerrors.Error\n```\nFromReason creates a Kratos error from a predefined error reason from the .proto file. This is the primary and consistent way to create standard application errors.\n\n```go\nfunc GatewayTimeout(reason, message string) *kerrors.Error\n```\n\n```go\nfunc InternalServer(reason, message string) *kerrors.Error\n```\n\n```go\nfunc Is(err, target error) bool\n```\n\n```go\nfunc IsBadRequest(err error) bool\n```\n\n```go\nfunc IsClientClosed(err error) bool\n```\n\n```go\nfunc IsConflict(err error) bool\n```\n\n```go\nfunc IsForbidden(err error) bool\n```\n\n```go\nfunc IsGatewayTimeout(err error) bool\n```\n\n```go\nfunc IsInternalServer(err error) bool\n```\n\n```go\nfunc IsNotFound(err error) bool\n```\n\n```go\nfunc IsServiceUnavailable(err error) bool\n```\n\n```go\nfunc IsUnauthorized(err error) bool\n```\n\n```go\nfunc LookupMeta(err error, key string) (string, bool)\n```\nLookupMeta retrieves a specific metadata value from a Kratos error. It returns the value and a boolean indicating if the key was found.\n\n```go\nfunc MethodNotAllowed(reason, message string) *kerrors.Error\n```\nMethodNotAllowed creates a 405 Method Not Allowed error.\n\n```go\nfunc New(code int, reason, message string) *kerrors.Error\n```\n\n```go\nfunc NewErrorEncoder() transhttp.EncodeErrorFunc\n```\nNewErrorEncoder returns a new transhttp.EncodeErrorFunc for centralized logging and error conversion.\n\n```go\nfunc NewMessage(reason commonv1.ErrorReason, format string, a ...interface{}) *kerrors.Error\n```\nNewMessage creates a Kratos error from a predefined error reason, with a formatted message.\n\n```go\nfunc NewMessageWithMeta(reason commonv1.ErrorReason, metadata map[string]string, format string, a ...interface{}) *kerrors.Error\n```\nNewMessageWithMeta creates a Kratos error from a predefined error reason, with a formatted message and specified metadata.\n\n```go\nfunc Newf(code int, reason, format string, a ...any) *kerrors.Error\n```\n\n```go\nfunc NotFound(reason, message string) *kerrors.Error\n```\n\n```go\nfunc Reason(err error) string\n```\n\n```go\nfunc RequestTimeout(reason, message string) *kerrors.Error\n```\nRequestTimeout creates a 408 Request Timeout error.\n\n```go\nfunc ServiceUnavailable(reason, message string) *kerrors.Error\n```\n\n```go\nfunc ToKratos(e *Structured, reason commonv1.ErrorReason) *kerrors.Error\n```\nToKratos converts an internal Structured error to a Kratos error for API responses. This function resides in a dedicated API adapter to keep internal errors decoupled from API logic.\n\n```go\nfunc TooManyRequests(reason, message string) *kerrors.Error\n```\nTooManyRequests creates a 429 Too Many Requests error.\n\n```go\nfunc Unauthorized(reason, message string) *kerrors.Error\n```\n\n```go\nfunc Unwrap(err error) error\n```\n\n```go\nfunc WithAudience(err error, audience string) *kerrors.Error\n```\nWithAudience adds the 'audience' metadata to an error. Example: WithAudience(err, \"user\"), WithAudience(err, \"developer\"), WithAudience(err, \"operator\")\n\n```go\nfunc WithBusinessDomain(err error, domain string) *kerrors.Error\n```\nWithBusinessDomain adds the 'business\\_domain' metadata to an error. Example: WithBusinessDomain(err, \"user\\_management\"), WithBusinessDomain(err, \"order\\_processing\")\n\n```go\nfunc WithErrorOrigin(err error, origin string) *kerrors.Error\n```\nWithErrorOrigin adds the 'error\\_origin' metadata to an error. Example: WithErrorOrigin(err, \"database\"), WithErrorOrigin(err, \"network\")\n\n```go\nfunc WithField(err error, key string, value interface{}) *kerrors.Error\n```\nWithField adds a single key-value pair as metadata to an error. It converts the error to a Kratos error first, then adds the field.\n\n```go\nfunc WithImpact(err error, impact string) *kerrors.Error\n```\nWithImpact adds the 'impact' metadata to an error. Example: WithImpact(err, \"fatal\"), WithImpact(err, \"critical\"), WithImpact(err, \"minor\")\n\n```go\nfunc WithMessage(err error, format string, args ...interface{}) *kerrors.Error\n```\nWithMessage enhances an error with a more specific message. It converts the error to a Kratos error first, then sets the message.\n\n```go\nfunc WithMeta(err error, meta map[string]interface{}) *kerrors.Error\n```\nWithMeta adds structured metadata to an error. It converts the error to a Kratos error first, then adds the metadata.\n\n```go\nfunc WithReason(err error, reason commonv1.ErrorReason) error\n```\nWithReason tags a generic error with a specific ErrorReason. This allows Convert to map it to a specific Kratos error type.\n\n```go\nfunc WithRecoverability(err error, status string) *kerrors.Error\n```\nWithRecoverability adds the 'recoverability' metadata to an error. Example: WithRecoverability(err, \"retriable\"), WithRecoverability(err, \"non\\_retriable\")\n\n```go\nfunc WrapAndConvert(originalErr error, reason commonv1.ErrorReason, format string, a ...interface{}) *kerrors.Error\n```\nWrapAndConvert wraps an original error with a reason, converts it to a Kratos error, and sets a formatted message.\n\n## Types\n\n```go\ntype Error = kerrors.Error\n```\n\n```go\ntype Status = kerrors.Status\n```\n\n```go\ntype Structured struct {\n\t// Module indicates which module the error occurred in (e.g., \"auth\", \"db\")\n\tModule string `json:\"module\"`\n\t// Message is the human-readable error message\n\tMessage string `json:\"message\"`\n\t// Op is the operation that caused the error (usually the function name)\n\tOp string `json:\"operation,omitempty\"`\n\t// Metadata contains additional context about the error\n\tMetadata map[string]string `json:\"metadata,omitempty\"`\n\t// Err is the underlying error\n\tErr error `json:\"-\"`\n}\n```\nStructured represents an error with module context and structured metadata. This type is primarily used for internal module error handling.\n\n```go\nfunc NewStructured(module, format string, args ...interface{}) *Structured\n```\nNewStructured creates a new structured error\n\n```go\nfunc WrapStructured(err error, module, format string, args ...interface{}) *Structured\n```\nWrapStructured wraps an error with additional context\n\n```go\nfunc (e *Structured) Error() string\n```\nError returns the error message\n\n```go\nfunc (e *Structured) Is(target error) bool\n```\nIs checks if the target error is of type \\*Structured and matches based on module\n\n```go\nfunc (e *Structured) Unwrap() error\n```\nUnwrap returns the underlying error\n\n```go\nfunc (e *Structured) WithCaller() *Structured\n```\nWithCaller adds operation context to the error by getting the caller function name\n\n```go\nfunc (e *Structured) WithField(key string, value string) *Structured\n```\nWithField adds a single key-value pair to the error metadata\n\n```go\nfunc (e *Structured) WithMetadata(metadata map[string]string) *Structured\n```\nWithMetadata adds metadata to the error\n\n```go\nfunc (e *Structured) WithOperation(op string) *Structured\n```\nWithOperation sets a specific operation name for the error\n\n```go\nfunc (e *Structured) Wrap(err error) *Structured\n```\n\n```go\ntype TaggedError struct {\n\tErr    error\n\tReason commonv1.ErrorReason\n}\n```\nTaggedError is an error that carries a specific ErrorReason. This allows for explicit mapping of generic errors to predefined reasons.\n\n```go\nfunc (e *TaggedError) Error() string\n```\n\n```go\nfunc (e *TaggedError) Unwrap() error\n```\n\n","path":"github.com/origadmin/runtime/errors","name":"errors","synopsis":"Package errors provides enhanced error handling with module support, error codes, and metadata.","isRedistributable":true}
