exception

package
v0.0.0-...-3edffe1 Latest Latest
Warning

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

Go to latest
Published: Apr 30, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func InitPGError

func InitPGError(errorCodes, constraintCodes *PGError)

InitPGError allows overriding or extending the default PostgreSQL error messages and constraint messages.

You can call this once at startup to customize messages.

Example:

func init() {
    customCodes := exception.PGError{
        "default":   "Query error",
        "not_found": "Record not found",
        "23505": "Custom duplicate error",
    }
    constraintMessages := exception.PGError{
        "users_email_key": "Email already exists",
    }
    exception.InitPGError(&customCodes, &constraintMessages)
}

func New

func New(name, description string, cause error) error

New creates a new Exception with the given name, description, and cause. It returns the Exception as an error.

func NewSimple

func NewSimple(name, description string) error

NewSimple creates a new Exception with the given name and description, omitting the cause. It's a convenience function for situations where there is no underlying error to wrap.

func PG

func PG(err error) error

PG converts a low-level database or ORM error into a user-friendly Exception.

It handles:

  • PostgreSQL errors (*pgconn.PgError)
  • GORM not found errors (gorm.ErrRecordNotFound)
  • All other error types (as generic query error)

Example:

err := &pgconn.PgError{
    Code:           "23505",
    ConstraintName: "users_email_key",
}

ex := exception.PG(err).(*exception.Exception)
fmt.Println(ex.Name)        // Output: "Duplicate record"
fmt.Println(ex.Description) // Output: "Email already exists"

Types

type Exception

type Exception struct {
	// Name is a short identifier for the error, such as an error code.
	Name string

	// Description provides a human-readable explanation of the error.
	Description string

	// Cause holds the underlying error that caused this exception, if any.
	Cause error
}

Exception represents a structured error with a name, a description, and an optional underlying cause. It implements the error interface.

func (*Exception) Error

func (e *Exception) Error() string

Error implements the error interface. It returns a formatted string representation of the exception, including the description, name, and cause if they are present.

func (*Exception) Unwrap

func (e *Exception) Unwrap() error

Unwrap returns the underlying cause of the Exception, enabling support for errors.Unwrap, errors.Is, and errors.As.

func (Exception) WithCause

func (e Exception) WithCause(cause error) error

WithCause returns a new Exception based on the current one but with a specified cause. This allows chaining errors while preserving the original error context.

type PGError

type PGError = map[string]string

PGError defines a mapping between PostgreSQL error codes or constraint names and user-friendly error messages.

Example:

var customCodes = PGError{
    "23505": "Email already exists",
}

Jump to

Keyboard shortcuts

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