errors API

errors

package

API reference for the errors package.

S
struct

MultiError

MultiError is a collection of errors that implements the error interface.

pkg/errors/errors.go:14-16
type MultiError struct

Example

errs := &errors.MultiError{}
errs.Append(err1, err2)
if err := errs.ErrorOrNil(); err != nil { ... }

Methods

Append
Method

Append adds errors to the collection. Nil errors are ignored.

Parameters

errs ...error
func (*MultiError) Append(errs ...error)
{
	for _, err := range errs {
		if err != nil {
			e.Errors = append(e.Errors, err)
		}
	}
}
Error
Method

Error implements the error interface.

Returns

string
func (*MultiError) Error() string
{
	if len(e.Errors) == 0 {
		return ""
	}
	if len(e.Errors) == 1 {
		return e.Errors[0].Error()
	}
	var sb strings.Builder
	sb.WriteString("multiple errors occurred: ")
	for i, err := range e.Errors {
		if i > 0 {
			sb.WriteString("; ")
		}
		sb.WriteString(err.Error())
	}
	return sb.String()
}
Unwrap
Method

Unwrap returns the errors as a slice for errors.Is/As support (Go 1.20+).

Returns

[]error
func (*MultiError) Unwrap() []error
{
	return e.Errors
}
HasErrors
Method

HasErrors returns true if there are any errors in the collection.

Returns

bool
func (*MultiError) HasErrors() bool
{
	return len(e.Errors) > 0
}
ErrorOrNil
Method

ErrorOrNil returns nil if there are no errors, otherwise returns itself.

Returns

error
func (*MultiError) ErrorOrNil() error
{
	if len(e.Errors) == 0 {
		return nil
	}
	return e
}

Fields

Name Type Description
Errors []error
F
function

Join

Join is a helper to join multiple errors into a single error.

Parameters

errs
...error

Returns

error
pkg/errors/errors.go:73-77
func Join(errs ...error) error

{
	e := &MultiError{}
	e.Append(errs...)
	return e.ErrorOrNil()
}

Example

err := errors.Join(err1, err2)
F
function

TestMultiError_Append

Parameters

pkg/errors/errors_test.go:9-18
func TestMultiError_Append(t *testing.T)

{
	e := &MultiError{}
	e.Append(errors.New("err1"))
	e.Append(nil)
	e.Append(errors.New("err2"))

	if len(e.Errors) != 2 {
		t.Errorf("got %d errors, want 2", len(e.Errors))
	}
}
F
function

TestMultiError_Error

Parameters

pkg/errors/errors_test.go:20-36
func TestMultiError_Error(t *testing.T)

{
	e := &MultiError{}
	if e.Error() != "" {
		t.Errorf("empty multierror should return empty string, got %q", e.Error())
	}

	e.Append(errors.New("one"))
	if e.Error() != "one" {
		t.Errorf("single error: got %q, want %q", e.Error(), "one")
	}

	e.Append(errors.New("two"))
	want := "multiple errors occurred: one; two"
	if e.Error() != want {
		t.Errorf("multiple errors: got %q, want %q", e.Error(), want)
	}
}
F
function

TestMultiError_Unwrap

Parameters

pkg/errors/errors_test.go:38-47
func TestMultiError_Unwrap(t *testing.T)

{
	err1 := errors.New("err1")
	err2 := errors.New("err2")
	e := &MultiError{Errors: []error{err1, err2}}

	unwrapped := e.Unwrap()
	if len(unwrapped) != 2 || unwrapped[0] != err1 || unwrapped[1] != err2 {
		t.Error("Unwrap should return all errors")
	}
}
F
function

TestJoin

Parameters

pkg/errors/errors_test.go:49-61
func TestJoin(t *testing.T)

{
	err := Join(errors.New("a"), nil, errors.New("b"))
	if err == nil {
		t.Fatal("Join should return error")
	}
	if !strings.Contains(err.Error(), "a") || !strings.Contains(err.Error(), "b") {
		t.Errorf("Join error content: %v", err)
	}

	if Join(nil, nil) != nil {
		t.Error("Join with only nil should return nil")
	}
}