Documentation ¶
Overview ¶
Package unierr presents an unification error model between gRPC transport and HTTP transport, and between server and client.
It is modeled after the gRPC status.
To create an not found error with a custom message:
unierr.New(codes.NotFound, "some stuff is missing")
To wrap an existing error:
unierr.Wrap(err, codes.NotFound)
See example for detailed usage.
Example ¶
package main import ( "errors" "fmt" "github.com/DoNewsCode/core/unierr" "google.golang.org/grpc/codes" ) func main() { fmt.Printf("Default status conversion:\n") for i := 1; i < 17; i++ { err := unierr.Wrap(errors.New(""), codes.Code(i)) fmt.Printf("GRPC %d <=> HTTP: %d\n", err.GRPCStatus().Code(), err.StatusCode()) } }
Output: Default status conversion: GRPC 1 <=> HTTP: 499 GRPC 2 <=> HTTP: 500 GRPC 3 <=> HTTP: 400 GRPC 4 <=> HTTP: 504 GRPC 5 <=> HTTP: 404 GRPC 6 <=> HTTP: 409 GRPC 7 <=> HTTP: 403 GRPC 8 <=> HTTP: 429 GRPC 9 <=> HTTP: 400 GRPC 10 <=> HTTP: 409 GRPC 11 <=> HTTP: 400 GRPC 12 <=> HTTP: 501 GRPC 13 <=> HTTP: 500 GRPC 14 <=> HTTP: 500 GRPC 15 <=> HTTP: 500 GRPC 16 <=> HTTP: 401
Index ¶
- func IsAbortedErr(e error) bool
- func IsAlreadyExistsErr(e error) bool
- func IsCanceledErr(e error) bool
- func IsDataLossErr(e error) bool
- func IsDeadlineExceededErr(e error) bool
- func IsFailedPreconditionErr(e error) bool
- func IsInternalErr(e error) bool
- func IsInvalidArgumentErr(e error) bool
- func IsNotFoundErr(e error) bool
- func IsOutOfRangeErr(e error) bool
- func IsPermissionDeniedErr(e error) bool
- func IsResourceExhaustedErr(e error) bool
- func IsUnauthenticatedErr(e error) bool
- func IsUnavailableErr(e error) bool
- func IsUnimplementedErr(e error) bool
- func IsUnknownErr(e error) bool
- type Error
- func AbortedErr(e error, msgAndArgs ...interface{}) *Error
- func AlreadyExistsErr(e error, msgAndArgs ...interface{}) *Error
- func CanceledErr(e error, msgAndArgs ...interface{}) *Error
- func DataLossErr(e error, msgAndArgs ...interface{}) *Error
- func DeadlineExceededErr(e error, msgAndArgs ...interface{}) *Error
- func FailedPreconditionErr(e error, msgAndArgs ...interface{}) *Error
- func FromStatus(s *status.Status) *Error
- func InternalErr(e error, msgAndArgs ...interface{}) *Error
- func InvalidArgumentErr(e error, msgAndArgs ...interface{}) *Error
- func New(code codes.Code, msg string) *Error
- func Newf(code codes.Code, format string, args ...interface{}) *Error
- func NotFoundErr(e error, msgAndArgs ...interface{}) *Error
- func OutOfRangeErr(e error, msgAndArgs ...interface{}) *Error
- func PermissionDeniedErr(e error, msgAndArgs ...interface{}) *Error
- func ResourceExhaustedErr(e error, msgAndArgs ...interface{}) *Error
- func UnauthenticatedErr(e error, msgAndArgs ...interface{}) *Error
- func UnavailableErr(e error, msgAndArgs ...interface{}) *Error
- func UnimplementedErr(e error, msgAndArgs ...interface{}) *Error
- func UnknownErr(e error) *Error
- func Wrap(err error, code codes.Code) *Error
- func Wrapf(err error, code codes.Code, format string, args ...interface{}) *Error
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func IsAbortedErr ¶
IsAbortedErr checks if an Error has codes.Aborted
func IsAlreadyExistsErr ¶
IsAlreadyExistsErr checks if an Error has codes.AlreadyExists
func IsCanceledErr ¶
IsCanceledErr checks if an Error has codes.Canceled
func IsDataLossErr ¶
IsDataLossErr checks if an Error has codes.DataLoss
func IsDeadlineExceededErr ¶
IsDeadlineExceededErr checks if an Error has codes.DeadlineExceeded
func IsFailedPreconditionErr ¶
IsFailedPreconditionErr checks if an Error has codes.FailedPrecondition
func IsInternalErr ¶
IsInternalErr checks if an Error has codes.Internal
func IsInvalidArgumentErr ¶
IsInvalidArgumentErr checks if an Error has codes.InvalidArgument
func IsNotFoundErr ¶
IsNotFoundErr checks if an Error has codes.NotFound
func IsOutOfRangeErr ¶
IsOutOfRangeErr checks if an Error has codes.OutOfRange
func IsPermissionDeniedErr ¶
IsPermissionDeniedErr checks if an Error has codes.PermissionDenied
func IsResourceExhaustedErr ¶
IsResourceExhaustedErr checks if an Error has codes.ResourceExhausted
func IsUnauthenticatedErr ¶
IsUnauthenticatedErr checks if an Error has codes.Unauthenticated
func IsUnavailableErr ¶
IsUnavailableErr checks if an Error has codes.Unavailable
func IsUnimplementedErr ¶
IsUnimplementedErr checks if an Error has codes.Unimplemented
func IsUnknownErr ¶
IsUnknownErr checks if an Error has codes.Unknown
Types ¶
type Error ¶
type Error struct { // Printer can ben used to achieve i18n. By default it is a text.BasePrinter. Printer contract.Printer // HttpStatusCodeFunc can overwrites the inferred HTTP status code from gRPC status. HttpStatusCodeFunc func(code codes.Code) int // contains filtered or unexported fields }
Error is the unified error type for HTTP/gRPC transports. In grpc transports, Error can not only be constructed from a grpc status but also producing a native grpc status. In HTTP transports, Error can be encoded and decoded in json format. It also infers HTTP status code.
The roundtrip conversion makes Error suitable as a unification error model, on both client side and server side. Note the json format follows the JSONRPC standard.
func AbortedErr ¶
AbortedErr creates an Error with codes.Aborted
func AlreadyExistsErr ¶
AlreadyExistsErr creates an Error with codes.AlreadyExists
func CanceledErr ¶
CanceledErr creates an Error with codes.Canceled
func DataLossErr ¶
DataLossErr creates an Error with codes.DataLoss
func DeadlineExceededErr ¶
DeadlineExceededErr creates an Error with codes.DeadlineExceeded
func FailedPreconditionErr ¶
FailedPreconditionErr creates an Error with codes.FailedPrecondition
func FromStatus ¶
FromStatus constructs the Error from a gRPC status.
func InternalErr ¶
InternalErr creates an Error with codes.Internal
func InvalidArgumentErr ¶
InvalidArgumentErr creates an Error with codes.InvalidArgument
func NotFoundErr ¶
NotFoundErr creates an Error with codes.NotFound
func OutOfRangeErr ¶
OutOfRangeErr creates an Error with codes.OutOfRange
func PermissionDeniedErr ¶
PermissionDeniedErr creates an Error with codes.PermissionDenied
func ResourceExhaustedErr ¶
ResourceExhaustedErr creates an Error with codes.ResourceExhausted
func UnauthenticatedErr ¶
UnauthenticatedErr creates an Error with codes.Unauthenticated
func UnavailableErr ¶
UnavailableErr creates an Error with codes.Unavailable
func UnimplementedErr ¶
UnimplementedErr creates an Error with codes.Unimplemented
func Wrapf ¶
Wrapf annotates an error with a codes.Code, and provides a new error message. The wrapped error hence is mainly kept for tracing and debugging. The message in the wrapped error becomes irrelevant as it is overwritten by the new message.
func (*Error) GRPCStatus ¶
GRPCStatus produces a native gRPC status.
Example ¶
package main import ( "errors" "fmt" "github.com/DoNewsCode/core/unierr" "google.golang.org/grpc/codes" ) func main() { err := errors.New("my stuff is missing") unifiedError := unierr.Wrap(err, codes.NotFound) grpcStatus := unifiedError.GRPCStatus() fmt.Println(grpcStatus.Code()) fmt.Println(grpcStatus.Message()) }
Output: NotFound my stuff is missing
func (*Error) MarshalJSON ¶
MarshalJSON implements json.Marshaler.
func (*Error) StackTrace ¶
func (e *Error) StackTrace() errors.StackTrace
StackTrace implements the interface of errors.Wrap()
func (*Error) StatusCode ¶
StatusCode infers the correct http status corresponding to Error's internal code. If a HttpStatusCode is set in Error, that status code will be used instead.
Example ¶
package main import ( "errors" "fmt" "github.com/DoNewsCode/core/unierr" "google.golang.org/grpc/codes" ) func main() { err := errors.New("my stuff is missing") unifiedError := unierr.Wrap(err, codes.NotFound) httpStatus := unifiedError.StatusCode() fmt.Println(httpStatus) bytes, _ := unifiedError.MarshalJSON() fmt.Println(string(bytes)) }
Output: 404 {"code":5,"message":"my stuff is missing"}
func (*Error) UnmarshalJSON ¶
UnmarshalJSON implements json.Unmarshaler.