Skip to content

Commit

Permalink
feat: add HandleGrpcError function (#67)
Browse files Browse the repository at this point in the history
* created HandleGrpcError function to transform grpc errors into graphql errors

* adding codes.AlreadyExists to switch cases
  • Loading branch information
mattvinicius authored Dec 22, 2023
1 parent 0467db1 commit 3ff1936
Showing 1 changed file with 110 additions and 0 deletions.
110 changes: 110 additions & 0 deletions pkg/grpc/handleError.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@ package grpc

import (
"context"
"errors"
"net/http"

"github.com/TheRafaBonin/roxy"

thunderGraphql "github.com/gothunder/thunder/pkg/graphql"
thunderLogger "github.com/gothunder/thunder/pkg/log"

"google.golang.org/grpc/codes"
Expand All @@ -27,3 +30,110 @@ func HandleError(ctx context.Context, err error) error {
thunderLogger.LogError(ctx, err)
return status.Error(grpcCode, grpcMessage)
}

func HandleGrpcErrorIgnoringNotFound(ctx context.Context, err error) error {
var bareStatusErr error
_, ok := err.(interface {
Unwrap() error
})
if !ok {
bareStatusErr = err
} else {
bareStatusErr = errors.Unwrap(err)
}
if statusCode, ok := status.FromError(bareStatusErr); ok {
switch statusCode.Code() {
case codes.NotFound:
return nil
case codes.InvalidArgument:
return thunderGraphql.HandleError(ctx, roxy.SetDefaultHTTPResponse(err, roxy.HTTPResponse{
Message: statusCode.Message(),
Status: http.StatusBadRequest,
}))
case codes.Internal:
return thunderGraphql.HandleError(ctx, roxy.SetDefaultHTTPResponse(err, roxy.HTTPResponse{
Message: "internal error",
Status: http.StatusInternalServerError,
}))
case codes.AlreadyExists:
return thunderGraphql.HandleError(ctx, roxy.SetDefaultHTTPResponse(err, roxy.HTTPResponse{
Message: statusCode.Message(),
Status: http.StatusConflict,
}))
default:
return thunderGraphql.HandleError(ctx, roxy.SetDefaultHTTPResponse(err, roxy.HTTPResponse{
Message: "internal error",
Status: http.StatusInternalServerError,
}))
}
} else {
return thunderGraphql.HandleError(ctx, roxy.SetDefaultHTTPResponse(err, roxy.HTTPResponse{
Message: "internal error",
Status: http.StatusInternalServerError,
}))
}
}

func GetStatusCodeFromRawError(err error) codes.Code {
var bareStatusErr error
_, ok := err.(interface {
Unwrap() error
})
if !ok {
bareStatusErr = err
} else {
bareStatusErr = errors.Unwrap(err)
}

if statusCode, ok := status.FromError(bareStatusErr); ok {
return statusCode.Code()
}

return codes.Internal
}

func HandleGrpcError(ctx context.Context, err error) error {
var bareStatusErr error
_, ok := err.(interface {
Unwrap() error
})
if !ok {
bareStatusErr = err
} else {
bareStatusErr = errors.Unwrap(err)
}
if statusCode, ok := status.FromError(bareStatusErr); ok {
switch statusCode.Code() {
case codes.NotFound:
return thunderGraphql.HandleError(ctx, roxy.SetDefaultHTTPResponse(err, roxy.HTTPResponse{
Message: statusCode.Message(),
Status: http.StatusNotFound,
}))
case codes.InvalidArgument:
return thunderGraphql.HandleError(ctx, roxy.SetDefaultHTTPResponse(err, roxy.HTTPResponse{
Message: statusCode.Message(),
Status: http.StatusBadRequest,
}))
case codes.Internal:
return thunderGraphql.HandleError(ctx, roxy.SetDefaultHTTPResponse(err, roxy.HTTPResponse{
Message: "internal error",
Status: http.StatusInternalServerError,
}))
case codes.AlreadyExists:
return thunderGraphql.HandleError(ctx, roxy.SetDefaultHTTPResponse(err, roxy.HTTPResponse{
Message: statusCode.Message(),
Status: http.StatusConflict,
}))
default:
return thunderGraphql.HandleError(ctx, roxy.SetDefaultHTTPResponse(err, roxy.HTTPResponse{
Message: "internal error",
Status: http.StatusInternalServerError,
}))
}
} else {
return thunderGraphql.HandleError(ctx, roxy.SetDefaultHTTPResponse(err, roxy.HTTPResponse{
Message: "internal error",
Status: http.StatusInternalServerError,
}))
}
}

0 comments on commit 3ff1936

Please sign in to comment.