rpc

package
v1.1.1 Latest Latest
Warning

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

Go to latest
Published: Mar 21, 2022 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Overview

Package rpc provides RPC functionality.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetErrorMessage added in v1.0.0

func GetErrorMessage(err error) string

GetErrorMessage gets the rpc error message, if there is an RPC error in the error chain. If not, it returns an empty string.

func GetIncomingHeader

func GetIncomingHeader(ctx context.Context, key string) string

GetIncomingHeader gets the given header key.

Headers are simple key/value with no differentiation between unset and nil. This is as opposed to i.e. grpc that does key/slice value with differentiation between unset and nil. Headers are case-insensitive.

If the key is unset, this returns the empty string.

func GetIncomingHeaders

func GetIncomingHeaders(ctx context.Context) map[string]string

GetIncomingHeaders gets the headers..

Headers are simple key/value with no differentiation between unset and nil. This is as opposed to i.e. grpc that does key/slice value with differentiation between unset and nil. Headers are case-insensitive.

If there are no headers, returns an empty map.

func GetOutgoingHeader

func GetOutgoingHeader(ctx context.Context, key string) string

GetOutgoingHeader gets the given header key.

Headers are simple key/value with no differentiation between unset and nil. This is as opposed to i.e. grpc that does key/slice value with differentiation between unset and nil. Headers are case-insensitive.

If the key is unset, this returns the empty string.

func GetOutgoingHeaders

func GetOutgoingHeaders(ctx context.Context) map[string]string

GetOutgoingHeaders gets the headers..

Headers are simple key/value with no differentiation between unset and nil. This is as opposed to i.e. grpc that does key/slice value with differentiation between unset and nil. Headers are case-insensitive.

If there are no headers, returns an empty map.

func IsError

func IsError(err error) bool

IsError returns true if err is an error created by this package.

If the error is nil, this returns false.

func NewAbortedError

func NewAbortedError(message string) error

NewAbortedError is a convenience function for errors with ErrorCodeAborted.

func NewAbortedErrorf

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

NewAbortedErrorf is a convenience function for errors with ErrorCodeAborted.

func NewAlreadyExistsError

func NewAlreadyExistsError(message string) error

NewAlreadyExistsError is a convenience function for errors with ErrorCodeAlreadyExists.

func NewAlreadyExistsErrorf

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

NewAlreadyExistsErrorf is a convenience function for errors with ErrorCodeAlreadyExists.

func NewCanceledError

func NewCanceledError(message string) error

NewCanceledError is a convenience function for errors with ErrorCodeCanceled.

func NewCanceledErrorf

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

NewCanceledErrorf is a convenience function for errors with ErrorCodeCanceled.

func NewDataLossError

func NewDataLossError(message string) error

NewDataLossError is a convenience function for errors with ErrorCodeDataLoss.

func NewDataLossErrorf

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

NewDataLossErrorf is a convenience function for errors with ErrorCodeDataLoss.

func NewDeadlineExceededError

func NewDeadlineExceededError(message string) error

NewDeadlineExceededError is a convenience function for errors with ErrorCodeDeadlineExceeded.

func NewDeadlineExceededErrorf

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

NewDeadlineExceededErrorf is a convenience function for errors with ErrorCodeDeadlineExceeded.

func NewError

func NewError(errorCode ErrorCode, message string) error

NewError returns a new error with a code.

The value of Error() will only contain the message. If you would like to also print the error code, you must do this manually.

If the code is invalid, an error with ErrorCodeInternal will be returned.

func NewErrorf

func NewErrorf(errorCode ErrorCode, format string, args ...interface{}) error

NewErrorf returns a new error.

The value of Error() will only contain the message. If you would like to also print the code, you must do this manually.

If the code is invalid, an error with ErrorCodeInternal will be returned.

func NewFailedPreconditionError

func NewFailedPreconditionError(message string) error

NewFailedPreconditionError is a convenience function for errors with ErrorCodeFailedPrecondition.

func NewFailedPreconditionErrorf

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

NewFailedPreconditionErrorf is a convenience function for errors with ErrorCodeFailedPrecondition.

func NewInternalError

func NewInternalError(message string) error

NewInternalError is a convenience function for errors with ErrorCodeInternal.

func NewInternalErrorf

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

NewInternalErrorf is a convenience function for errors with ErrorCodeInternal.

func NewInvalidArgumentError

func NewInvalidArgumentError(message string) error

NewInvalidArgumentError is a convenience function for errors with ErrorCodeInvalidArgument.

func NewInvalidArgumentErrorf

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

NewInvalidArgumentErrorf is a convenience function for errors with ErrorCodeInvalidArgument.

func NewNotFoundError

func NewNotFoundError(message string) error

NewNotFoundError is a convenience function for errors with ErrorCodeNotFound.

func NewNotFoundErrorf

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

NewNotFoundErrorf is a convenience function for errors with ErrorCodeNotFound.

func NewOutOfRangeError

func NewOutOfRangeError(message string) error

NewOutOfRangeError is a convenience function for errors with ErrorCodeOutOfRange.

func NewOutOfRangeErrorf

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

NewOutOfRangeErrorf is a convenience function for errors with ErrorCodeOutOfRange.

func NewPermissionDeniedError

func NewPermissionDeniedError(message string) error

NewPermissionDeniedError is a convenience function for errors with ErrorCodePermissionDenied.

func NewPermissionDeniedErrorf

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

NewPermissionDeniedErrorf is a convenience function for errors with ErrorCodePermissionDenied.

func NewResourceExhaustedError

func NewResourceExhaustedError(message string) error

NewResourceExhaustedError is a convenience function for errors with ErrorCodeResourceExhausted.

func NewResourceExhaustedErrorf

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

NewResourceExhaustedErrorf is a convenience function for errors with ErrorCodeResourceExhausted.

func NewUnauthenticatedError

func NewUnauthenticatedError(message string) error

NewUnauthenticatedError is a convenience function for errors with ErrorCodeUnauthenticated.

func NewUnauthenticatedErrorf

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

NewUnauthenticatedErrorf is a convenience function for errors with ErrorCodeUnauthenticated.

func NewUnavailableError

func NewUnavailableError(message string) error

NewUnavailableError is a convenience function for errors with ErrorCodeUnavailable.

func NewUnavailableErrorf

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

NewUnavailableErrorf is a convenience function for errors with ErrorCodeUnavailable.

func NewUnimplementedError

func NewUnimplementedError(message string) error

NewUnimplementedError is a convenience function for errors with ErrorCodeUnimplemented.

func NewUnimplementedErrorf

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

NewUnimplementedErrorf is a convenience function for errors with ErrorCodeUnimplemented.

func NewUnknownError

func NewUnknownError(message string) error

NewUnknownError is a convenience function for errors with ErrorCodeUnknown.

func NewUnknownErrorf

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

NewUnknownErrorf is a convenience function for errors with ErrorCodeUnknown.

func WithIncomingHeader

func WithIncomingHeader(ctx context.Context, key string, value string) context.Context

WithIncomingHeader adds the given header to the context.

Headers are simple key/value with no differentiation between unset and nil. This is as opposed to i.e. grpc that does key/slice value with differentiation between unset and nil. Headers are case-insensitive.

If the key or value is empty, this is a no-op. If the key was already set, this will overwrite the value for the key.

func WithIncomingHeaders

func WithIncomingHeaders(ctx context.Context, headers map[string]string) context.Context

WithIncomingHeaders adds the given headers to the context.

Headers are simple key/value with no differentiation between unset and nil. This is as opposed to i.e. grpc that does key/slice value with differentiation between unset and nil. Headers are case-insensitive.

If headers is empty or nil, this is a no-op. If a key or value is empty, this is a no-op for that key. If a key was already set, this will overwrite the value for the key.

func WithOutgoingHeader

func WithOutgoingHeader(ctx context.Context, key string, value string) context.Context

WithOutgoingHeader adds the given header to the context.

Headers are simple key/value with no differentiation between unset and nil. This is as opposed to i.e. grpc that does key/slice value with differentiation between unset and nil. Headers are case-insensitive.

If the key or value is empty, this is a no-op. If the key was already set, this will overwrite the value for the key.

func WithOutgoingHeaders

func WithOutgoingHeaders(ctx context.Context, headers map[string]string) context.Context

WithOutgoingHeaders adds the given headers to the context.

Headers are simple key/value with no differentiation between unset and nil. This is as opposed to i.e. grpc that does key/slice value with differentiation between unset and nil. Headers are case-insensitive.

If headers is empty or nil, this is a no-op. If a key or value is empty, this is a no-op for that key. If a key was already set, this will overwrite the value for the key.

Types

type ErrorCode

type ErrorCode int

ErrorCode is an error code.

Unlike gRPC and Twirp, there is no zero code for success.

All errors must have a valid error code. If an error does not have a valid error code when performing error operations, a new error with ErrorCodeInternal will be returned.

const (
	// ErrorCodeOK is a sentinel code for no error.
	//
	// No error should use this directly. This is returned by GetErrorCode if the error is nil.
	ErrorCodeOK ErrorCode = 0

	// ErrorCodeCanceled indicates the operation was canceled.
	//
	// HTTP equivalent: 408 REQUEST TIMEOUT
	ErrorCodeCanceled ErrorCode = 1

	// ErrorCodeUnknown indicates an unknown error.
	//
	// This is for errors that do not have specific error information.
	//
	// HTTP equivalent: 500 INTERNAL SERVER ERROR
	ErrorCodeUnknown ErrorCode = 2

	// ErrorCodeInvalidArgument indicates that the an invalid argument was specified, for
	// example missing required arguments and invalid arguments.
	//
	// HTTP equivalent: 400 BAD REQUEST
	ErrorCodeInvalidArgument ErrorCode = 3

	// ErrorCodeDeadlineExceeded indicates that a deadline was exceeded, for example a timeout.
	//
	// HTTP equivalent: 504 GATEWAY TIMEOUT
	// Note that Twirp treats this as a 408 REQUEST TIMEOUT, but grpc-gateway treats this
	// as 504 GATEWAY TIMEOUT.
	ErrorCodeDeadlineExceeded ErrorCode = 4

	// ErrorCodeNotFound indicates that an entity was not found.
	//
	// HTTP equivalent: 404 NOT FOUND
	ErrorCodeNotFound ErrorCode = 5

	// ErrorCodeAlreadyExists indicates that entity creation was unsuccessful as the entity
	// already exists.
	//
	// HTTP equivalent: 409 CONFLICT
	ErrorCodeAlreadyExists ErrorCode = 6

	// ErrorCodePermissionDenied indicates the caller does not have permission to perform
	// the requested operation.
	//
	// HTTP equivalent: 403 FORBIDDEN
	ErrorCodePermissionDenied ErrorCode = 7

	// ErrorCodeResourceExhausted indicates some resource has been exhausted, for example
	// throttling or out-of-space errors.
	//
	// HTTP equivalent: 429 TOO MANY REQUESTS
	ErrorCodeResourceExhausted ErrorCode = 8

	// ErrorCodeFailedPrecondition indicates operation was rejected because the system is not
	// in a state required for the operation's execution, for example a non-recursive
	// non-empty directory deletion.
	//
	// HTTP equivalent: 400 BAD REQUEST
	// Note that Twirp treats this as 412 PRECONDITION FAILED, but grpc-gateway treats this
	// as 400 BAD REQUEST, and has a note saying this is on purpose (and it makes sense).
	ErrorCodeFailedPrecondition ErrorCode = 9

	// ErrorCodeAborted indicates the operation was aborted, for example when a transaction
	// is aborted.
	//
	// HTTP equivalent: 409 CONFLICT
	ErrorCodeAborted ErrorCode = 10

	// ErrorCodeOutOfRange indicates an operation was attempted past the valid range, for example
	// seeking or reading past the end of a paginated collection.
	//
	// Unlike InvalidArgument, this error indicates a problem that may be fixed if
	// the system state changes (i.e. adding more items to the collection).
	//
	// There is a fair bit of overlap between FailedPrecondition and OutOfRange.
	// We recommend using OutOfRange (the more specific error) when it applies so
	// that callers who are iterating through a space can easily look for an
	// OutOfRange error to detect when they are done.
	//
	// HTTP equivant: 400 BAD REQUEST
	ErrorCodeOutOfRange ErrorCode = 11

	// ErrorCodeUnimplemented indicates operation is not implemented or not
	// supported/enabled in this service.
	//
	// HTTP equivalent: 501 NOT IMPLEMENTED
	ErrorCodeUnimplemented ErrorCode = 12

	// ErrorCodeInternal indicates an internal system error.
	//
	// HTTP equivalent: 500 INTERNAL SERVER ERROR
	ErrorCodeInternal ErrorCode = 13

	// ErrorCodeUnavailable indicates the service is currently unavailable.
	// This is a most likely a transient condition and may be corrected
	// by retrying with a backoff.
	//
	// HTTP equivalent: 503 SERVICE UNAVAILABLE
	ErrorCodeUnavailable ErrorCode = 14

	// ErrorCodeDataLoss indicates unrecoverable data loss or corruption.
	//
	// HTTP equivalent: 500 INTERNAL SERVER ERROR
	ErrorCodeDataLoss ErrorCode = 15

	// ErrorCodeUnauthenticated indicates the request does not have valid
	// authentication credentials for the operation. This is different than
	// PermissionDenied, which deals with authorization.
	//
	// HTTP equivalent: 401 UNAUTHORIZED
	ErrorCodeUnauthenticated ErrorCode = 16
)

func GetErrorCode

func GetErrorCode(err error) ErrorCode

GetErrorCode gets the error code.

If the error is nil, this returns ErrorCodeOK. If the error is not created by this package, it will return ErrorCodeInternal.

Note that gRPC maps to ErrorCodeUnknown in the same scenario.

func (ErrorCode) String

func (e ErrorCode) String() string

String returns the string value of e.

type ServerHandler

type ServerHandler interface {
	Handle(ctx context.Context, request interface{}) (interface{}, error)
}

ServerHandler is a server handler.

type ServerHandlerFunc

type ServerHandlerFunc func(ctx context.Context, request interface{}) (interface{}, error)

ServerHandlerFunc is a function that implements ServerHandler.

func (ServerHandlerFunc) Handle

func (h ServerHandlerFunc) Handle(ctx context.Context, request interface{}) (interface{}, error)

Handle implements ServerHandler.

type ServerInfo

type ServerInfo struct {
	// Path is the rpc method path.
	//
	// This is RPC-system dependent, but will be unique for a given RPC method.
	// For gRPC, this is /package.Service/Method.
	//
	// May be empty, although almost never is in current practice.
	Path string
}

ServerInfo contains server rpc info.

type ServerInterceptor

type ServerInterceptor interface {
	Intercept(
		ctx context.Context,
		request interface{},
		serverInfo *ServerInfo,
		serverHandler ServerHandler,
	) (interface{}, error)
}

ServerInterceptor is a server interceptor.

func NewChainedServerInterceptor

func NewChainedServerInterceptor(interceptors ...ServerInterceptor) ServerInterceptor

NewChainedServerInterceptor returns a new chained ServerInterceptor.

Returns nil if interceptors is empty.

type ServerInterceptorFunc

type ServerInterceptorFunc func(
	ctx context.Context,
	request interface{},
	serverInfo *ServerInfo,
	serverHandler ServerHandler,
) (interface{}, error)

ServerInterceptorFunc is a function that implements ServerInterceptor.

func (ServerInterceptorFunc) Intercept

func (i ServerInterceptorFunc) Intercept(
	ctx context.Context,
	request interface{},
	serverInfo *ServerInfo,
	serverHandler ServerHandler,
) (interface{}, error)

Intercept implements ServerInterceptor.

Directories

Path Synopsis
Package rpcheader is a helper package.
Package rpcheader is a helper package.

Jump to

Keyboard shortcuts

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