go-vitess.v1: gopkg.in/src-d/go-vitess.v1/vt/vterrors Index | Files

package vterrors

import "gopkg.in/src-d/go-vitess.v1/vt/vterrors"

Package vterrors provides simple error handling primitives for Vitess

In all Vitess code, errors should be propagated using vterrors.Wrapf() and not fmt.Errorf(). This makes sure that stacktraces are kept and propagated correctly.

New errors should be created using vterrors.New or vterrors.Errorf

Vitess uses canonical error codes for error reporting. This is based on years of industry experience with error reporting. This idea is that errors should be classified into a small set of errors (10 or so) with very specific meaning. Each error has a code, and a message. When errors are passed around (even through RPCs), the code is propagated. To handle errors, only the code should be looked at (and not string-matching on the error message).

Error codes are defined in /proto/vtrpc.proto. Along with an RPCError message that can be used to transmit errors through RPCs, in the message payloads. These codes match the names and numbers defined by gRPC.

A standardized error implementation that allows you to build an error with an associated canonical code is also defined. While sending an error through gRPC, these codes are transmitted using gRPC's error propagation mechanism and decoded back to the original code on the other end.

Retrieving the cause of an error

Using vterrors.Wrap constructs a stack of errors, adding context to the preceding error, instead of simply building up a string. Depending on the nature of the error it may be necessary to reverse the operation of errors.Wrap to retrieve the original error for inspection. Any error value which implements this interface

type causer interface {
        Cause() error
}

can be inspected by vterrors.Cause and vterrors.RootCause.

* vterrors.Cause will find the immediate cause if one is available, or nil

if the error is not a `causer` or if no cause is available.

* vterrors.RootCause will recursively retrieve

the topmost error which does not implement causer, which is assumed to be
the original cause. For example:

  switch err := errors.RootCause(err).(type) {
  case *MyError:
          // handle specifically
  default:
          // unknown error
  }

causer interface is not exported by this package, but is considered a part of stable public API.

Formatted printing of errors

All error values returned from this package implement fmt.Formatter and can be formatted by the fmt package. The following verbs are supported

%s    print the error. If the error has a Cause it will be
      printed recursively
%v    extended format. Each Frame of the error's StackTrace will
      be printed in detail.

Most but not all of the code in this file was originally copied from https://github.com/pkg/errors/blob/v0.8.0/errors.go

Index

Package Files

aggregate.go grpc.go proto3.go stack.go vterrors.go

Constants

const (
    // Informational errors.
    PriorityOK = iota
    PriorityCanceled
    PriorityAlreadyExists
    PriorityOutOfRange
    // Potentially retryable errors.
    PriorityUnavailable
    PriorityDeadlineExceeded
    PriorityAborted
    PriorityFailedPrecondition
    // Permanent errors.
    PriorityResourceExhausted
    PriorityUnknown
    PriorityUnauthenticated
    PriorityPermissionDenied
    PriorityInvalidArgument
    PriorityNotFound
    PriorityUnimplemented
    // Serious errors.
    PriorityInternal
    PriorityDataLoss
)

A list of all vtrpcpb.Code, ordered by priority. These priorities are used when aggregating multiple errors in VtGate. Higher priority error codes are more urgent for users to see. They are prioritized based on the following question: assuming a scatter query produced multiple errors, which of the errors is the most likely to give the user useful information about why the query failed and how they should proceed?

Variables

var LogErrStacks bool

LogErrStacks controls whether or not printing errors includes the embedded stack trace in the output.

func Aggregate Uses

func Aggregate(errors []error) error

Aggregate aggregates several errors into a single one. The resulting error code will be the one with the highest priority as defined by the priority constants in this package.

func Cause Uses

func Cause(err error) error

Cause will return the immediate cause, if possible. An error value has a cause if it implements the following interface:

type causer interface {
       Cause() error
}

If the error does not implement Cause, nil will be returned

func Code Uses

func Code(err error) vtrpcpb.Code

Code returns the error code if it's a vtError. If err is nil, it returns ok.

func CodeToLegacyErrorCode Uses

func CodeToLegacyErrorCode(code vtrpcpb.Code) vtrpcpb.LegacyErrorCode

CodeToLegacyErrorCode maps a vtrpcpb.Code to a vtrpcpb.LegacyErrorCode.

func Equals Uses

func Equals(a, b error) bool

Equals returns true iff the error message and the code returned by Code() are equal.

func Errorf Uses

func Errorf(code vtrpcpb.Code, format string, args ...interface{}) error

Errorf formats according to a format specifier and returns the string as a value that satisfies error. Errorf also records the stack trace at the point it was called.

func FromGRPC Uses

func FromGRPC(err error) error

FromGRPC returns a gRPC error as a vtError, translating between error codes. However, there are a few errors which are not translated and passed as they are. For example, io.EOF since our code base checks for this error to find out that a stream has finished.

func FromVTRPC Uses

func FromVTRPC(rpcErr *vtrpcpb.RPCError) error

FromVTRPC recovers a vtError from a *vtrpcpb.RPCError (which is how vtError is transmitted across proto3 RPC boundaries).

func LegacyErrorCodeToCode Uses

func LegacyErrorCodeToCode(code vtrpcpb.LegacyErrorCode) vtrpcpb.Code

LegacyErrorCodeToCode maps a vtrpcpb.LegacyErrorCode to a gRPC vtrpcpb.Code.

func New Uses

func New(code vtrpcpb.Code, message string) error

New returns an error with the supplied message. New also records the stack trace at the point it was called.

func NewWithoutCode Uses

func NewWithoutCode(message string) error

NewWithoutCode returns an error when no applicable error code is available It will record the stack trace when creating the error

func Print Uses

func Print(err error) string

Print is meant to print the vtError object in test failures. For comparing two vterrors, use Equals() instead.

func RootCause Uses

func RootCause(err error) error

RootCause returns the underlying cause of the error, if possible. An error value has a cause if it implements the following interface:

type causer interface {
       Cause() error
}

If the error does not implement Cause, the original error will be returned. If the error is nil, nil will be returned without further investigation.

func ToGRPC Uses

func ToGRPC(err error) error

ToGRPC returns an error as a gRPC error, with the appropriate error code.

func ToVTRPC Uses

func ToVTRPC(err error) *vtrpcpb.RPCError

ToVTRPC converts from vtError to a vtrpcpb.RPCError.

func Wrap Uses

func Wrap(err error, message string) error

Wrap returns an error annotating err with a stack trace at the point Wrap is called, and the supplied message. If err is nil, Wrap returns nil.

func Wrapf Uses

func Wrapf(err error, format string, args ...interface{}) error

Wrapf returns an error annotating err with a stack trace at the point Wrapf is call, and the format specifier. If err is nil, Wrapf returns nil.

type Frame Uses

type Frame uintptr

Frame represents a program counter inside a stack frame.

func (Frame) Format Uses

func (f Frame) Format(s fmt.State, verb rune)

Format formats the frame according to the fmt.Formatter interface.

%s    source file
%d    source line
%n    function name
%v    equivalent to %s:%d

Format accepts flags that alter the printing of some verbs, as follows:

%+s   path of source file relative to the compile time GOPATH
%+v   equivalent to %+s:%d

type StackTrace Uses

type StackTrace []Frame

StackTrace is stack of Frames from innermost (newest) to outermost (oldest).

func (StackTrace) Format Uses

func (st StackTrace) Format(s fmt.State, verb rune)

Format format the stacktrace according to the fmt.Formatter interface.

%s    source file
%d    source line
%n    function name
%v    equivalent to %s:%d

Format accepts flags that alter the printing of some verbs, as follows:

%+s   path of source file relative to the compile time GOPATH
%+v   equivalent to %+s:%d

Package vterrors imports 11 packages (graph) and is imported by 87 packages. Updated 2019-06-13. Refresh now. Tools for package owners.