Documentation ¶
Overview ¶
Package apperr defines an error that can distinguish between a client error and a server error. It also defines a function that extracts the defined client or server error from the wrapped error.
Example ¶
package main import ( "fmt" "log" "github.com/takuoki/golib/apperr" "google.golang.org/grpc/codes" ) func main() { // Buisiness logic err := func(id string) error { if id == "" { return IDRequired } return NotFound }("id1") // Error handling e, ok := apperr.Extract(err) if !ok { e = NewInternalServerError(err) } if e.Log() != "" { log.Println(e.Log()) } resp := struct { Code string Message string }{ Code: e.DetailCode(), Message: e.Message(), } fmt.Printf("%+v", resp) } var ( NotFound = apperr.NewClientError(codes.NotFound, "E0001", "not found") IDRequired = apperr.NewClientError(codes.InvalidArgument, "E0002", "id is required") ) func NewInternalServerError(err error) apperr.Err { return apperr.NewServerError( codes.Internal, "S0001", "internal server error", err.Error(), ) }
Output: {Code:E0001 Message:not found}
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Err ¶
type Err interface { Error() string Code() codes.Code DetailCode() string Message() string Log() string Type() Type HTTPStatus() int GRPCError(domain string) error }
Err is a interface that represents an error.
func ExtractFromGRPCError ¶
ExtractFromGRPCError is a function to extract apperr.Err from a gRPC error.
Example ¶
package main import ( "errors" "fmt" "github.com/takuoki/golib/apperr" "google.golang.org/grpc/codes" ) func main() { callGrpcAPI := func(e apperr.Err) error { return e.GRPCError("domain") } // Client error err := callGrpcAPI(NotFound) if e, ok := apperr.ExtractFromGRPCError(err); ok { fmt.Printf("Code: %s, DetailCode: %s, Message: %s\n", e.Code(), e.DetailCode(), e.Message()) } // Server error err = callGrpcAPI(NewInternalServerError(errors.New("error"))) if e, ok := apperr.ExtractFromGRPCError(err); ok { fmt.Printf("Code: %s, DetailCode: %s, Message: %s\n", e.Code(), e.DetailCode(), e.Message()) } // nil error err = nil if e, ok := apperr.ExtractFromGRPCError(err); ok { fmt.Printf("Code: %s, DetailCode: %s, Message: %s\n", e.Code(), e.DetailCode(), e.Message()) } // non-gRPC error err = errors.New("error") if e, ok := apperr.ExtractFromGRPCError(err); ok { fmt.Printf("Code: %s, DetailCode: %s, Message: %s\n", e.Code(), e.DetailCode(), e.Message()) } } var NotFound = apperr.NewClientError(codes.NotFound, "E0001", "not found") func NewInternalServerError(err error) apperr.Err { return apperr.NewServerError( codes.Internal, "S0001", "internal server error", err.Error(), ) }
Output: Code: NotFound, DetailCode: E0001, Message: not found Code: Internal, DetailCode: S0001, Message: internal server error
func NewClientError ¶
NewClientError creates new client error. Set the gRPC code to `code`. Set the error detail code (ex. "E0001") that the client can handle to `detailCode`.
Click to show internal directories.
Click to hide internal directories.