statusx

package
v0.0.0-...-cd938e1 Latest Latest
Warning

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

Go to latest
Published: Mar 25, 2020 License: MIT Imports: 11 Imported by: 0

README

statusx

本包封装status.Status, 但目前只表达 grpc status 的错误状态,便于服务端组装错误

Documentation

Overview

Example (It_is_error)
package main

import (
	"fmt"
	"github.com/chen56/go-common/grpc/statusx"
)

func main() {
	var err error = statusx.NotFound("user不存在: %s", "chen").Err()
	fmt.Println(err)

}
Output:

NotFound: user不存在: chen
Example (RetryInfo)
package main

import (
	"context"
	"fmt"
	"github.com/chen56/go-common/grpc/statusx"
	"github.com/golang/protobuf/ptypes/duration"
	"github.com/grpc-ecosystem/grpc-gateway/runtime"
	"google.golang.org/genproto/googleapis/rpc/errdetails"
	"net/http"
	"net/http/httptest"
)

func main() {
	fmt.Println(toJson(statusx.Unavailable("暂时不能服务,稍后再试").WithRetryInfo(&errdetails.RetryInfo{
		RetryDelay: &duration.Duration{
			Seconds: 1,
		},
	})))
}

func toJson(err *statusx.Status) string {
	ctx := context.Background()

	w := httptest.NewRecorder()
	req, _ := http.NewRequest("", "", nil)
	runtime.DefaultHTTPError(ctx, &runtime.ServeMux{}, &runtime.JSONPb{}, w, req, err.Err())
	return w.Body.String()
}
Output:

{"error":"暂时不能服务,稍后再试","message":"暂时不能服务,稍后再试","code":14,"details":[{"@type":"type.googleapis.com/google.rpc.RetryInfo","retryDelay":"1s"}]}

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Try

func Try(f func() error) (err error)

Types

type Status

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

func Aborted

func Aborted(msgAndArgs ...interface{}) *Status

func AlreadyExists

func AlreadyExists(msgAndArgs ...interface{}) *Status

func Canceled

func Canceled(msgAndArgs ...interface{}) *Status

func DataLoss

func DataLoss(msgAndArgs ...interface{}) *Status

func DeadlineExceeded

func DeadlineExceeded(msgAndArgs ...interface{}) *Status

func FailedPrecondition

func FailedPrecondition(msgAndArgs ...interface{}) *Status

func FromError

func FromError(err error) *Status

func FromStatus

func FromStatus(status *status.Status) *Status

func Internal

func Internal(msgAndArgs ...interface{}) *Status

func InvalidArgument

func InvalidArgument(msgAndArgs ...interface{}) *Status

func MapIfStdError

func MapIfStdError(err error, newError func(err error) *Status) *Status

MapIfStdError 如果参数err是普通的error(而不是errorsg包的Error),则 用newError转换为新error

func NotFound

func NotFound(msgAndArgs ...interface{}) *Status

func OutOfRange

func OutOfRange(msgAndArgs ...interface{}) *Status

func PermissionDenied

func PermissionDenied(msgAndArgs ...interface{}) *Status

func ResourceExhausted

func ResourceExhausted(msgAndArgs ...interface{}) *Status

func Unauthenticated

func Unauthenticated(msgAndArgs ...interface{}) *Status

func Unavailable

func Unavailable(msgAndArgs ...interface{}) *Status

func Unimplemented

func Unimplemented(msgAndArgs ...interface{}) *Status

func Unknown

func Unknown(msgAndArgs ...interface{}) *Status

func (*Status) Code

func (x *Status) Code() codes.Code

func (*Status) Err

func (x *Status) Err() error

to error interface

func (*Status) GRPCStatus

func (x *Status) GRPCStatus() *status.Status

func (*Status) Message

func (x *Status) Message() string

func (*Status) RetryInfo

func (x *Status) RetryInfo() *errdetails.RetryInfo

func (*Status) WithDebugError

func (x *Status) WithDebugError(err error) *Status

func (*Status) WithDebugMessage

func (x *Status) WithDebugMessage(debugMsgAndArgs ...interface{}) *Status

func (*Status) WithDetails

func (x *Status) WithDetails(details ...proto.Message) *Status

func (*Status) WithRetryInfo

func (x *Status) WithRetryInfo(retryInfo *errdetails.RetryInfo) *Status

Jump to

Keyboard shortcuts

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