grpcweberr

package module
v0.0.0-...-26e09a7 Latest Latest
Warning

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

Go to latest
Published: Oct 28, 2019 License: MIT Imports: 6 Imported by: 0

README

Package for easy creation & reading of GRPC errors with added data for web clients

GRPCWebError was built to solve the problem of how to transfer descriptive errors to web clients through microservices which communicate using GRPC, in order to achieve that there are 2 steps which need to be taken:

The first step is where an error is created:

if 1 != 0 {
    gwe := grpcweberr.New()
    return gwe.New(codes.InvalidArgument, 422, "Received invalid values")
}

While the error is being transferred through the system it can just be handled like a simple error.

When its time to prepare a response to the web client the following functions can be used to retrieve the error data:

gwe := grpcweberr.New()
httpStatus := gwe.GetHTTPStatus(err)
errMsg := gwe.GetUserErrorMessage(err)

There is also a way to track and log specific errors which travel through the microservices, for that, first append a logTracingID to the error:

if 1 != 0 {
    gwe := grpcweberr.New()
    err := gwe.New(codes.InvalidArgument, 422, "Received invalid values")
    return gwe.AddLogTracingID(tracingID, err)
}

Where tracingID is some string which can be used to track errors across the microservices.

Later at specific points where u may want to log an error:

gwe := grpcweberr.New()
if logID := gwe.GetLogTracingID(err); logID != "" {
    // some logging code... here I use sdlog but its up to u
    sdlog.New().Info("Error of Info lvl from X service", sdlog.AddLogTracingID(logID), sdlog.Lbl("err", err))
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type GRPCWebErr

type GRPCWebErr struct{}

GRPCWebErr serve as a receiver in order to allow mocking

func New

func New() *GRPCWebErr

New is a factory method for GRPCWebErr struct

func (*GRPCWebErr) AddLogTracingID

func (*GRPCWebErr) AddLogTracingID(logTracingID string, err error) error

AddLogTracingID append logTracingID value to the error so it could be tracked as it flow through the services

func (*GRPCWebErr) GetHTTPStatus

func (*GRPCWebErr) GetHTTPStatus(err error) int

GetHTTPStatus is a getter for the http value which was supplied at New(...)

func (*GRPCWebErr) GetLogTracingID

func (*GRPCWebErr) GetLogTracingID(err error) string

GetLogTracingID is a getter for logTracingId

func (*GRPCWebErr) GetMessageToUser

func (*GRPCWebErr) GetMessageToUser(err error) string

GetMessageToUser is a getter for the messageToUser which was supplied at New(...)

func (*GRPCWebErr) New

func (*GRPCWebErr) New(grpcStatusCode codes.Code, httpStatusCode int, messageToUser string) error

New (with a receiver pointer) create a new error with embedded data of status codes and message

grpcStatusCode codes.Code - import "google.golang.org/grpc/codes" httpStatusCode int - http status code to return messageToUser string - intended message returned to client, get default msg if empty

Jump to

Keyboard shortcuts

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