utils

package
v1.8.0 Latest Latest
Warning

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

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

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrorCodeMapper = struct {
	// ToHTTPStatus returns the HTTP status code for an error code
	ToHTTPStatus func(code core.ErrorCode) int
	// ToCategory returns the category for an error code
	ToCategory func(code core.ErrorCode) string
	// ToMessage returns a user-friendly message for an error code
	ToMessage func(code core.ErrorCode) string
}{
	ToHTTPStatus: func(code core.ErrorCode) int {
		return core.GetHTTPStatus(code)
	},
	ToCategory: func(code core.ErrorCode) string {
		switch code {
		case core.NotFoundCode,
			core.InvalidInputCode,
			core.AlreadyExistsCode,
			core.ResourceExhaustedCode,
			core.ValidationErrorCode,
			core.BusinessRuleViolationCode:
			return "Client"
		case core.DatabaseErrorCode,
			core.InternalErrorCode,
			core.DataCorruptionCode,
			core.ConfigurationErrorCode:
			return "Server"
		case core.TimeoutCode,
			core.CanceledCode,
			core.ConcurrencyErrorCode:
			return "System"
		case core.ExternalServiceErrorCode,
			core.NetworkErrorCode:
			return "External"
		case core.UnauthorizedCode,
			core.ForbiddenCode:
			return "Security"
		default:
			return "Unknown"
		}
	},
	ToMessage: func(code core.ErrorCode) string {
		switch code {
		case core.NotFoundCode:
			return "The requested resource was not found"
		case core.InvalidInputCode:
			return "The provided input is invalid"
		case core.DatabaseErrorCode:
			return "A database operation failed"
		case core.InternalErrorCode:
			return "An internal server error occurred"
		case core.TimeoutCode:
			return "The operation timed out"
		case core.CanceledCode:
			return "The operation was canceled"
		case core.AlreadyExistsCode:
			return "The resource already exists"
		case core.UnauthorizedCode:
			return "Authentication failed"
		case core.ForbiddenCode:
			return "Access is forbidden"
		case core.ValidationErrorCode:
			return "Input validation failed"
		case core.BusinessRuleViolationCode:
			return "A business rule was violated"
		case core.ExternalServiceErrorCode:
			return "An external service call failed"
		case core.NetworkErrorCode:
			return "A network error occurred"
		case core.ConfigurationErrorCode:
			return "Configuration error"
		case core.ResourceExhaustedCode:
			return "Resource limit reached"
		case core.DataCorruptionCode:
			return "Data corruption detected"
		case core.ConcurrencyErrorCode:
			return "Concurrency violation"
		default:
			return "An unknown error occurred"
		}
	},
}

ErrorCodeMapper maps error codes to various representations

Functions

func AddDetail

func AddDetail(err error, key string, value interface{}) error

AddDetail adds a single detail to an error's context

func AddDetails

func AddDetails(err error, details map[string]interface{}) error

AddDetails adds multiple details to an error's context

func BenchmarkAddDetail

func BenchmarkAddDetail(b *testing.B)

func BenchmarkAddDetailMultiple

func BenchmarkAddDetailMultiple(b *testing.B)

func BenchmarkAddDetails

func BenchmarkAddDetails(b *testing.B)

func BenchmarkErrorCodeCategorizer_Categorize

func BenchmarkErrorCodeCategorizer_Categorize(b *testing.B)

func BenchmarkErrorCodeCategorizer_IsClientError

func BenchmarkErrorCodeCategorizer_IsClientError(b *testing.B)

func BenchmarkErrorCodeCategorizer_IsExternalError

func BenchmarkErrorCodeCategorizer_IsExternalError(b *testing.B)

func BenchmarkErrorCodeCategorizer_IsSecurityError

func BenchmarkErrorCodeCategorizer_IsSecurityError(b *testing.B)

func BenchmarkErrorCodeCategorizer_IsServerError

func BenchmarkErrorCodeCategorizer_IsServerError(b *testing.B)

func BenchmarkErrorCodeCategorizer_IsSystemError

func BenchmarkErrorCodeCategorizer_IsSystemError(b *testing.B)

func BenchmarkErrorCodeGrouper_Group

func BenchmarkErrorCodeGrouper_Group(b *testing.B)

func BenchmarkErrorCodeGrouper_IsBusinessError

func BenchmarkErrorCodeGrouper_IsBusinessError(b *testing.B)

func BenchmarkErrorCodeGrouper_IsExternalError

func BenchmarkErrorCodeGrouper_IsExternalError(b *testing.B)

func BenchmarkErrorCodeGrouper_IsInputError

func BenchmarkErrorCodeGrouper_IsInputError(b *testing.B)

func BenchmarkErrorCodeGrouper_IsResourceError

func BenchmarkErrorCodeGrouper_IsResourceError(b *testing.B)

func BenchmarkErrorCodeGrouper_IsSecurityError

func BenchmarkErrorCodeGrouper_IsSecurityError(b *testing.B)

func BenchmarkErrorCodeGrouper_IsSystemError

func BenchmarkErrorCodeGrouper_IsSystemError(b *testing.B)

func BenchmarkErrorCodeMapper_ToCategory

func BenchmarkErrorCodeMapper_ToCategory(b *testing.B)

func BenchmarkErrorCodeMapper_ToCategoryInvalidCode

func BenchmarkErrorCodeMapper_ToCategoryInvalidCode(b *testing.B)

func BenchmarkErrorCodeMapper_ToHTTPStatus

func BenchmarkErrorCodeMapper_ToHTTPStatus(b *testing.B)

func BenchmarkErrorCodeMapper_ToHTTPStatusInvalidCode

func BenchmarkErrorCodeMapper_ToHTTPStatusInvalidCode(b *testing.B)

func BenchmarkErrorCodeMapper_ToMessage

func BenchmarkErrorCodeMapper_ToMessage(b *testing.B)

func BenchmarkErrorCodeMapper_ToMessageInvalidCode

func BenchmarkErrorCodeMapper_ToMessageInvalidCode(b *testing.B)

func BenchmarkGetDetail

func BenchmarkGetDetail(b *testing.B)

func BenchmarkGetDetails

func BenchmarkGetDetails(b *testing.B)

func BenchmarkValidator_GetCodeName

func BenchmarkValidator_GetCodeName(b *testing.B)

func BenchmarkValidator_GetCodeName_Invalid

func BenchmarkValidator_GetCodeName_Invalid(b *testing.B)

func BenchmarkValidator_IsValid

func BenchmarkValidator_IsValid(b *testing.B)

func BenchmarkValidator_Validate

func BenchmarkValidator_Validate(b *testing.B)

func BenchmarkValidator_Validate_Invalid

func BenchmarkValidator_Validate_Invalid(b *testing.B)

func FromJSON

func FromJSON(jsonStr string) (error, error)

FromJSON converts a JSON string to an error.

func GetCallerPackage

func GetCallerPackage(skip int) string

GetCallerPackage returns the package name of the caller.

func GetDetail

func GetDetail(err error, key string) (interface{}, bool)

GetDetail retrieves a detail from an error's context

func GetDetails

func GetDetails(err error) map[string]interface{}

GetDetails retrieves all details from an error's context

func IsValid

func IsValid(code core.ErrorCode) bool

IsValid returns true if the error code is valid

func ToJSON

func ToJSON(err error) string

ToJSON converts an error to a JSON string.

Types

type ErrorCodeCategorizer

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

ErrorCodeCategorizer categorizes error codes

func NewErrorCodeCategorizer

func NewErrorCodeCategorizer() *ErrorCodeCategorizer

NewErrorCodeCategorizer creates a new error code categorizer

func (*ErrorCodeCategorizer) Categorize

Categorize returns the category of an error code

func (*ErrorCodeCategorizer) IsClientError

func (c *ErrorCodeCategorizer) IsClientError(code core.ErrorCode) bool

IsClientError returns true if the error code is a client error

func (*ErrorCodeCategorizer) IsExternalError

func (c *ErrorCodeCategorizer) IsExternalError(code core.ErrorCode) bool

IsExternalError returns true if the error code is an external error

func (*ErrorCodeCategorizer) IsSecurityError

func (c *ErrorCodeCategorizer) IsSecurityError(code core.ErrorCode) bool

IsSecurityError returns true if the error code is a security error

func (*ErrorCodeCategorizer) IsServerError

func (c *ErrorCodeCategorizer) IsServerError(code core.ErrorCode) bool

IsServerError returns true if the error code is a server error

func (*ErrorCodeCategorizer) IsSystemError

func (c *ErrorCodeCategorizer) IsSystemError(code core.ErrorCode) bool

IsSystemError returns true if the error code is a system error

type ErrorCodeCategory

type ErrorCodeCategory string

ErrorCodeCategory represents a category of error codes

const (
	// ClientError indicates an error caused by client input
	ClientError ErrorCodeCategory = "CLIENT_ERROR"
	// ServerError indicates an error caused by server-side issues
	ServerError ErrorCodeCategory = "SERVER_ERROR"
	// SystemError indicates an error caused by system-level issues
	SystemError ErrorCodeCategory = "SYSTEM_ERROR"
	// ExternalError indicates an error caused by external systems
	ExternalError ErrorCodeCategory = "EXTERNAL_ERROR"
	// SecurityError indicates a security-related error
	SecurityError ErrorCodeCategory = "SECURITY_ERROR"
)

type ErrorCodeGroup

type ErrorCodeGroup string

ErrorCodeGroup represents a group of related error codes

const (
	// ResourceGroup contains resource-related errors
	ResourceGroup ErrorCodeGroup = "RESOURCE"
	// InputGroup contains input validation errors
	InputGroup ErrorCodeGroup = "INPUT"
	// SystemGroup contains system-level errors
	SystemGroup ErrorCodeGroup = "SYSTEM"
	// SecurityGroup contains security-related errors
	SecurityGroup ErrorCodeGroup = "SECURITY"
	// ExternalGroup contains external system errors
	ExternalGroup ErrorCodeGroup = "EXTERNAL"
	// BusinessGroup contains business rule errors
	BusinessGroup ErrorCodeGroup = "BUSINESS"
)

type ErrorCodeGrouper

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

ErrorCodeGrouper groups error codes

func NewErrorCodeGrouper

func NewErrorCodeGrouper() *ErrorCodeGrouper

NewErrorCodeGrouper creates a new error code grouper

func (*ErrorCodeGrouper) Group

Group returns the group of an error code

func (*ErrorCodeGrouper) IsBusinessError

func (g *ErrorCodeGrouper) IsBusinessError(code core.ErrorCode) bool

IsBusinessError returns true if the error code is a business error

func (*ErrorCodeGrouper) IsExternalError

func (g *ErrorCodeGrouper) IsExternalError(code core.ErrorCode) bool

IsExternalError returns true if the error code is an external error

func (*ErrorCodeGrouper) IsInputError

func (g *ErrorCodeGrouper) IsInputError(code core.ErrorCode) bool

IsInputError returns true if the error code is an input error

func (*ErrorCodeGrouper) IsResourceError

func (g *ErrorCodeGrouper) IsResourceError(code core.ErrorCode) bool

IsResourceError returns true if the error code is a resource error

func (*ErrorCodeGrouper) IsSecurityError

func (g *ErrorCodeGrouper) IsSecurityError(code core.ErrorCode) bool

IsSecurityError returns true if the error code is a security error

func (*ErrorCodeGrouper) IsSystemError

func (g *ErrorCodeGrouper) IsSystemError(code core.ErrorCode) bool

IsSystemError returns true if the error code is a system error

type ErrorCodeValidator

type ErrorCodeValidator struct {
}

ErrorCodeValidator validates error codes

func NewErrorCodeValidator

func NewErrorCodeValidator() *ErrorCodeValidator

NewErrorCodeValidator creates a new error code validator

func (*ErrorCodeValidator) GetCodeName

func (v *ErrorCodeValidator) GetCodeName(code core.ErrorCode) string

GetCodeName returns the human-readable name of an error code

func (*ErrorCodeValidator) IsValid

func (v *ErrorCodeValidator) IsValid(code core.ErrorCode) bool

IsValid returns true if the error code is valid

func (*ErrorCodeValidator) Validate

func (v *ErrorCodeValidator) Validate(code core.ErrorCode) error

Validate validates an error code

Jump to

Keyboard shortcuts

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