grpcerr

package
v0.20.0 Latest Latest
Warning

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

Go to latest
Published: May 19, 2019 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
var Separator = ":\n\t"

Separator is the string used to separate nested errors. By default, to make errors easier on the eye, nested errors are indented on a new line. A server may instead choose to keep each error on a single line by modifying the separator string, perhaps to ":: ".

Functions

func Is

func Is(kind Kind, err error) bool

Is reports whether err is an *Error of the given Kind. If err is nil then Is returns false.

func Match

func Match(err1, err2 error) bool

Match compares its two error arguments. It can be used to check for expected errors in tests. Both arguments must have underlying type *Error or Match will return false. Otherwise it returns true iff every non-zero element of the first error is equal to the corresponding element of the second. If the Err field is a *Error, Match recurs on that field; otherwise it compares the strings returned by the Error methods. Elements that are in the second argument but not present in the first are ignored.

For example,

Match(errors.E(errors.Permission), err)

tests whether err is an Error with Kind=Permission.

Example
package main

import (
	"fmt"

	"github.com/piotrkowalczuk/charon/internal/grpcerr"
)

func main() {
	err := grpcerr.E("network unreachable")
	// Construct an error, one we pretend to have received from a test.
	got := grpcerr.E(grpcerr.Op("Get"), grpcerr.Kind("io"), err)
	// Now construct a reference error, which might not have all
	// the fields of the error from the test.
	expect := grpcerr.E(grpcerr.Kind("io"), err)
	fmt.Println("Match:", grpcerr.Match(expect, got))
	// Now one that's incorrect - wrong Kind.
	got = grpcerr.E(grpcerr.Op("Get"), grpcerr.Kind("permission"), err)
	fmt.Println("Mismatch:", grpcerr.Match(expect, got))
}
Output:


Match: true
Mismatch: false

func StreamServerInterceptor

func StreamServerInterceptor() grpc.StreamServerInterceptor

func UnaryServerInterceptor

func UnaryServerInterceptor() grpc.UnaryServerInterceptor

Types

type Error

type Error struct {
	// The underlying error that triggered this one, if any.
	Err    error
	Msg    string
	Fields []zapcore.Field
	// Kind is the class of error, such as permission failure,
	// or "Other" if its class is unknown or irrelevant.
	Kind Kind
	Op   Op
	Code codes.Code
	// Details can be mapped into grpc status details.
	Details []proto.Message
}
Example
package main

import (
	"fmt"

	"github.com/piotrkowalczuk/charon/internal/grpcerr"
)

func main() {
	// Single error.
	e1 := grpcerr.E(grpcerr.Op("Get"), grpcerr.Kind("io"), "network unreachable")
	fmt.Println("\nSimple error:")
	fmt.Println(e1)
	// Nested error.
	fmt.Println("\nNested error:")
	e2 := grpcerr.E(grpcerr.Op("Read"), grpcerr.Kind("other"), e1)
	fmt.Println(e2)
}
Output:


Simple error:
Get: io: network unreachable

Nested error:
Read: io:
	Get: network unreachable

func E

func E(args ...interface{}) *Error

E builds an error value from its arguments. There must be at least one argument or E panics. The type of each argument determines its meaning. If more than one argument of a given type is presented, only the last one is recorded.

The types are:

errorek.Op
	The operation being performed, usually the method
	being invoked (Get, Put, etc.).
codes.Code
	GRPC response status code.
proto.Message
	GRPC response status details.
string
	Treated as an error message and assigned to the
	Err field after a call to errors.Str. To avoid a common
	class of misuse, if the string contains an @, it will be
	treated as a PathName or UserName, as appropriate. Use
	errors.Str explicitly to avoid this special-casing.
errorek.Kind
	The class of error, such as permission failure.
error
	The underlying error that triggered this one.

If the error is printed, only those items that have been set to non-zero values will appear in the result.

If Kind is not specified or Other, we set it to the Kind of the underlying error.

func (*Error) Error

func (e *Error) Error() string

type Kind

type Kind string

Kind defines the kind of error this is, mostly for use by systems such as FUSE that must act differently depending on the error.

var Other Kind = "other"

func (Kind) String

func (k Kind) String() string

String implements fmt Stringer interface.

type Op

type Op string

Op describes an operation, usually as the package and method, such as "key/server.Lookup".

func (Op) String

func (o Op) String() string

String implements fmt Stringer interface.

Jump to

Keyboard shortcuts

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