cgerrors

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Nov 18, 2021 License: Apache-2.0 Imports: 9 Imported by: 13

Documentation

Overview

Package cgerrors provides a way to return detailed information for an RPC request error. The error is normally JSON encoded.

Index

Constants

View Source
const (
	// CodeOK should be used as the codes.OK. Returned on success.
	CodeOK = ErrorCode_OK

	// CodeCanceled indicates the operation was canceled (typically by the caller).
	CodeCanceled = ErrorCode_Canceled

	// CodeUnknown error. An example of where this error may be returned is
	// if an error value received from another address space belongs to
	// an error-space that is not known in this address space. Also, errors
	// raised by APIs that do not return enough error information
	// may be converted to this error.
	CodeUnknown = ErrorCode_Unknown

	// CodeInvalidArgument indicates client specified an invalid argument.
	// Note that this differs from FailedPrecondition. It indicates arguments
	// that are problematic regardless of the state of the system
	// (e.g., a malformed file name, or validation errors).
	CodeInvalidArgument = ErrorCode_InvalidArgument

	// CodeDeadlineExceeded means operation expired before completion.
	// For operations that change the state of the system, this error may be
	// returned even if the operation has completed successfully. For
	// example, a successful response from a server could have been delayed
	// long enough for the deadline to expire.
	CodeDeadlineExceeded = ErrorCode_DeadlineExceeded

	// CodeNotFound means some requested entity or model (e.g., file or directory) was
	// not found.
	CodeNotFound = ErrorCode_NotFound

	// CodeAlreadyExists means an attempt to create an entity failed because one
	// already exists.
	CodeAlreadyExists = ErrorCode_AlreadyExists

	// CodePermissionDenied indicates the caller does not have permission to
	// execute the specified operation. It must not be used for rejections
	// caused by exhausting some resource (use ResourceExhausted
	// instead for those errors). It must not be
	// used if the caller cannot be identified (use Unauthenticated
	// instead for those errors).
	CodePermissionDenied = ErrorCode_PermissionDenied

	// CodeResourceExhausted indicates some resource has been exhausted, perhaps
	// a per-user quota, or perhaps the entire file system is out of space.
	//
	// This error code will be generated by the gRPC framework in
	// out-of-memory and server overload situations, or when a message is
	// larger than the configured maximum size.
	CodeResourceExhausted = ErrorCode_ResourceExhausted

	// CodeFailedPrecondition indicates operation was rejected because the
	// system is not in a state required for the operation's execution.
	// For example, directory to be deleted may be non-empty, a rmdir
	// operation is applied to a non-directory, etc.
	//
	// A litmus test that may help a service implementor in deciding
	// between CodeFailedPrecondition, CodeAborted, and CodeUnavailable:
	//  (a) Use CodeUnavailable if the client can retry just the failing call.
	//  (b) Use CodeAborted if the client should retry at a higher-level
	//      (e.g., restarting a read-modify-write sequence).
	//  (c) Use CodeFailedPrecondition if the client should not retry until
	//      the system state has been explicitly fixed. E.g., if a "rmdir"
	//      fails because the directory is non-empty, FailedPrecondition
	//      should be returned since the client should not retry unless
	//      they have first fixed up the directory by deleting files from it.
	//  (d) Use CodeFailedPrecondition if the client performs conditional
	//      REST Get/Update/Delete on a resource and the resource on the
	//      server does not match the condition. E.g., conflicting
	//      read-modify-write on the same resource.
	CodeFailedPrecondition = ErrorCode_FailedPrecondition

	// CodeAborted indicates the operation was aborted, typically due to a
	// concurrency issue like sequencer check failures, transaction aborts,
	// etc.
	//
	// See litmus test above for deciding between CodeFailedPrecondition,
	// CodeAborted, and CodeUnavailable.
	CodeAborted = ErrorCode_Aborted

	// CodeOutOfRange means operation was attempted past the valid range.
	// E.g., seeking or reading past end of file.
	//
	// Unlike CodeInvalidArgument, this error indicates a problem that may
	// be fixed if the system state changes. For example, a 32-bit file
	// system will generate CodeInvalidArgument if asked to read at an
	// offset that is not in the range [0,2^32-1], but it will generate
	// CodeOutOfRange if asked to read from an offset past the current
	// file size.
	//
	// There is a fair bit of overlap between CodeFailedPrecondition and
	// CodeOutOfRange. We recommend using CodeOutOfRange (the more specific
	// error) when it applies so that callers who are iterating through
	// a space can easily look for an CodeOutOfRange error to detect when
	// they are done.
	CodeOutOfRange = ErrorCode_OutOfRange

	// CodeUnimplemented indicates operation is not implemented or not
	// supported/enabled in this service.
	CodeUnimplemented = ErrorCode_Unimplemented

	// CodeInternal errors. Means some invariants expected by underlying
	// system has been broken. If you see one of these errors,
	// something is very broken.
	CodeInternal = ErrorCode_Internal

	// CodeUnavailable indicates the service is currently unavailable.
	// This is a most likely a transient condition and may be corrected
	// by retrying with a backoff. Note that it is not always safe to retry
	// non-idempotent operations.
	//
	// See litmus test above for deciding between CodeFailedPrecondition,
	// CodeAborted, and CodeUnavailable.
	CodeUnavailable = ErrorCode_Unavailable

	// CodeDataLoss indicates unrecoverable data loss or corruption.
	CodeDataLoss = ErrorCode_DataLoss

	// CodeUnauthenticated indicates the request does not have valid
	// authentication credentials for the operation.
	CodeUnauthenticated = ErrorCode_Unauthenticated
)

ErrorCode new names

View Source
const MetaKeyWrapped = "x-err-wrapped"

MetaKeyWrapped adds the error metadata key that keeps the wrapped error value.

Variables

This section is empty.

Functions

func Equal

func Equal(err1 error, err2 error) bool

Equal tries to compare errors

func Is

func Is(err, target error) bool

Is compares the errors with their values.

func IsAlreadyExists

func IsAlreadyExists(err error) bool

IsAlreadyExists checks if given error means that given entity already exists.

func IsDeadlineExceeded

func IsDeadlineExceeded(err error) bool

IsDeadlineExceeded checks if given error is of type Deadline Exceeded.

func IsInternal

func IsInternal(err error) bool

IsInternal checks if the input is an internal error.

func IsInvalidArgument

func IsInvalidArgument(err error) bool

IsInvalidArgument checks if given error contains a CodeInvalidArgument.

func IsNotFound

func IsNotFound(err error) bool

IsNotFound checks if given input error is of code NotFound.

func IsPermissionDenied

func IsPermissionDenied(err error) bool

IsPermissionDenied checks if given error is of type CodePermissionDenied.

func IsUnauthenticated

func IsUnauthenticated(err error) bool

IsUnauthenticated checks if given error is an unauthenticated error.

func IsUnimplemented added in v0.0.5

func IsUnimplemented(err error) bool

IsUnimplemented checks if given error contains CodeUnimplemented.

func IsUnknown added in v0.1.0

func IsUnknown(err error) bool

IsUnknown checks if given error contains CodeUnknown.

func ToGRPCError

func ToGRPCError(err error) error

ToGRPCError converts an error to GRPC status.Status.

Types

type Error

type Error struct {
	ID      string            `json:"id,omitempty"`
	Code    ErrorCode         `json:"code,omitempty"`
	Detail  string            `json:"detail,omitempty"`
	Process string            `json:"process,omitempty"`
	Meta    map[string]string `json:"meta,omitempty"`
	// contains filtered or unexported fields
}

Error is the error message that has id, it's code and a detail.

func ErrAlreadyExists

func ErrAlreadyExists(a ...interface{}) *Error

ErrAlreadyExists generates a 409 error.

func ErrAlreadyExistsf

func ErrAlreadyExistsf(format string, a ...interface{}) *Error

ErrAlreadyExistsf generates formatted 409 error.

func ErrDeadlineExceeded

func ErrDeadlineExceeded(a ...interface{}) *Error

ErrDeadlineExceeded generates a 408 error.

func ErrDeadlineExceededf

func ErrDeadlineExceededf(format string, a ...interface{}) *Error

ErrDeadlineExceededf generates formatted 408 error.

func ErrFailedPrecondition added in v0.0.21

func ErrFailedPrecondition(a ...interface{}) *Error

ErrFailedPrecondition generates a 400 error.

func ErrFailedPreconditionf added in v0.0.21

func ErrFailedPreconditionf(format string, a ...interface{}) *Error

ErrFailedPreconditionf generates a 400 error.

func ErrInternal

func ErrInternal(a ...interface{}) *Error

ErrInternal generates a 500 error.

func ErrInternalf

func ErrInternalf(format string, a ...interface{}) *Error

ErrInternalf generates formatted 500 error.

func ErrInvalidArgument

func ErrInvalidArgument(a ...interface{}) *Error

ErrInvalidArgument generates a 400 error.

func ErrInvalidArgumentf

func ErrInvalidArgumentf(format string, a ...interface{}) *Error

ErrInvalidArgumentf generates formatted 400 error.

func ErrNotFound

func ErrNotFound(a ...interface{}) *Error

ErrNotFound generates a 404 error.

func ErrNotFoundf

func ErrNotFoundf(format string, a ...interface{}) *Error

ErrNotFoundf generates formatted 404 error.

func ErrOutOfRange added in v0.0.27

func ErrOutOfRange(a ...interface{}) *Error

ErrOutOfRange generates an error with the CodeOutOfRange.

func ErrOutOfRangef added in v0.0.27

func ErrOutOfRangef(format string, a ...interface{}) *Error

ErrOutOfRangef generates formatted error with the CodeOutOfRange.

func ErrPermissionDenied

func ErrPermissionDenied(a ...interface{}) *Error

ErrPermissionDenied generates a 403 error.

func ErrPermissionDeniedf

func ErrPermissionDeniedf(format string, a ...interface{}) *Error

ErrPermissionDeniedf generates a 403 error.

func ErrUnauthenticated

func ErrUnauthenticated(a ...interface{}) *Error

ErrUnauthenticated generates a 401 error.

func ErrUnauthenticatedf

func ErrUnauthenticatedf(format string, a ...interface{}) *Error

ErrUnauthenticatedf generates 401 error with formatted message.

func ErrUnavailable added in v0.0.25

func ErrUnavailable(a ...interface{}) *Error

ErrUnavailable generates Unavailable error.

func ErrUnavailablef added in v0.0.25

func ErrUnavailablef(format string, a ...interface{}) *Error

ErrUnavailablef generates Unavailable error with formatting.

func ErrUnimplemented added in v0.0.5

func ErrUnimplemented(a ...interface{}) *Error

ErrUnimplemented generates Unimplemented error.

func ErrUnimplementedf added in v0.0.5

func ErrUnimplementedf(format string, a ...interface{}) *Error

ErrUnimplementedf generates Unimplemented error with formatting.

func ErrUnknown added in v0.0.23

func ErrUnknown(a ...interface{}) *Error

ErrUnknown generates Unknown error.

func ErrUnknownf added in v0.0.23

func ErrUnknownf(format string, a ...interface{}) *Error

ErrUnknownf generates Unknown error with formatting.

func FromError

func FromError(err error) *Error

FromError try to convert go error to *Error

func FromString added in v0.0.23

func FromString(err string) (*Error, bool)

FromString parses string error into an Error structure.

func New

func New(id, detail string, code ErrorCode) *Error

New generates a custom error.

func Parse

func Parse(err string) *Error

Parse tries to parse a JSON string into an error. If that fails, it will set the given string as the error detail.

func To

func To(err error) *Error

To type check if given error is of *Error type or has encoded ErrorCode in it. Otherwise creates a new error with Unknown code.

func Wrap added in v0.0.25

func Wrap(err error, code ErrorCode, detail string) *Error

Wrap wraps the error with given code and detail.

func Wrapf added in v0.0.25

func Wrapf(err error, code ErrorCode, format string, args ...interface{}) *Error

Wrapf wraps the error with given code and formatted detail message.

func (*Error) Error

func (e *Error) Error() string

Error implements error interface.

func (*Error) GRPCStatus

func (e *Error) GRPCStatus() *status.Status

GRPCStatus implements grpc client interface used to convert statuses.

func (*Error) Is

func (e *Error) Is(err error) bool

Is implements errors.Is function interface to check if input error matches this error or the one wrapped.

func (*Error) Unwrap added in v0.0.25

func (e *Error) Unwrap() error

Unwrap implements errors.Unwrap function internal interface.

func (*Error) WithCode

func (e *Error) WithCode(code ErrorCode) *Error

WithCode sets the code for given error.

func (*Error) WithMeta

func (e *Error) WithMeta(key, value string) *Error

WithMeta sets the key, value metadata for given error.

func (*Error) WithProcess

func (e *Error) WithProcess(process string) *Error

WithProcess sets the process for given error.

type ErrorCode

type ErrorCode int32

ErrorCode is a code that defines errors specification.

const (
	// Deprecated: use CodeOK instead.
	ErrorCode_OK ErrorCode = 0
	// Deprecated: use CodeCanceled instead.
	ErrorCode_Canceled ErrorCode = 1
	// Deprecated: use CodeUnknown instead.
	ErrorCode_Unknown ErrorCode = 2
	// Deprecated: use CodeInvalidArgument instead.
	ErrorCode_InvalidArgument ErrorCode = 3
	// Deprecated: use CodeDeadlineExceeded instead.
	ErrorCode_DeadlineExceeded ErrorCode = 4
	// Deprecated: use CodeNotFound instead.
	ErrorCode_NotFound ErrorCode = 5
	// Deprecated: use CodeAlreadyExists instead.
	ErrorCode_AlreadyExists ErrorCode = 6
	// Deprecated: use CodePermissionDenied instead.
	ErrorCode_PermissionDenied ErrorCode = 7
	// Deprecated: use CodeResourceExhausted instead.
	ErrorCode_ResourceExhausted ErrorCode = 8
	// Deprecated: use CodeFailedPrecondition instead.
	ErrorCode_FailedPrecondition ErrorCode = 9
	// Deprecated: use CodeAborted instead.
	ErrorCode_Aborted ErrorCode = 10
	// Deprecated: use CodeOutOfRange instead.
	ErrorCode_OutOfRange ErrorCode = 11
	// Deprecated: use CodeUnimplemented instead.
	ErrorCode_Unimplemented ErrorCode = 12
	// Deprecated: use CodeInternal instead.
	ErrorCode_Internal ErrorCode = 13
	// Deprecated: use CodeUnavailable instead.
	ErrorCode_Unavailable ErrorCode = 14
	// Deprecated: use CodeDataLoss instead.
	ErrorCode_DataLoss ErrorCode = 15
	// Deprecated: use CodeUnauthenticated instead.
	ErrorCode_Unauthenticated ErrorCode = 16
)

ErrorCode well known codes. nolint: revive,stylecheck

func Code

func Code(err error) ErrorCode

Code gets the code defined in given

func (ErrorCode) String

func (c ErrorCode) String() string

func (ErrorCode) ToGRPCCode

func (c ErrorCode) ToGRPCCode() codes.Code

ToGRPCCode gets the related grpc code.

type ErrorCoder

type ErrorCoder interface {
	ErrorCode(err error) ErrorCode
}

ErrorCoder is an interface used to obtain an error code from given error within given implementation.

type GRPCError

type GRPCError interface {
	GRPCStatus() *status.Status
}

GRPCError is an interface used to get grpcStatus

Jump to

Keyboard shortcuts

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