Help us shape the future of Go! Take the 2026 Go Developer Survey

errdiff

package
v0.14.1 Latest Latest
Warning

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

Go to latest
Published: Mar 26, 2025 License: Apache-2.0 Imports: 2 Imported by: 0

Documentation

Overview

Package errdiff makes it easy to compare Error by code, substring or exact match in tests.

Similar in intended usage to messagediff.Diff and pretty.Compare, particularly in table-driven tests.

Example usage:

testCases := []struct {
	...
	wantSubstring string
}{
	// Success
	{...},
	// Failures
	{..., wantSubstring: "failed"},
	{..., wantSubstring: "too many users"},
}
for _, c := range testCases {
	got, err := fn(...)
	if diff := errdiff.Substring(err, c.wantSubstring); diff != "" {
		t.Errorf("fn() %v", diff)
		continue
	}
	...
}

The generic function Check may be used in place of Code or Substring or when comparing against another error or for simple existance of an error:

testCases := []struct {
	...
	err interface{}
}{
	// Success
	{...},
	// Failures
	{..., err: io.EOF},  // An explicit error
	{..., err: "my expected error string"},  // contains text
	{..., err: true}, // expect an error, don't care what
}
for _, c := range testCases {
	got, err := fn(...)
	if diff := errdiff.Check(err, c.err); diff != "" {
		t.Errorf("fn() %v", diff)
		continue
	}
	...

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Check

func Check(got error, want interface{}) string

Check returns a message describing the difference between the error err and want. If want is a codes.Code, this function is the same as Code. If want is a string, this function is the same as Substring. If want is an error, this is essentially the same as ExactTextCompare(got, w.Error()). If want is a bool, err is simply tested for existance (want of true means an error is wanted).

func Substring

func Substring(got error, want string) string

Substring returns a message describing the difference between the error text and the desired input. want="" indicates that no error is expected.

func Text

func Text(got error, want string) string

Text returns a message describing the difference between the error text and the desired input. want="" indicates that no error is expected.

Types

This section is empty.

Jump to

Keyboard shortcuts

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