48 lines
1.6 KiB
Go
48 lines
1.6 KiB
Go
|
package exception
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
|
||
|
"google.golang.org/grpc/codes"
|
||
|
"google.golang.org/grpc/status"
|
||
|
)
|
||
|
|
||
|
var (
|
||
|
// google grpc error status.
|
||
|
OK = ErrorJson(0, "OK")
|
||
|
ErrCanceled = ErrorJson(1, "Canceled")
|
||
|
ErrUnknown = ErrorJson(2, "Unknown")
|
||
|
ErrInvalidArgument = ErrorJson(3, "Invalid Argument")
|
||
|
ErrDeadlineExceeded = ErrorJson(4, "Deadline Exceeded")
|
||
|
ErrNotFound = ErrorJson(5, "Not Found")
|
||
|
ErrAlreadyExists = ErrorJson(6, "Already Exists")
|
||
|
ErrPermissionDenied = ErrorJson(7, "Permission Denied")
|
||
|
ErrResourceExhausted = ErrorJson(8, "Resource Exhausted")
|
||
|
ErrFailedPrecondition = ErrorJson(9, "Failed Precondition")
|
||
|
ErrAborted = ErrorJson(10, "Aborted")
|
||
|
ErrOutOfRange = ErrorJson(11, "Out Of Range")
|
||
|
ErrUnimplemented = ErrorJson(12, "Unimplemented")
|
||
|
ErrInternal = ErrorJson(13, "Internal")
|
||
|
ErrUnavailable = ErrorJson(14, "Unavailable")
|
||
|
ErrDataLoss = ErrorJson(15, "Data Loss")
|
||
|
ErrUnauthenticated = ErrorJson(16, "Unauthenticated")
|
||
|
ErrJSONMarshal = ErrorJson(17, "Marshal JSON")
|
||
|
ErrJSONUnmarshal = ErrorJson(18, "Unmarshal JSON")
|
||
|
|
||
|
ErrSmsCode = ErrorJson(20, "SMS Code Invalid")
|
||
|
|
||
|
// coustom error status
|
||
|
)
|
||
|
|
||
|
func Error(c uint32, msg string) error {
|
||
|
return status.New(codes.Code(c), msg).Err()
|
||
|
}
|
||
|
|
||
|
func Errorf(c uint32, format string, a ...interface{}) error {
|
||
|
return status.New(codes.Code(c), fmt.Sprintf(format, a...)).Err()
|
||
|
}
|
||
|
|
||
|
func ErrorJson(c uint32, msg string) error {
|
||
|
return status.New(codes.Code(c), fmt.Sprintf(`{ "Code": %d, "Msg": "%s" }`, c, msg)).Err()
|
||
|
}
|