xerror

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Feb 1, 2026 License: Apache-2.0 Imports: 2 Imported by: 1

README

xerror

The xerror package provides advanced tools for working with errors in Go.

Features

  • Const: Constant errors that can be declared at the package level without risk of modification.
  • SentinelError: A wrapper that allows associating any error with a "marker" (sentinel) error, preserving the ability to check via errors.Is.

Installation

go get github.com/AleksandrGurkin/xerror

Usage

Const

Use xerror.Const to create immutable errors:

const ErrMyConstant = xerror.Const("my constant error")

func doSomething() error {
    return ErrMyConstant
}
SentinelError

Allows wrapping an error so it behaves like a sentinel but contains details:

var ErrNotFound = xerror.Const("not found")

func findUser(id string) error {
    err := db.Query(...)
    if err != nil {
        return xerror.WrapWithSentinel(err, ErrNotFound)
    }
    return nil
}

// Later:
if errors.Is(err, ErrNotFound) {
    // Will work
}

License

See the LICENSE file.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func WrapWithSentinel added in v1.0.0

func WrapWithSentinel(err error, sentinel error) error

Types

type Const added in v1.0.0

type Const string

Const is used for constant sentinels

const Err1 = xerror.Const("const error")

func (Const) Error added in v1.0.0

func (e Const) Error() string

type SentinelError added in v1.0.0

type SentinelError struct {
	// contains filtered or unexported fields
}

func (*SentinelError) Error added in v1.0.0

func (e *SentinelError) Error() string

func (*SentinelError) Is added in v1.0.0

func (e *SentinelError) Is(target error) bool

func (*SentinelError) Unwrap added in v1.0.0

func (e *SentinelError) Unwrap() error

Jump to

Keyboard shortcuts

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