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 ¶
New creates a new Exception with the given name, description, and cause. It returns the Exception as an error.
func NewSimple ¶
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 ¶
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 ¶
Error implements the error interface. It returns a formatted string representation of the exception, including the description, name, and cause if they are present.