apperrors

package
v0.0.0-...-61c7e6f Latest Latest
Warning

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

Go to latest
Published: May 12, 2025 License: MIT Imports: 2 Imported by: 0

Documentation

Overview

Package apperrors defines domain-specific error types and codes for the application. These errors provide more context than standard Go errors and help in mapping internal issues to appropriate JSON-RPC error responses or handling them specifically within the application.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func MapAppErrorToJSONRPC

func MapAppErrorToJSONRPC(err error) (code int, message string, data map[string]interface{})

MapAppErrorToJSONRPC translates an application error (or any error) into JSON-RPC components (code, message, data) suitable for a JSON-RPC error response.

func NewAuthError

func NewAuthError(code ErrorCode, message string, cause error, context map[string]interface{}) error

NewAuthError creates a new authentication error.

func NewInternalError

func NewInternalError(message string, cause error, context map[string]interface{}) error

NewInternalError creates a generic internal server error (maps to JSON-RPC -32603).

func NewInvalidParamsError

func NewInvalidParamsError(message string, cause error, context map[string]interface{}) error

NewInvalidParamsError creates an error for invalid parameters (maps to JSON-RPC -32602).

func NewInvalidRequestError

func NewInvalidRequestError(message string, cause error, context map[string]interface{}) error

NewInvalidRequestError creates an invalid request structure error (maps to JSON-RPC -32600).

func NewMethodNotFoundError

func NewMethodNotFoundError(message string, cause error, context map[string]interface{}) error

NewMethodNotFoundError creates an error for method not found (maps to JSON-RPC -32601).

func NewParseError

func NewParseError(message string, cause error, context map[string]interface{}) error

NewParseError creates a JSON parse error (maps to JSON-RPC -32700).

func NewProtocolError

func NewProtocolError(code ErrorCode, message string, cause error, context map[string]interface{}) error

NewProtocolError creates a new API/protocol error.

func NewResourceError

func NewResourceError(code ErrorCode, message string, cause error, context map[string]interface{}) error

NewResourceError creates a new resource error.

func NewServiceNotFoundError

func NewServiceNotFoundError(message string, cause error, context map[string]interface{}) error

NewServiceNotFoundError creates an error when a required internal service/component lookup fails.

Types

type AuthError

type AuthError struct{ BaseError }

AuthError represents an authentication or authorization error.

type BaseError

type BaseError struct {
	Code    ErrorCode
	Message string
	Cause   error
	Context map[string]interface{}
}

BaseError is the common base for custom application error types.

func (*BaseError) Error

func (e *BaseError) Error() string

Error implements the standard Go error interface.

func (*BaseError) Unwrap

func (e *BaseError) Unwrap() error

Unwrap returns the underlying error (Cause), enabling errors.Is and errors.As.

func (*BaseError) WithContext

func (e *BaseError) WithContext(key string, value interface{}) *BaseError

WithContext adds a key-value pair to the error's context map. It initializes the map if necessary and returns the modified error pointer for chaining.

type ErrorCode

type ErrorCode int

ErrorCode defines domain-specific error codes.

const (
	// --- Auth Errors (1000-1999) ---.
	// Consistent with general authentication concepts.
	ErrAuthFailure ErrorCode = 1000 + iota
	ErrAuthExpired
	ErrAuthInvalid
	ErrAuthMissing

	// --- Resource Errors (3000-3999) ---.
	// Consistent with general resource access concepts.
	ErrResourceNotFound ErrorCode = 3000 + iota
	ErrResourceForbidden
	ErrResourceInvalid

	// --- API/Protocol Errors (4000-4999 & JSON-RPC range) ---.
	// For errors related to the application's API or general protocol handling.
	ErrProtocolInvalid     ErrorCode = 4000 + iota // e.g., malformed API request beyond basic parsing
	ErrProtocolUnsupported                         // e.g., trying to use an unsupported version or feature

	// JSON-RPC Standard Codes mapped to our ErrorCode type.
	// These are standard and highly relevant for any JSON-RPC style API.
	ErrParseError     ErrorCode = -32700 // JSONRPCParseError
	ErrInvalidRequest ErrorCode = -32600 // JSONRPCInvalidRequest
	ErrMethodNotFound ErrorCode = -32601 // JSONRPCMethodNotFound
	ErrInvalidParams  ErrorCode = -32602 // JSONRPCInvalidParams
	ErrInternalError  ErrorCode = -32603 // JSONRPCInternalError

	// Custom server-defined API/application errors within the recommended JSON-RPC range (-32000 to -32099).
	ErrRequestSequence ErrorCode = -32001 // Invalid message sequence for current state (if state matters)
	ErrServiceNotFound ErrorCode = -32002 // Specific internal error when a required internal service/component lookup fails

)

Domain-specific error codes.

type InternalError

type InternalError struct{ BaseError }

InternalError represents a generic internal server error, aligning with JSON-RPC.

type InvalidParamsError

type InvalidParamsError struct{ BaseError }

InvalidParamsError represents an error due to invalid method parameters, aligning with JSON-RPC.

type InvalidRequestError

type InvalidRequestError struct{ BaseError }

InvalidRequestError represents an invalid JSON-RPC request structure error, aligning with JSON-RPC.

type MethodNotFoundError

type MethodNotFoundError struct{ BaseError }

MethodNotFoundError represents an error when a requested method is not found, aligning with JSON-RPC.

type ParseError

type ParseError struct{ BaseError }

ParseError represents a JSON parsing error, aligning with JSON-RPC.

type ProtocolError

type ProtocolError struct{ BaseError }

ProtocolError represents a violation of the application's API protocol rules or structure.

type ResourceError

type ResourceError struct{ BaseError }

ResourceError represents an error related to accessing or manipulating an application resource.

type ServiceNotFoundError

type ServiceNotFoundError struct{ BaseError }

ServiceNotFoundError represents an error when a required internal service/component cannot be found.

Jump to

Keyboard shortcuts

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