markers

package
v1.4.0 Latest Latest
Warning

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

Go to latest
Published: Apr 11, 2020 License: Apache-2.0 Imports: 6 Imported by: 38

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func HasType added in v1.4.0

func HasType(err error, referenceType error) bool

HasType returns true iff err contains an error whose concrete type matches that of referenceType.

func If

func If(err error, pred func(err error) (interface{}, bool)) (interface{}, bool)

If returns a predicate's return value the first time the predicate returns true.

Note: if any of the error types has been migrated from a previous package location or a different type, ensure that RegisterTypeMigration() was called prior to If().

func Is

func Is(err, reference error) bool

Is determines whether one of the causes of the given error or any of its causes is equivalent to some reference error.

Note: if any of the error types has been migrated from a previous package location or a different type, ensure that RegisterTypeMigration() was called prior to Is().

func IsAny

func IsAny(err error, references ...error) bool

IsAny is like Is except that multiple references are compared.

Note: if any of the error types has been migrated from a previous package location or a different type, ensure that RegisterTypeMigration() was called prior to IsAny().

func IsInterface added in v1.3.0

func IsInterface(err error, referenceInterface interface{}) bool

IsInterface returns true if err contains an error which implements the interface pointed to by referenceInterface. The type of referenceInterface must be a pointer to an interface type. If referenceInterface is not a pointer to an interface, this function will panic.

Example
package main

import (
	"fmt"
	"net"

	"github.com/cockroachdb/errors/markers"
	"github.com/pkg/errors"
)

func main() {
	base := &net.AddrError{
		Addr: "ndn",
		Err:  "ndn doesn't really exists :(",
	}
	err := errors.Wrap(base, "bummer")
	fmt.Println(markers.IsInterface(err, (*net.Error)(nil)))
	func() {
		defer func() {
			if r := recover(); r != nil {
				fmt.Println("*net.AddrError is not a pointer to an interface type so the call panics")
			}
		}()
		fmt.Println(markers.IsInterface(err, (*net.AddrError)(nil)))
	}()

}
Output:


true
*net.AddrError is not a pointer to an interface type so the call panics

func IsType added in v1.3.0

func IsType(err error, referenceType error) bool

IsType returns true if the outermost err object has a concrete type matching that of referenceType.

Example
package main

import (
	"fmt"
	"net"

	"github.com/cockroachdb/errors/markers"
	"github.com/pkg/errors"
)

type ExampleError struct{ msg string }

func (e *ExampleError) Error() string { return e.msg }

func main() {
	base := &ExampleError{"world"}
	err := errors.Wrap(base, "hello")
	fmt.Println(markers.HasType(err, (*ExampleError)(nil)))
	fmt.Println(markers.IsType(err, (*ExampleError)(nil)))
	fmt.Println(markers.HasType(err, nil))
	fmt.Println(markers.IsType(err, nil))
	fmt.Println(markers.HasType(err, (*net.AddrError)(nil)))
	fmt.Println(markers.IsType(err, (*net.AddrError)(nil)))

}
Output:


true
false
false
false
false
false

func Mark

func Mark(err error, reference error) error

Mark creates an explicit mark for the given error, using the same mark as some reference error.

Note: if any of the error types has been migrated from a previous package location or a different type, ensure that RegisterTypeMigration() was called prior to Mark().

Types

This section is empty.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
JackTT - Gopher 🇻🇳