unierr

package
v0.12.3 Latest Latest
Warning

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

Go to latest
Published: Mar 23, 2022 License: MIT Imports: 8 Imported by: 3

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

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func IsAbortedErr

func IsAbortedErr(e error) bool

IsAbortedErr checks if an Error has codes.Aborted

func IsAlreadyExistsErr

func IsAlreadyExistsErr(e error) bool

IsAlreadyExistsErr checks if an Error has codes.AlreadyExists

func IsCanceledErr

func IsCanceledErr(e error) bool

IsCanceledErr checks if an Error has codes.Canceled

func IsDataLossErr

func IsDataLossErr(e error) bool

IsDataLossErr checks if an Error has codes.DataLoss

func IsDeadlineExceededErr

func IsDeadlineExceededErr(e error) bool

IsDeadlineExceededErr checks if an Error has codes.DeadlineExceeded

func IsFailedPreconditionErr

func IsFailedPreconditionErr(e error) bool

IsFailedPreconditionErr checks if an Error has codes.FailedPrecondition

func IsInternalErr

func IsInternalErr(e error) bool

IsInternalErr checks if an Error has codes.Internal

func IsInvalidArgumentErr

func IsInvalidArgumentErr(e error) bool

IsInvalidArgumentErr checks if an Error has codes.InvalidArgument

func IsNotFoundErr

func IsNotFoundErr(e error) bool

IsNotFoundErr checks if an Error has codes.NotFound

func IsOutOfRangeErr

func IsOutOfRangeErr(e error) bool

IsOutOfRangeErr checks if an Error has codes.OutOfRange

func IsPermissionDeniedErr

func IsPermissionDeniedErr(e error) bool

IsPermissionDeniedErr checks if an Error has codes.PermissionDenied

func IsResourceExhaustedErr

func IsResourceExhaustedErr(e error) bool

IsResourceExhaustedErr checks if an Error has codes.ResourceExhausted

func IsUnauthenticatedErr

func IsUnauthenticatedErr(e error) bool

IsUnauthenticatedErr checks if an Error has codes.Unauthenticated

func IsUnavailableErr

func IsUnavailableErr(e error) bool

IsUnavailableErr checks if an Error has codes.Unavailable

func IsUnimplementedErr

func IsUnimplementedErr(e error) bool

IsUnimplementedErr checks if an Error has codes.Unimplemented

func IsUnknownErr

func IsUnknownErr(e error) bool

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

func AbortedErr(e error, msgAndArgs ...interface{}) *Error

AbortedErr creates an Error with codes.Aborted

func AlreadyExistsErr

func AlreadyExistsErr(e error, msgAndArgs ...interface{}) *Error

AlreadyExistsErr creates an Error with codes.AlreadyExists

func CanceledErr

func CanceledErr(e error, msgAndArgs ...interface{}) *Error

CanceledErr creates an Error with codes.Canceled

func DataLossErr

func DataLossErr(e error, msgAndArgs ...interface{}) *Error

DataLossErr creates an Error with codes.DataLoss

func DeadlineExceededErr

func DeadlineExceededErr(e error, msgAndArgs ...interface{}) *Error

DeadlineExceededErr creates an Error with codes.DeadlineExceeded

func FailedPreconditionErr

func FailedPreconditionErr(e error, msgAndArgs ...interface{}) *Error

FailedPreconditionErr creates an Error with codes.FailedPrecondition

func FromStatus

func FromStatus(s *status.Status) *Error

FromStatus constructs the Error from a gRPC status.

func InternalErr

func InternalErr(e error, msgAndArgs ...interface{}) *Error

InternalErr creates an Error with codes.Internal

func InvalidArgumentErr

func InvalidArgumentErr(e error, msgAndArgs ...interface{}) *Error

InvalidArgumentErr creates an Error with codes.InvalidArgument

func New

func New(code codes.Code, msg string) *Error

New returns an error representing code and msg. If code is OK, returns nil.

func Newf

func Newf(code codes.Code, format string, args ...interface{}) *Error

Newf returns New(code, fmt.Sprintf(format, args...)).

func NotFoundErr

func NotFoundErr(e error, msgAndArgs ...interface{}) *Error

NotFoundErr creates an Error with codes.NotFound

func OutOfRangeErr

func OutOfRangeErr(e error, msgAndArgs ...interface{}) *Error

OutOfRangeErr creates an Error with codes.OutOfRange

func PermissionDeniedErr

func PermissionDeniedErr(e error, msgAndArgs ...interface{}) *Error

PermissionDeniedErr creates an Error with codes.PermissionDenied

func ResourceExhaustedErr

func ResourceExhaustedErr(e error, msgAndArgs ...interface{}) *Error

ResourceExhaustedErr creates an Error with codes.ResourceExhausted

func UnauthenticatedErr

func UnauthenticatedErr(e error, msgAndArgs ...interface{}) *Error

UnauthenticatedErr creates an Error with codes.Unauthenticated

func UnavailableErr

func UnavailableErr(e error, msgAndArgs ...interface{}) *Error

UnavailableErr creates an Error with codes.Unavailable

func UnimplementedErr

func UnimplementedErr(e error, msgAndArgs ...interface{}) *Error

UnimplementedErr creates an Error with codes.Unimplemented

func UnknownErr

func UnknownErr(e error) *Error

UnknownErr creates an Error with codes.Unknown.

func Wrap

func Wrap(err error, code codes.Code) *Error

Wrap annotates an error with a codes.Code

func Wrapf

func Wrapf(err error, code codes.Code, format string, args ...interface{}) *Error

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) Error

func (e *Error) Error() string

Error implements error. it consults the Printer for the output.

func (*Error) GRPCStatus

func (e *Error) GRPCStatus() *status.Status

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

func (e *Error) MarshalJSON() (result []byte, err error)

MarshalJSON implements json.Marshaler.

func (*Error) StackTrace

func (e *Error) StackTrace() errors.StackTrace

StackTrace implements the interface of errors.Measure()

func (*Error) StatusCode

func (e *Error) StatusCode() int

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

func (e *Error) UnmarshalJSON(bytes []byte) error

UnmarshalJSON implements json.Unmarshaler.

func (*Error) Unwrap

func (e *Error) Unwrap() error

Unwrap implements go's standard errors.Unwrap() interface

Jump to

Keyboard shortcuts

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