Skip to content

Commit

Permalink
grpc to fiber err (#38)
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesReate committed Jun 23, 2023
1 parent 980ffc0 commit 5819ef3
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions grpc_utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package shared

import (
"github.com/gofiber/fiber/v2"
"github.com/pkg/errors"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)

// GrpcErrorToFiber useful anywhere calling a grpc underlying service and wanting to augment the error for fiber from grpc status codes
// meant to play nicely with the ErrorHandler in api.go that this would hand off errors to.
// msgAppend appends to error string, to eg. help if this gets logged
func GrpcErrorToFiber(err error, msgAppend string) error {
if err == nil {
return nil
}
// pull out grpc error status to then convert to fiber http equivalent
errStatus, _ := status.FromError(err)

switch errStatus.Code() {
case codes.InvalidArgument:
return fiber.NewError(fiber.StatusBadRequest, errStatus.Message()+". "+msgAppend)
case codes.NotFound:
return fiber.NewError(fiber.StatusNotFound, errStatus.Message()+". "+msgAppend)
case codes.Aborted:
return fiber.NewError(fiber.StatusConflict, errStatus.Message()+". "+msgAppend)
case codes.Internal:
return fiber.NewError(fiber.StatusInternalServerError, errStatus.Message()+". "+msgAppend)
}
return errors.Wrap(err, msgAppend)
}

0 comments on commit 5819ef3

Please sign in to comment.