Documentation ¶
Overview ¶
Package rpc provides RPC functionality.
Index ¶
- func GetErrorMessage(err error) string
- func GetIncomingHeader(ctx context.Context, key string) string
- func GetIncomingHeaders(ctx context.Context) map[string]string
- func GetOutgoingHeader(ctx context.Context, key string) string
- func GetOutgoingHeaders(ctx context.Context) map[string]string
- func IsError(err error) bool
- func NewAbortedError(message string) error
- func NewAbortedErrorf(format string, args ...interface{}) error
- func NewAlreadyExistsError(message string) error
- func NewAlreadyExistsErrorf(format string, args ...interface{}) error
- func NewCanceledError(message string) error
- func NewCanceledErrorf(format string, args ...interface{}) error
- func NewDataLossError(message string) error
- func NewDataLossErrorf(format string, args ...interface{}) error
- func NewDeadlineExceededError(message string) error
- func NewDeadlineExceededErrorf(format string, args ...interface{}) error
- func NewError(errorCode ErrorCode, message string) error
- func NewErrorf(errorCode ErrorCode, format string, args ...interface{}) error
- func NewFailedPreconditionError(message string) error
- func NewFailedPreconditionErrorf(format string, args ...interface{}) error
- func NewInternalError(message string) error
- func NewInternalErrorf(format string, args ...interface{}) error
- func NewInvalidArgumentError(message string) error
- func NewInvalidArgumentErrorf(format string, args ...interface{}) error
- func NewNotFoundError(message string) error
- func NewNotFoundErrorf(format string, args ...interface{}) error
- func NewOutOfRangeError(message string) error
- func NewOutOfRangeErrorf(format string, args ...interface{}) error
- func NewPermissionDeniedError(message string) error
- func NewPermissionDeniedErrorf(format string, args ...interface{}) error
- func NewResourceExhaustedError(message string) error
- func NewResourceExhaustedErrorf(format string, args ...interface{}) error
- func NewUnauthenticatedError(message string) error
- func NewUnauthenticatedErrorf(format string, args ...interface{}) error
- func NewUnavailableError(message string) error
- func NewUnavailableErrorf(format string, args ...interface{}) error
- func NewUnimplementedError(message string) error
- func NewUnimplementedErrorf(format string, args ...interface{}) error
- func NewUnknownError(message string) error
- func NewUnknownErrorf(format string, args ...interface{}) error
- func WithIncomingHeader(ctx context.Context, key string, value string) context.Context
- func WithIncomingHeaders(ctx context.Context, headers map[string]string) context.Context
- func WithOutgoingHeader(ctx context.Context, key string, value string) context.Context
- func WithOutgoingHeaders(ctx context.Context, headers map[string]string) context.Context
- type ErrorCode
- type ServerHandler
- type ServerHandlerFunc
- type ServerInfo
- type ServerInterceptor
- type ServerInterceptorFunc
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func GetErrorMessage ¶ added in v1.0.0
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
IsError returns true if err is an error created by this package.
If the error is nil, this returns false.
func NewAbortedError ¶
NewAbortedError is a convenience function for errors with ErrorCodeAborted.
func NewAbortedErrorf ¶
NewAbortedErrorf is a convenience function for errors with ErrorCodeAborted.
func NewAlreadyExistsError ¶
NewAlreadyExistsError is a convenience function for errors with ErrorCodeAlreadyExists.
func NewAlreadyExistsErrorf ¶
NewAlreadyExistsErrorf is a convenience function for errors with ErrorCodeAlreadyExists.
func NewCanceledError ¶
NewCanceledError is a convenience function for errors with ErrorCodeCanceled.
func NewCanceledErrorf ¶
NewCanceledErrorf is a convenience function for errors with ErrorCodeCanceled.
func NewDataLossError ¶
NewDataLossError is a convenience function for errors with ErrorCodeDataLoss.
func NewDataLossErrorf ¶
NewDataLossErrorf is a convenience function for errors with ErrorCodeDataLoss.
func NewDeadlineExceededError ¶
NewDeadlineExceededError is a convenience function for errors with ErrorCodeDeadlineExceeded.
func NewDeadlineExceededErrorf ¶
NewDeadlineExceededErrorf is a convenience function for errors with ErrorCodeDeadlineExceeded.
func NewError ¶
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 ¶
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 ¶
NewFailedPreconditionError is a convenience function for errors with ErrorCodeFailedPrecondition.
func NewFailedPreconditionErrorf ¶
NewFailedPreconditionErrorf is a convenience function for errors with ErrorCodeFailedPrecondition.
func NewInternalError ¶
NewInternalError is a convenience function for errors with ErrorCodeInternal.
func NewInternalErrorf ¶
NewInternalErrorf is a convenience function for errors with ErrorCodeInternal.
func NewInvalidArgumentError ¶
NewInvalidArgumentError is a convenience function for errors with ErrorCodeInvalidArgument.
func NewInvalidArgumentErrorf ¶
NewInvalidArgumentErrorf is a convenience function for errors with ErrorCodeInvalidArgument.
func NewNotFoundError ¶
NewNotFoundError is a convenience function for errors with ErrorCodeNotFound.
func NewNotFoundErrorf ¶
NewNotFoundErrorf is a convenience function for errors with ErrorCodeNotFound.
func NewOutOfRangeError ¶
NewOutOfRangeError is a convenience function for errors with ErrorCodeOutOfRange.
func NewOutOfRangeErrorf ¶
NewOutOfRangeErrorf is a convenience function for errors with ErrorCodeOutOfRange.
func NewPermissionDeniedError ¶
NewPermissionDeniedError is a convenience function for errors with ErrorCodePermissionDenied.
func NewPermissionDeniedErrorf ¶
NewPermissionDeniedErrorf is a convenience function for errors with ErrorCodePermissionDenied.
func NewResourceExhaustedError ¶
NewResourceExhaustedError is a convenience function for errors with ErrorCodeResourceExhausted.
func NewResourceExhaustedErrorf ¶
NewResourceExhaustedErrorf is a convenience function for errors with ErrorCodeResourceExhausted.
func NewUnauthenticatedError ¶
NewUnauthenticatedError is a convenience function for errors with ErrorCodeUnauthenticated.
func NewUnauthenticatedErrorf ¶
NewUnauthenticatedErrorf is a convenience function for errors with ErrorCodeUnauthenticated.
func NewUnavailableError ¶
NewUnavailableError is a convenience function for errors with ErrorCodeUnavailable.
func NewUnavailableErrorf ¶
NewUnavailableErrorf is a convenience function for errors with ErrorCodeUnavailable.
func NewUnimplementedError ¶
NewUnimplementedError is a convenience function for errors with ErrorCodeUnimplemented.
func NewUnimplementedErrorf ¶
NewUnimplementedErrorf is a convenience function for errors with ErrorCodeUnimplemented.
func NewUnknownError ¶
NewUnknownError is a convenience function for errors with ErrorCodeUnknown.
func NewUnknownErrorf ¶
NewUnknownErrorf is a convenience function for errors with ErrorCodeUnknown.
func WithIncomingHeader ¶
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 ¶
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 ¶
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 ¶
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 // 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 ¶
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.
type ServerHandler ¶
type ServerHandler interface {
Handle(ctx context.Context, request interface{}) (interface{}, error)
}
ServerHandler is a server handler.
type ServerHandlerFunc ¶
ServerHandlerFunc is a function that 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.