Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add HandleGrpcError function #67

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
}))
}
}
Loading