39 lines
1.1 KiB
Go
39 lines
1.1 KiB
Go
package pkg
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"google.golang.org/grpc/codes"
|
|
"google.golang.org/grpc/status"
|
|
)
|
|
|
|
var (
|
|
NoPermission = errors.New("no permission")
|
|
ErrUnauthenticated = errors.New("unauthenticated")
|
|
ErrUnhandled = errors.New("unhandled")
|
|
ErrNotFound = errors.New("not found")
|
|
ErrBadInput = errors.New("bad input")
|
|
ErrInternal = errors.New("internal")
|
|
)
|
|
|
|
func Wrap(basic error, err error, op string, msg string) error {
|
|
return errors.Join(basic, err, fmt.Errorf("during %s: %s", op, msg))
|
|
}
|
|
|
|
func ToGRPC(err error) error {
|
|
switch {
|
|
case errors.Is(err, ErrUnauthenticated):
|
|
return status.Errorf(codes.Unauthenticated, err.Error())
|
|
case errors.Is(err, ErrBadInput):
|
|
return status.Errorf(codes.InvalidArgument, err.Error())
|
|
case errors.Is(err, ErrNotFound):
|
|
return status.Errorf(codes.NotFound, err.Error())
|
|
case errors.Is(err, ErrInternal):
|
|
return status.Errorf(codes.Internal, err.Error())
|
|
case errors.Is(err, NoPermission):
|
|
return status.Errorf(codes.PermissionDenied, err.Error())
|
|
}
|
|
|
|
return status.Errorf(codes.Unknown, err.Error())
|
|
}
|