hrpc

package module
v0.0.0-...-f4bd0a6 Latest Latest
Warning

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

Go to latest
Published: Aug 1, 2020 License: MIT Imports: 19 Imported by: 0

README

Requirements:

go : minimum version 1.13

Install

go get github.com/Kamva/hexa-rpc

Proposal

  • Change Hexa error status from http status to gRPC status, because:

    Advantages

    • If we using gRPC status, so will dont need to convert https status to gRPC status, we will just convert our gRPC status to http on the gateway responses.

    Drawbacks

    • If we use gRPC codes, so we will need to import gRPC library in all of libraries that need to define or use hexa error, while http statues exists in most languages as standard libraries.

Todo

  • Use recover interceptor in the gRPC interceptors.
  • Implement status to Hexa error (and reverse) mapper.
  • Set Hexa logger as gRPC Logger (implement gRPC logger adapter by hexa logger)
  • Implement request logger (log start-time, end-time, method, error,...)
  • We should implement all of our interceptors for the Stream request/responses also (for now we just support Unary Request/responses).
  • Write Tests
  • Add badges to README.
  • CI

Documentation

Index

Constants

View Source
const ContextKeyHexaCtx = "__hexa_ctx__"

ContextKeyHexaCtx is the identifier to set the hexa context as a field in the context of a gRPC method.

View Source
const GRPCLogVerbosityLevel = "GRPC_GO_LOG_VERBOSITY_LEVEL"

Config name for log verbosity v

Variables

View Source
var (
	ErrInvalidHexaContextPayload = status.Error(codes.Internal, "invalid hexa context payload provided to json marshaller")
)
View Source
var (
	ErrUnknownError = hexa.NewError(http.StatusInternalServerError, "hrpc.u.e.0", hexa.ErrKeyInternalError, nil)
)

Functions

func CodeFromHTTPStatus

func CodeFromHTTPStatus(status int) codes.Code

CodeFromHTTPStatus converts a https status into corresponding gRPC error code. Note: error mapping from http status to hRPC code is not good, do not use this function as you can.

func Ctx

func Ctx(ctx hexa.Context) context.Context

Ctx gets Hexa context and embed it in a go context to pass to the gRPC methods.

func DurationToTimeMillisFormatter

func DurationToTimeMillisFormatter(duration time.Duration) hexa.Map

DurationToTimeMillisFormatter converts the duration to milliseconds.

func Error

func Error(status *status.Status) hexa.Error

Error gets a gRPC status and converts it to Hexa error

func HTTPStatusFromCode

func HTTPStatusFromCode(code codes.Code) int

HTTPStatusFromCode converts a gRPC error code into the corresponding HTTP response status. See: https://github.com/googleapis/googleapis/blob/master/google/rpc/code.proto Note: We got this function from the [gRPC gateway](https://github.com/grpc-ecosystem/grpc-gateway/blob/master/runtime/errors.go)

func MaskMapPaths

func MaskMapPaths(m hexa.Map, mask *field_mask.FieldMask, depth int)

MaskMapPaths mask all paths in the provided map with the provided depth.

func NewLogger

func NewLogger(l hexa.Logger, cfg hexa.Config) grpclog.LoggerV2

NewLogger returns new instance of the gRPC Logger v2

func RecoverHandler

func RecoverHandler(r interface{}) error

RecoverHandler handle handle recovered data from panics in the gRPC server

func Status

func Status(hexaErr hexa.Error, t hexa.Translator) *status.Status

Status gets a Hexa error and converts it to gRPC Status Implementation Details: - Convert http status to gRPC code - Set localized message and data.

Types

type DurationFormatter

type DurationFormatter func(duration time.Duration) hexa.Map

DurationFunc get a duration and return formatted duration as key (name of field that should log) and value(formatted time)

type ErrInterceptorOptions

type ErrInterceptorOptions struct {
	Logger       hexa.Logger
	Translator   hexa.Translator
	ReportErrors bool // report errors ?
}

ErrInterceptorOptions is the options

type ErrorDetails

type ErrorDetails struct {
	// HTTP status
	Status int32 `protobuf:"varint,1,opt,name=status,proto3" json:"status,omitempty"`
	// Error code
	Code string `protobuf:"bytes,2,opt,name=code,proto3" json:"code,omitempty"`
	// Localized error message
	LocalizedMessage string `protobuf:"bytes,3,opt,name=localized_message,json=localizedMessage,proto3" json:"localized_message,omitempty"`
	// json marshalled error data
	Data                 string   `protobuf:"bytes,4,opt,name=data,proto3" json:"data,omitempty"`
	XXX_NoUnkeyedLiteral struct{} `json:"-"`
	XXX_unrecognized     []byte   `json:"-"`
	XXX_sizecache        int32    `json:"-"`
}

--------------------------------

Hexa error message

-------------------------------- Go Generation command:

func (*ErrorDetails) Descriptor

func (*ErrorDetails) Descriptor() ([]byte, []int)

func (*ErrorDetails) GetCode

func (m *ErrorDetails) GetCode() string

func (*ErrorDetails) GetData

func (m *ErrorDetails) GetData() string

func (*ErrorDetails) GetLocalizedMessage

func (m *ErrorDetails) GetLocalizedMessage() string

func (*ErrorDetails) GetStatus

func (m *ErrorDetails) GetStatus() int32

func (*ErrorDetails) ProtoMessage

func (*ErrorDetails) ProtoMessage()

func (*ErrorDetails) Reset

func (m *ErrorDetails) Reset()

func (*ErrorDetails) String

func (m *ErrorDetails) String() string

func (*ErrorDetails) XXX_DiscardUnknown

func (m *ErrorDetails) XXX_DiscardUnknown()

func (*ErrorDetails) XXX_Marshal

func (m *ErrorDetails) XXX_Marshal(b []byte, deterministic bool) ([]byte, error)

func (*ErrorDetails) XXX_Merge

func (m *ErrorDetails) XXX_Merge(src proto.Message)

func (*ErrorDetails) XXX_Size

func (m *ErrorDetails) XXX_Size() int

func (*ErrorDetails) XXX_Unmarshal

func (m *ErrorDetails) XXX_Unmarshal(b []byte) error

type ErrorInterceptor

type ErrorInterceptor struct{}

ErrorInterceptor implements a gRPC interceptor to convert error into status and reverse.

func NewErrorInterceptor

func NewErrorInterceptor() *ErrorInterceptor

NewErrorInterceptor returns new instance of the ErrorInterceptor

func (ErrorInterceptor) UnaryClientInterceptor

func (i ErrorInterceptor) UnaryClientInterceptor() grpc.UnaryClientInterceptor

UnaryClientInterceptor returns client interceptor to convert status to Hexa error. Note: error interceptor must be first client interceptor.

func (ErrorInterceptor) UnaryServerInterceptor

UnaryServerInterceptor returns unary server interceptor to convert Hexa error to status.

type HexaContextInterceptor

type HexaContextInterceptor struct {
	// contains filtered or unexported fields
}

HexaContextInterceptor is the gRPC interceptor to pass hexa context through gRPC. Note: we do not provide stream interceptors, if you think need it, create PR or issue.

func NewHexaContextInterceptor

func NewHexaContextInterceptor(cei hexa.ContextExporterImporter) *HexaContextInterceptor

NewHexaContextInterceptor returns new instance of the HexaContextInterceptor.

func (*HexaContextInterceptor) UnaryClientInterceptor

func (ci *HexaContextInterceptor) UnaryClientInterceptor(ctx context.Context, method string, req interface{}, reply interface{}, cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error

func (*HexaContextInterceptor) UnaryServerInterceptor

func (ci *HexaContextInterceptor) UnaryServerInterceptor(c context.Context, req interface{}, info *grpc.UnaryServerInfo, h grpc.UnaryHandler) (interface{}, error)

type LoggerOptions

type LoggerOptions struct {
	ErrorToCode       grpc_logging.ErrorToCode
	ShouldLog         grpc_logging.Decider
	DurationFormatter DurationFormatter
	LogRequest        bool
	LogResponse       bool
}

func DefaultLoggerOptions

func DefaultLoggerOptions(logRequestResponse bool) LoggerOptions

type RequestLogger

type RequestLogger struct {
	// contains filtered or unexported fields
}

RequestLogger implements gRPC interceptor to log each request

func NewRequestLogger

func NewRequestLogger(l hexa.Logger) *RequestLogger

NewRequestLogger returns new instance of the RequestLogger

func (*RequestLogger) UnaryServerInterceptor

func (l *RequestLogger) UnaryServerInterceptor(o LoggerOptions) grpc.UnaryServerInterceptor

type Service

type Service struct {
}

Service is just a base struct to use in hexa services(it's optional and you can drop it).

func (Service) Ctx

func (r Service) Ctx(c context.Context) hexa.Context

Ctx method extract the hexa context from the context of a gRPC service

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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