errors

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Jul 24, 2026 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package errors provides type-safe error handling for dupl

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrUnsupportedValueType = errors.New("unsupported value type")
	ErrUnsupportedType      = errors.New("unsupported type")
	ErrInvalidUTF8          = errors.New("invalid UTF-8 encoding")
)

Pre-defined error formats for static error wrapping.

Functions

func HandleMarshalingError

func HandleMarshalingError(operation, context string, err error) error

HandleMarshalingError provides unified JSON marshaling error handling. It inspects the concrete encoding/json/v2 error types (not their message strings, which are unstable) so callers can match the typed sentinels via errors.Is.

In json/v2, UnsupportedValueError and UnsupportedTypeError from v1 are unified into SemanticError. We match on the action field to distinguish marshal-time type incompatibilities from other semantic errors.

func Is

func Is(err error, errorType ErrorType) bool

Is checks if error matches a specific type.

func IsDuplError

func IsDuplError(err error) bool

IsDuplError checks if an error is already a DuplError.

func SafeMarshal

func SafeMarshal(v any, context string) ([]byte, error)

SafeMarshal provides safe marshaling with consistent error handling.

func SafeMarshalIndent

func SafeMarshalIndent(v any, prefix, indent, context string) ([]byte, error)

SafeMarshalIndent provides safe indented marshaling with consistent error handling.

func SafeMarshalIndentNilSafe

func SafeMarshalIndentNilSafe(v any, prefix, indent, nilErrorMessage string) ([]byte, error)

SafeMarshalIndentNilSafe provides safe indented marshaling with nil check. Returns a validation error if the input is nil.

func SafeMarshalNilSafe

func SafeMarshalNilSafe(v any, nilErrorMessage string) ([]byte, error)

SafeMarshalNilSafe provides safe marshaling with nil check. Returns a validation error if the input is nil.

func SafeUnmarshal

func SafeUnmarshal(data []byte, v any, context string) error

SafeUnmarshal provides safe unmarshaling with consistent error handling.

func Wrap

func Wrap(err error, errorType ErrorType, msg string) error

Wrap wraps an error with additional context using the specified error type. If the cause is already a DuplError, it returns the original error.

func WrapConfig

func WrapConfig(err error, context string) error

WrapConfig wraps an error as a ConfigError.

func WrapFile

func WrapFile(err error, file, operation string) error

WrapFile wraps an error as a FileError with operation context.

func WrapIO

func WrapIO(err error, file, operation string) error

WrapIO wraps an error as an IOError with file context.

func WrapValidation

func WrapValidation(err error, context string) error

WrapValidation wraps an error as a ValidationError.

func Wrapf

func Wrapf(err error, errorType ErrorType, format string, args ...any) error

Wrapf wraps an error with formatted context using the specified error type.

Types

type DuplError

type DuplError struct {
	Type    ErrorType
	Message string
	File    string
	Cause   error
}

DuplError is the main error type with rich context.

func NewConfigError

func NewConfigError(msg string, cause error) *DuplError

NewConfigError creates a new configuration error.

func NewFileError

func NewFileError(file, msg string, cause error) *DuplError

NewFileError creates a new file error with context.

func NewIOError

func NewIOError(file, msg string, cause error) *DuplError

NewIOError creates a new I/O error.

func NewInternalError

func NewInternalError(
	msg string,
	cause error,
) *DuplError

NewInternalError creates a new internal error.

func NewValidationError

func NewValidationError(msg string, cause error) *DuplError

NewValidationError creates a new validation error.

func (*DuplError) Error

func (e *DuplError) Error() string

Error implements the error interface.

func (*DuplError) Unwrap

func (e *DuplError) Unwrap() error

Unwrap implements the errors.Wrapper interface.

type ErrorType

type ErrorType string

ErrorType categorizes different types of errors.

const (
	ConfigError     ErrorType = "config"
	IOError         ErrorType = "io"
	ValidationError ErrorType = "validation"
	InternalError   ErrorType = "internal"
	AnalysisError   ErrorType = "analysis"
	FileError       ErrorType = "file"
	CacheError      ErrorType = "cache"
)

type MarshalError

type MarshalError struct {
	Operation string
	Context   string
	Cause     error
}

MarshalError is a specialized error for JSON marshaling failures.

func (*MarshalError) Error

func (e *MarshalError) Error() string

func (*MarshalError) Unwrap

func (e *MarshalError) Unwrap() error

Jump to

Keyboard shortcuts

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