code

package
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Dec 19, 2025 License: MIT Imports: 2 Imported by: 0

Documentation

Overview

Package code provides business error codes with HTTP status mapping.

This package defines business-specific error codes and provides convenient functions for creating and wrapping errors with these codes.

Usage:

import (
    "github.com/NSObjects/go-kit/errors"  // for low-level operations
    "github.com/NSObjects/go-kit/code"    // for business errors
)

// Create business errors
err := code.NewNotFoundError("user")
err := code.WrapDatabaseError(dbErr, "query")

// Low-level operations (use errors package)
code := errors.GetCode(err)
httpStatus := errors.HTTPStatus(code)

Index

Constants

View Source
const (
	// ErrSuccess - 200: OK.
	ErrSuccess int = iota + 100001

	// ErrUnknown - 500: Internal server error.
	ErrUnknown

	// ErrBind - 400: Error occurred while binding the request body to the struct.
	ErrBind

	// ErrValidation - 400: Validation failed.
	ErrValidation

	// ErrTokenInvalid - 401: Token invalid.
	ErrTokenInvalid
)

Basic errors (100001-100099)

View Source
const (
	// ErrDatabase - 500: Database error.
	ErrDatabase int = iota + 100101

	// ErrRedis - 500: Redis error.
	ErrRedis

	// ErrKafka - 500: Kafka error.
	ErrKafka

	// ErrExternalService - 500: External service error.
	ErrExternalService
)

Database/Infrastructure errors (100101-100199)

View Source
const (
	// ErrBadRequest - 400: Bad request.
	ErrBadRequest int = 100400
	// ErrUnauthorized - 401: Unauthorized.
	ErrUnauthorized int = 100401
	// ErrForbidden - 403: Forbidden.
	ErrForbidden int = 100403
	// ErrNotFound - 404: Not found.
	ErrNotFound int = 100404
	// ErrInternalServer - 500: Internal server error.
	ErrInternalServer int = 100500
)

HTTP status code related errors (explicit values for clarity)

View Source
const (
	// ErrEncrypt - 401: Error occurred while encrypting the user password.
	ErrEncrypt int = iota + 100201

	// ErrSignatureInvalid - 401: Signature is invalid.
	ErrSignatureInvalid

	// ErrExpired - 401: Token expired.
	ErrExpired

	// ErrInvalidAuthHeader - 401: Invalid authorization header.
	ErrInvalidAuthHeader

	// ErrMissingHeader - 401: The Authorization header was empty.
	ErrMissingHeader

	// ErrPasswordIncorrect - 401: Password was incorrect.
	ErrPasswordIncorrect

	// ErrPermissionDenied - 403: Permission denied.
	ErrPermissionDenied

	// ErrAccountLocked - 403: Account is locked.
	ErrAccountLocked

	// ErrAccountDisabled - 403: Account is disabled.
	ErrAccountDisabled

	// ErrTooManyAttempts - 403: Too many login attempts.
	ErrTooManyAttempts
)

Authentication/Authorization errors (100201-100299)

View Source
const (
	// ErrEncodingFailed - 500: Encoding failed due to an error with the data.
	ErrEncodingFailed int = iota + 100301

	// ErrDecodingFailed - 500: Decoding failed due to an error with the data.
	ErrDecodingFailed

	// ErrInvalidJSON - 500: Data is not valid JSON.
	ErrInvalidJSON

	// ErrEncodingJSON - 500: JSON data could not be encoded.
	ErrEncodingJSON

	// ErrDecodingJSON - 500: JSON data could not be decoded.
	ErrDecodingJSON

	// ErrInvalidYaml - 500: Data is not valid Yaml.
	ErrInvalidYaml

	// ErrEncodingYaml - 500: Yaml data could not be encoded.
	ErrEncodingYaml

	// ErrDecodingYaml - 500: Yaml data could not be decoded.
	ErrDecodingYaml
)

Encoding/Decoding errors (100301-100399)

Variables

This section is empty.

Functions

func IsClientError

func IsClientError(errCode int) bool

IsClientError returns true if the error code represents a client error (4xx).

func IsServerError

func IsServerError(errCode int) bool

IsServerError returns true if the error code represents a server error (5xx).

func NewBadRequestError

func NewBadRequestError(message string) error

NewBadRequestError creates a bad request error.

func NewError

func NewError(code int, message string) error

NewError creates a new error with the specified code and message.

func NewErrorf

func NewErrorf(code int, format string, args ...any) error

NewErrorf creates a new error with the specified code and formatted message.

func NewForbiddenError

func NewForbiddenError() error

NewForbiddenError creates a forbidden error.

func NewNotFoundError

func NewNotFoundError(resource string) error

NewNotFoundError creates a not found error.

func NewPermissionDeniedError

func NewPermissionDeniedError(resource, action string) error

NewPermissionDeniedError creates a permission denied error.

func NewTokenExpiredError

func NewTokenExpiredError() error

NewTokenExpiredError creates a token expired error.

func NewTokenInvalidError

func NewTokenInvalidError() error

NewTokenInvalidError creates a token invalid error.

func NewUnauthorizedError

func NewUnauthorizedError() error

NewUnauthorizedError creates an unauthorized error.

func NewValidationError

func NewValidationError(field, message string) error

NewValidationError creates a validation error.

func WrapBadRequestError

func WrapBadRequestError(err error, message string) error

WrapBadRequestError wraps a 400 error.

func WrapBindError

func WrapBindError(err error, message string) error

WrapBindError wraps a request binding error.

func WrapDatabaseError

func WrapDatabaseError(err error, operation string) error

WrapDatabaseError wraps a database error.

func WrapError

func WrapError(err error, code int, message string) error

WrapError wraps an error with a code.

func WrapErrorf

func WrapErrorf(err error, code int, format string, args ...any) error

WrapErrorf wraps an error with a code and formatted message.

func WrapExternalError

func WrapExternalError(err error, service, operation string) error

WrapExternalError wraps an external service error.

func WrapForbiddenError

func WrapForbiddenError(err error, message string) error

WrapForbiddenError wraps a 403 error.

func WrapInternalServerError

func WrapInternalServerError(err error, message string) error

WrapInternalServerError wraps a 500 error.

func WrapKafkaError

func WrapKafkaError(err error, operation string) error

WrapKafkaError wraps a Kafka error.

func WrapNotFoundError

func WrapNotFoundError(err error, message string) error

WrapNotFoundError wraps a 404 error.

func WrapRedisError

func WrapRedisError(err error, operation string) error

WrapRedisError wraps a Redis error.

func WrapUnauthorizedError

func WrapUnauthorizedError(err error, message string) error

WrapUnauthorizedError wraps a 401 error.

func WrapValidationError

func WrapValidationError(err error, message string) error

WrapValidationError wraps a validation error.

Types

type ErrorCategory

type ErrorCategory string

ErrorCategory represents the category of error.

const (
	CategoryDatabase   ErrorCategory = "database"
	CategoryRedis      ErrorCategory = "redis"
	CategoryKafka      ErrorCategory = "kafka"
	CategoryExternal   ErrorCategory = "external"
	CategorySystem     ErrorCategory = "system"
	CategoryAuth       ErrorCategory = "auth"
	CategoryPermission ErrorCategory = "permission"
	CategoryValidation ErrorCategory = "validation"
	CategoryBusiness   ErrorCategory = "business"
)

type ErrorInfo

type ErrorInfo struct {
	Type     ErrorType     `json:"type"`
	Category ErrorCategory `json:"category"`
	Code     int           `json:"code"`
	Message  string        `json:"message"`
	Details  string        `json:"details,omitempty"`
}

ErrorInfo provides structured error information.

func NewErrorInfo

func NewErrorInfo(err error) ErrorInfo

NewErrorInfo creates ErrorInfo from an error.

func (*ErrorInfo) IsBusiness

func (e *ErrorInfo) IsBusiness() bool

IsBusiness returns true if this is a business error.

func (*ErrorInfo) IsInternal

func (e *ErrorInfo) IsInternal() bool

IsInternal returns true if this is an internal error.

type ErrorType

type ErrorType int

ErrorType represents the type of error.

const (
	InternalError ErrorType = iota
	BusinessError
)

Jump to

Keyboard shortcuts

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