errs

package module
v0.0.0-...-cb46146 Latest Latest
Warning

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

Go to latest
Published: Feb 4, 2026 License: MIT Imports: 7 Imported by: 0

README

errs

The github.com/markbates/errs package provides simple typed error implementations for common use cases, including HTTP status codes, integers, and strings. It also includes several map types for aggregating field-specific errors.

See the documentation for more details.

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ErrorsMap

type ErrorsMap map[string][]error

ErrorsMap is a map of error slices representing field-specific error messages.

Example
package main

import (
	"fmt"

	"github.com/markbates/errs"
)

func main() {
	// Create an ErrorsMap error with field-specific errors.
	err := errs.ErrorsMap{
		"server":   {errs.String("error2"), errs.String("error1")},
		"database": {errs.String("error3")},
	}

	// Error printing is sorted by field name,
	// and each error is printed on its own line.
	// The messages for each field are also sorted.
	fmt.Println(err.Error())

}
Output:

database:
	- error3
server:
	- error1
	- error2

func (ErrorsMap) Error

func (m ErrorsMap) Error() string

Error returns a formatted string representation of the ErrorsMap.

func (ErrorsMap) Is

func (m ErrorsMap) Is(target error) bool

Is reports whether the target is also a ErrorsMap error, ignoring the value.

type Int

type Int int

Int is a typed integer error value.

Example
package main

import (
	"fmt"

	"github.com/markbates/errs"
)

func main() {
	// Create an Int error with a specific value.
	err := errs.Int(42)
	fmt.Println(err.Error())

	// Create an Int error with the zero value.
	err = errs.Int(0)
	fmt.Println(err.Error())

}
Output:

42 error
0 error

func (Int) Error

func (i Int) Error() string

Error formats the integer as "<n> error".

func (Int) Is

func (i Int) Is(target error) bool

Is reports whether the target is also an Int error, ignoring the value.

type StatusCode

type StatusCode int

StatusCode is a typed error value that represents an HTTP status code.

Example
package main

import (
	"fmt"

	"github.com/markbates/errs"
)

func main() {
	// Create a StatusCode error with a specific code.
	err := errs.StatusCode(404)
	fmt.Println(err.StatusCode())
	fmt.Println(err.Error())

	// Create a StatusCode error with the zero value, which defaults to 200.
	err = errs.StatusCode(0)
	fmt.Println(err.StatusCode())
	fmt.Println(err.Error())

}
Output:

404
status: 404
200
status: 200

func (StatusCode) Error

func (s StatusCode) Error() string

Error formats the status code as "status: <code>".

func (StatusCode) Is

func (s StatusCode) Is(target error) bool

Is reports whether the target is also a StatusCode error, ignoring the value.

func (StatusCode) MarshalJSON

func (s StatusCode) MarshalJSON() ([]byte, error)

MarshalJSON marshals the numeric status code value.

func (StatusCode) StatusCode

func (s StatusCode) StatusCode() int

StatusCode returns the code value, defaulting to 200 when it is zero.

type String

type String string

String is a typed string error value.

Example
package main

import (
	"errors"
	"fmt"

	"github.com/markbates/errs"
)

func main() {
	err1 := errs.String("an error occurred")
	fmt.Println(err1.Error())

	err2 := errs.String("boom")
	fmt.Println(err2.Error())

	err3 := fmt.Errorf("wrapping: %w", err1)
	fmt.Println(err3.Error())

	fmt.Println(errors.Is(err3, err1))

}
Output:

an error occurred
boom
wrapping: an error occurred
true

func (String) Error

func (e String) Error() string

Error returns the string contents.

func (String) Is

func (e String) Is(target error) bool

Is reports whether the target is also a String error, ignoring the value.

type StringsMap

type StringsMap map[string][]string

StringsMap is a map of string slices representing field-specific error messages.

Example
package main

import (
	"fmt"

	"github.com/markbates/errs"
)

func main() {
	// Create a StringsMap error with field-specific errors.
	err := errs.StringsMap{
		"server":   {"error2", "error1"},
		"database": {"error3"},
	}

	// Error printing is sorted by field name,
	// and each error is printed on its own line.
	// The messages for each field are also sorted.
	fmt.Println(err.Error())

}
Output:

database:
	- error3
server:
	- error1
	- error2

func (StringsMap) Error

func (m StringsMap) Error() string

Error returns a formatted string representation of the StringsMap.

func (StringsMap) Is

func (m StringsMap) Is(target error) bool

Is returns true if the target is a StringsMap.

Directories

Path Synopsis
internal

Jump to

Keyboard shortcuts

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