Documentation
¶
Index ¶
- Constants
- func IsAppError(err error) bool
- func IsRecordNotFound(err error) bool
- type AppError
- func (e *AppError) AsAccessDenied() *AppError
- func (e *AppError) AsAuthentication() *AppError
- func (e *AppError) AsAuthenticationWithCode(code string) *AppError
- func (e *AppError) AsAuthorizationWithCode(code string) *AppError
- func (e *AppError) AsConfiguration() *AppError
- func (e *AppError) AsConflictWithCode(code string) *AppError
- func (e *AppError) AsDatabaseConnection() *AppError
- func (e *AppError) AsDatabaseConstraint() *AppError
- func (e *AppError) AsDatabaseError() *AppError
- func (e *AppError) AsDatabaseTimeout() *AppError
- func (e *AppError) AsExternalServiceUnavailable() *AppError
- func (e *AppError) AsExternalWithCode(code string) *AppError
- func (e *AppError) AsInsufficientPermissions() *AppError
- func (e *AppError) AsInternalWithCode(code string) *AppError
- func (e *AppError) AsInvalidCredentials() *AppError
- func (e *AppError) AsInvalidFormat() *AppError
- func (e *AppError) AsInvalidInput() *AppError
- func (e *AppError) AsInvalidRange() *AppError
- func (e *AppError) AsLoginRequired() *AppError
- func (e *AppError) AsNotFoundWithCode(code string) *AppError
- func (e *AppError) AsRateLimitWithCode(code string) *AppError
- func (e *AppError) AsRequiredField() *AppError
- func (e *AppError) AsResourceExists() *AppError
- func (e *AppError) AsResourceForbidden() *AppError
- func (e *AppError) AsResourceNotFound() *AppError
- func (e *AppError) AsServiceTimeout() *AppError
- func (e *AppError) AsServiceUnavailable() *AppError
- func (e *AppError) AsTimeout() *AppError
- func (e *AppError) AsTokenExpired() *AppError
- func (e *AppError) AsTokenInvalid() *AppError
- func (e *AppError) AsTooManyRequests() *AppError
- func (e *AppError) AsUnavailableWithCode(code string) *AppError
- func (e *AppError) AsValidationError() *AppError
- func (e *AppError) AsValidationWithCode(code string) *AppError
- func (e *AppError) Cause() error
- func (e *AppError) Error() string
- func (e *AppError) GetHTTPStatus() int
- func (e *AppError) GetStackTrace() string
- func (e *AppError) GetStackTraceLines() []string
- func (e *AppError) HasCode(code string) bool
- func (e *AppError) IsType(errorType ErrorType) bool
- func (e *AppError) String() string
- func (e *AppError) Unwrap() error
- func (e *AppError) UnwrapAll() error
- func (e *AppError) WithCause(cause error) *AppError
- func (e *AppError) WithCode(code string) *AppError
- func (e *AppError) WithCodeAndMessage(code, message string) *AppError
- func (e *AppError) WithDetails(details string) *AppError
- func (e *AppError) WithHTTPStatus(status int) *AppError
- func (e *AppError) WithMessage(message string) *AppError
- func (e *AppError) WithType(errorType ErrorType) *AppError
- type ErrorType
Constants ¶
const ( // Validation error codes (400) CodeValidationError = "VALIDATION_ERROR" CodeInvalidInput = "INVALID_INPUT" CodeRequiredField = "REQUIRED_FIELD" CodeInvalidFormat = "INVALID_FORMAT" CodeInvalidRange = "INVALID_RANGE" // Authentication error codes (401) CodeInvalidCredentials = "INVALID_CREDENTIALS" CodeTokenExpired = "TOKEN_EXPIRED" CodeTokenInvalid = "TOKEN_INVALID" CodeLoginRequired = "LOGIN_REQUIRED" CodeAuthRequired = "AUTH_REQUIRED" // Authorization error codes (403) CodeAccessDenied = "ACCESS_DENIED" CodeInsufficientPermissions = "INSUFFICIENT_PERMISSIONS" CodeResourceForbidden = "RESOURCE_FORBIDDEN" CodeInsufficientRole = "INSUFFICIENT_ROLE" // Resource error codes (404, 409) CodeResourceNotFound = "RESOURCE_NOT_FOUND" CodeResourceExists = "RESOURCE_EXISTS" // Rate limit error codes (429) CodeRateLimitExceeded = "RATE_LIMIT_EXCEEDED" // Internal system error codes (500) CodeInternalError = "INTERNAL_ERROR" CodeDatabaseError = "DATABASE_ERROR" CodeDatabaseConnection = "DATABASE_CONNECTION" CodeDatabaseConstraint = "DATABASE_CONSTRAINT" CodeInternalTimeout = "INTERNAL_TIMEOUT" CodeConfigurationError = "CONFIGURATION_ERROR" CodeOperationCanceled = "OPERATION_CANCELED" // Context and middleware error codes (500) CodeInvalidUserContext = "INVALID_USER_CONTEXT" CodeOrgContextMissing = "ORG_CONTEXT_MISSING" CodeInvalidOrgContext = "INVALID_ORG_CONTEXT" CodeUserRoleNotFound = "USER_ROLE_NOT_FOUND" CodePermissionCheckFailed = "PERMISSION_CHECK_FAILED" // External service error codes (502) CodeExternalError = "EXTERNAL_ERROR" CodeExternalTimeout = "EXTERNAL_TIMEOUT" CodeServiceUnavailable = "SERVICE_UNAVAILABLE" )
Error codes for application error handling.
const ( // General messages MsgUnknownError = "Unknown error occurred" // Authentication messages MsgAuthRequired = "Authentication required" MsgInvalidCredentials = "Invalid credentials provided" // Authorization messages MsgInsufficientPermissions = "Access denied - insufficient permissions" MsgInsufficientRole = "Access denied - insufficient role permissions" // Context error messages MsgInvalidUserContext = "Invalid user context" MsgOrgContextMissing = "Organization context missing" MsgInvalidOrgContext = "Invalid organization context" MsgUserRoleNotFound = "User role not found" MsgPermissionCheckFailed = "Permission check failed" )
Error messages for consistent error reporting
const (
StatusInternalServerError = http.StatusInternalServerError
)
Default HTTP status codes for error types.
Variables ¶
This section is empty.
Functions ¶
func IsRecordNotFound ¶
IsRecordNotFound checks if an error indicates that a record was not found.
Types ¶
type AppError ¶
type AppError struct {
Type ErrorType `json:"type"`
Code string `json:"code"`
Message string `json:"message"`
Details string `json:"details,omitempty"`
HTTPStatus int `json:"http_status,omitempty"`
// contains filtered or unexported fields
}
AppError represents a structured application error with HTTP mapping capabilities and enhanced error details using cockroachdb/errors.
func AsAppError ¶
AsAppError safely converts an error to AppError if possible.
func NewAppError ¶
NewAppError creates a new AppError with specified type, code, and message.
func (*AppError) AsAccessDenied ¶
AsAccessDenied converts the error to an access denied error.
func (*AppError) AsAuthentication ¶
AsAuthentication converts the error to an authentication error type.
func (*AppError) AsAuthenticationWithCode ¶
AsAuthenticationWithCode converts the error to an authentication error with a specific code.
func (*AppError) AsAuthorizationWithCode ¶
AsAuthorizationWithCode converts the error to an authorization error with a specific code.
func (*AppError) AsConfiguration ¶
AsConfiguration converts the error to a configuration error.
func (*AppError) AsConflictWithCode ¶
AsConflictWithCode converts the error to a conflict error with a specific code.
func (*AppError) AsDatabaseConnection ¶
AsDatabaseConnection converts the error to a database connection error.
func (*AppError) AsDatabaseConstraint ¶
AsDatabaseConstraint converts the error to a database constraint error.
func (*AppError) AsDatabaseError ¶
AsDatabaseError converts the error to a database error.
func (*AppError) AsDatabaseTimeout ¶
AsDatabaseTimeout converts the error to a database timeout error.
func (*AppError) AsExternalServiceUnavailable ¶
AsServiceUnavailable converts the error to a service unavailable error.
func (*AppError) AsExternalWithCode ¶
AsExternalWithCode converts the error to an external service error with a specific code.
func (*AppError) AsInsufficientPermissions ¶
AsInsufficientPermissions converts the error to an insufficient permissions error.
func (*AppError) AsInternalWithCode ¶
AsInternalWithCode converts the error to an internal error with a specific code.
func (*AppError) AsInvalidCredentials ¶
AsInvalidCredentials converts the error to an invalid credentials error.
func (*AppError) AsInvalidFormat ¶
AsInvalidFormat converts the error to an invalid format validation error.
func (*AppError) AsInvalidInput ¶
AsInvalidInput converts the error to an invalid input validation error.
func (*AppError) AsInvalidRange ¶
AsInvalidRange converts the error to an invalid range validation error.
func (*AppError) AsLoginRequired ¶
AsLoginRequired converts the error to a login required error.
func (*AppError) AsNotFoundWithCode ¶
AsNotFoundWithCode converts the error to a not found error with a specific code.
func (*AppError) AsRateLimitWithCode ¶
AsRateLimitWithCode converts the error to a rate limit error with a specific code.
func (*AppError) AsRequiredField ¶
AsRequiredField converts the error to a required field validation error.
func (*AppError) AsResourceExists ¶
AsResourceExists converts the error to a resource exists error.
func (*AppError) AsResourceForbidden ¶
AsResourceForbidden converts the error to a resource forbidden error.
func (*AppError) AsResourceNotFound ¶
AsResourceNotFound converts the error to a resource not found error.
func (*AppError) AsServiceTimeout ¶
AsServiceTimeout converts the error to a service timeout error.
func (*AppError) AsServiceUnavailable ¶
AsServiceUnavailable converts the error to a service unavailable error.
func (*AppError) AsTokenExpired ¶
AsTokenExpired converts the error to a token expired error.
func (*AppError) AsTokenInvalid ¶
AsTokenInvalid converts the error to a token invalid error.
func (*AppError) AsTooManyRequests ¶
AsTooManyRequests converts the error to a too many requests error.
func (*AppError) AsUnavailableWithCode ¶
AsUnavailableWithCode converts the error to an unavailable service error with a specific code.
func (*AppError) AsValidationError ¶
AsValidationError converts the error to a validation error with a default code.
func (*AppError) AsValidationWithCode ¶
AsValidationWithCode converts the error to a validation error with a specific code.
func (*AppError) GetHTTPStatus ¶
GetHTTPStatus returns the HTTP status code for this error.
func (*AppError) GetStackTrace ¶
GetStackTrace returns the full stack trace if available.
func (*AppError) GetStackTraceLines ¶
GetStackTraceLines returns stack trace as a slice of strings from an error. Removes leading whitespace, tabs, and carriage returns from each line.
func (*AppError) UnwrapAll ¶
UnwrapAll retrieves the root cause of the error by traversing the entire chain of causes.
func (*AppError) WithCodeAndMessage ¶
WithCodeAndMessage sets both the error code and message for the AppError.
func (*AppError) WithDetails ¶
WithDetails adds detailed information to the error.
func (*AppError) WithHTTPStatus ¶
WithHTTPStatus allows overriding the default HTTP status code.
func (*AppError) WithMessage ¶
WithMessage sets the error message for the AppError.
type ErrorType ¶
type ErrorType string
ErrorType defines the category of application errors for HTTP status mapping.
const ( // Client-side Errors (4xx) ErrorTypeValidation ErrorType = "VALIDATION" // 400 Bad Request ErrorTypeAuthentication ErrorType = "AUTHENTICATION" // 401 Unauthorized ErrorTypeAuthorization ErrorType = "AUTHORIZATION" // 403 Forbidden ErrorTypeNotFound ErrorType = "NOT_FOUND" // 404 Not Found ErrorTypeConflict ErrorType = "CONFLICT" // 409 Conflict ErrorTypeRateLimit ErrorType = "RATE_LIMIT" // 429 Too Many Requests // Server-side Errors (5xx) ErrorTypeInternal ErrorType = "INTERNAL" // 500 Internal Server Error ErrorTypeExternal ErrorType = "EXTERNAL" // 502 Bad Gateway )
func (ErrorType) DefaultHTTPStatus ¶
DefaultHTTPStatus returns the default HTTP status code for each error type.
Directories
¶
| Path | Synopsis |
|---|---|
|
_examples
|
|
|
basic
command
Package main demonstrates basic usage of the xerrs package.
|
Package main demonstrates basic usage of the xerrs package. |
|
chaining
command
Package main demonstrates error type conversion and chaining in xerrs.
|
Package main demonstrates error type conversion and chaining in xerrs. |
|
wrapping
command
Package main demonstrates error wrapping and automatic detection in xerrs.
|
Package main demonstrates error wrapping and automatic detection in xerrs. |