Documentation ¶
Overview ¶
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 ¶
- Constants
- Variables
- func Aggregate(errors []error) error
- func Cause(err error) error
- func Code(err error) vtrpcpb.Code
- func CodeToLegacyErrorCode(code vtrpcpb.Code) vtrpcpb.LegacyErrorCode
- func Equals(a, b error) bool
- func Errorf(code vtrpcpb.Code, format string, args ...interface{}) error
- func FromGRPC(err error) error
- func FromVTRPC(rpcErr *vtrpcpb.RPCError) error
- func LegacyErrorCodeToCode(code vtrpcpb.LegacyErrorCode) vtrpcpb.Code
- func New(code vtrpcpb.Code, message string) error
- func NewErrorf(code vtrpcpb.Code, state State, format string, args ...interface{}) error
- func Print(err error) string
- func RootCause(err error) error
- func ToGRPC(err error) error
- func ToVTRPC(err error) *vtrpcpb.RPCError
- func Wrap(err error, message string) error
- func Wrapf(err error, format string, args ...interface{}) error
- type Frame
- type StackTrace
- type State
Constants ¶
const ( // Informational errors. PriorityOK = iota PriorityCanceled PriorityAlreadyExists PriorityOutOfRange // Potentially retryable errors. 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?
const ( NotServing = "operation not allowed in state NOT_SERVING" ShuttingDown = "operation not allowed in state SHUTTING_DOWN" )
Operation not allowed error
const WrongTablet = "wrong tablet type"
WrongTablet for invalid tablet type error
Variables ¶
var LogErrStacks bool
LogErrStacks controls whether or not printing errors includes the embedded stack trace in the output.
var RxOp = regexp.MustCompile("operation not allowed in state (NOT_SERVING|SHUTTING_DOWN)")
RxOp regex for operation not allowed error
var RxWrongTablet = regexp.MustCompile("(wrong|invalid) tablet type")
RxWrongTablet regex for invalid tablet type error
Functions ¶
func Aggregate ¶
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 ¶
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 CodeToLegacyErrorCode ¶
func CodeToLegacyErrorCode(code vtrpcpb.Code) vtrpcpb.LegacyErrorCode
CodeToLegacyErrorCode maps a vtrpcpb.Code to a vtrpcpb.LegacyErrorCode.
func Errorf ¶
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 ¶
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 ¶
FromVTRPC recovers a vtError from a *vtrpcpb.RPCError (which is how vtError is transmitted across proto3 RPC boundaries).
func LegacyErrorCodeToCode ¶
func LegacyErrorCodeToCode(code vtrpcpb.LegacyErrorCode) vtrpcpb.Code
LegacyErrorCodeToCode maps a vtrpcpb.LegacyErrorCode to a gRPC vtrpcpb.Code.
func New ¶
New returns an error with the supplied message. New also records the stack trace at the point it was called.
func NewErrorf ¶ added in v0.10.0
NewErrorf formats according to a format specifier and returns the string as a value that satisfies error. NewErrorf also records the stack trace at the point it was called.
func Print ¶
Print is meant to print the vtError object in test failures. For comparing two vterrors, use Equals() instead.
func RootCause ¶
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.
Types ¶
type Frame ¶
type Frame uintptr
Frame represents a program counter inside a stack frame.
func (Frame) Format ¶
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 ¶
type StackTrace []Frame
StackTrace is stack of Frames from innermost (newest) to outermost (oldest).
func (StackTrace) Format ¶
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
type State ¶ added in v0.10.0
type State int
State is error state
const ( Undefined State = iota // invalid argument BadFieldError CantUseOptionHere DataOutOfRange EmptyQuery ForbidSchemaChange IncorrectGlobalLocalVar NonUniqTable SyntaxError WrongGroupField WrongTypeForVar WrongValueForVar LockOrActiveTransaction // failed precondition NoDB InnodbReadOnly WrongNumberOfColumnsInSelect // not found BadDb DbDropExists NoSuchTable SPDoesNotExist UnknownSystemVariable UnknownTable // already exists DbCreateExists // resource exhausted NetPacketTooLarge // cancelled QueryInterrupted // unimplemented NotSupportedYet UnsupportedPS // permission denied AccessDeniedError // No state should be added below NumOfStates NumOfStates )
All the error states