mirror of
https://github.com/go-kratos/kratos.git
synced 2026-05-22 10:15:24 +02:00
+46
-27
@@ -4,6 +4,10 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"google.golang.org/genproto/googleapis/rpc/errdetails"
|
||||
"google.golang.org/grpc/codes"
|
||||
"google.golang.org/grpc/status"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -11,42 +15,66 @@ const (
|
||||
SupportPackageIsVersion1 = true
|
||||
)
|
||||
|
||||
// Error contains an error response from the server.
|
||||
// For more details see https://github.com/go-kratos/kratos/issues/858.
|
||||
// Error is describes the cause of the error with structured details.
|
||||
// For more details see https://github.com/googleapis/googleapis/blob/master/google/rpc/error_details.proto.
|
||||
type Error struct {
|
||||
Code int `json:"code"`
|
||||
Message string `json:"message"`
|
||||
s *status.Status
|
||||
|
||||
Domain string `json:"domain"`
|
||||
Reason string `json:"reason"`
|
||||
Metadata map[string]string `json:"metadata"`
|
||||
}
|
||||
|
||||
func (e *Error) Error() string {
|
||||
return fmt.Sprintf("error: code = %d message = %s", e.Code, e.Message)
|
||||
return fmt.Sprintf("error: domain = %s reason = %s", e.Domain, e.Reason)
|
||||
}
|
||||
|
||||
// GRPCStatus returns the Status represented by se.
|
||||
func (e *Error) GRPCStatus() *status.Status {
|
||||
s, err := e.s.WithDetails(&errdetails.ErrorInfo{
|
||||
Domain: e.Domain,
|
||||
Reason: e.Reason,
|
||||
Metadata: e.Metadata,
|
||||
})
|
||||
if err != nil {
|
||||
return e.s
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
// Is matches each error in the chain with the target value.
|
||||
func (e *Error) Is(err error) bool {
|
||||
if target := new(Error); errors.As(err, &target) {
|
||||
return target.Code == e.Code
|
||||
return target.Domain == e.Domain && target.Reason == e.Reason
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// WithMetadata with an MD formed by the mapping of key, value.
|
||||
func (e *Error) WithMetadata(md map[string]string) *Error {
|
||||
err := *e
|
||||
err.Metadata = md
|
||||
return &err
|
||||
}
|
||||
|
||||
// New returns an error object for the code, message.
|
||||
func New(code int, message string) *Error {
|
||||
func New(code codes.Code, domain, reason, message string) *Error {
|
||||
return &Error{
|
||||
Code: code,
|
||||
Message: message,
|
||||
s: status.New(codes.Code(code), message),
|
||||
Domain: domain,
|
||||
Reason: reason,
|
||||
}
|
||||
}
|
||||
|
||||
// Newf New(code fmt.Sprintf(format, a...))
|
||||
func Newf(code int, format string, a ...interface{}) *Error {
|
||||
return New(code, fmt.Sprintf(format, a...))
|
||||
func Newf(code codes.Code, domain, reason, format string, a ...interface{}) *Error {
|
||||
return New(code, domain, reason, fmt.Sprintf(format, a...))
|
||||
}
|
||||
|
||||
// Errorf returns an error object for the code, message and error info.
|
||||
func Errorf(code int, domain, reason, format string, a ...interface{}) *ErrorInfo {
|
||||
return &ErrorInfo{
|
||||
err: Newf(code, format, a...),
|
||||
func Errorf(code codes.Code, domain, reason, format string, a ...interface{}) error {
|
||||
return &Error{
|
||||
s: status.New(codes.Code(code), fmt.Sprintf(format, a...)),
|
||||
Domain: domain,
|
||||
Reason: reason,
|
||||
}
|
||||
@@ -54,12 +82,12 @@ func Errorf(code int, domain, reason, format string, a ...interface{}) *ErrorInf
|
||||
|
||||
// Code returns the code for a particular error.
|
||||
// It supports wrapped errors.
|
||||
func Code(err error) int {
|
||||
func Code(err error) codes.Code {
|
||||
if err == nil {
|
||||
return http.StatusOK
|
||||
}
|
||||
if target := new(Error); errors.As(err, &target) {
|
||||
return target.Code
|
||||
return target.s.Code()
|
||||
}
|
||||
return http.StatusInternalServerError
|
||||
}
|
||||
@@ -67,7 +95,7 @@ func Code(err error) int {
|
||||
// Domain returns the domain for a particular error.
|
||||
// It supports wrapped errors.
|
||||
func Domain(err error) string {
|
||||
if target := new(ErrorInfo); errors.As(err, &target) {
|
||||
if target := new(Error); errors.As(err, &target) {
|
||||
return target.Domain
|
||||
}
|
||||
return ""
|
||||
@@ -76,17 +104,8 @@ func Domain(err error) string {
|
||||
// Reason returns the reason for a particular error.
|
||||
// It supports wrapped errors.
|
||||
func Reason(err error) string {
|
||||
if target := new(ErrorInfo); errors.As(err, &target) {
|
||||
if target := new(Error); errors.As(err, &target) {
|
||||
return target.Reason
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// FromError try to convert an error to *Error.
|
||||
// It supports wrapped errors.
|
||||
func FromError(err error) *Error {
|
||||
if target := new(Error); errors.As(err, &target) {
|
||||
return target
|
||||
}
|
||||
return New(http.StatusInternalServerError, err.Error())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user