-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from zeroabe/feature/grpc_errors
grpc errors
- Loading branch information
Showing
6 changed files
with
255 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
package ginerrors | ||
|
||
import ( | ||
"errors" | ||
"net/http" | ||
|
||
"google.golang.org/genproto/googleapis/rpc/errdetails" | ||
"google.golang.org/grpc/codes" | ||
"google.golang.org/grpc/status" | ||
"gopkg.in/go-playground/validator.v9" | ||
) | ||
|
||
type GRPCValidationError struct { | ||
Violations []*errdetails.BadRequest_FieldViolation | ||
} | ||
|
||
var ( | ||
httpToGRPCCodes = map[int]codes.Code{ | ||
http.StatusNotFound: codes.NotFound, | ||
http.StatusMethodNotAllowed: codes.Unavailable, | ||
http.StatusInternalServerError: codes.Internal, | ||
http.StatusBadRequest: codes.InvalidArgument, | ||
} | ||
|
||
grpcToHTTPCodes = map[codes.Code]int{ | ||
codes.NotFound: http.StatusNotFound, | ||
codes.Unavailable: http.StatusMethodNotAllowed, | ||
codes.Internal: http.StatusInternalServerError, | ||
codes.InvalidArgument: http.StatusBadRequest, | ||
} | ||
) | ||
|
||
func UnwrapRPCError(st *status.Status) interface{} { | ||
switch st.Code() { | ||
case codes.NotFound: | ||
return ErrRecordNotFound | ||
case codes.Unavailable: | ||
return ErrNoMethod | ||
case codes.Internal: | ||
return ErrServerError | ||
case codes.InvalidArgument: | ||
details := st.Details() | ||
violations := getViolations(details) | ||
|
||
return GRPCValidationError{Violations: violations} | ||
default: | ||
return errors.New(st.Message()) | ||
} | ||
} | ||
|
||
func WrapErrorWithStatus(err error, lang string) error { | ||
l := langName(lang) | ||
httpCode, msg := getErrCode(err) | ||
|
||
code, ok := getGRPCCode(httpCode) | ||
if !ok { | ||
return err | ||
} | ||
|
||
st := status.New(code, msg) | ||
|
||
if code == codes.InvalidArgument { | ||
if errs, ok := err.(validator.ValidationErrors); ok { | ||
var e error | ||
br := &errdetails.BadRequest{} | ||
for _, ee := range errs { | ||
field := getFieldName(ee.Namespace(), ee.Field()) | ||
msg := getErrMessage(validationRule(ee.ActualTag()), field, ee.Param(), l) | ||
violation := &errdetails.BadRequest_FieldViolation{ | ||
Field: string(field), | ||
Description: string(msg), | ||
} | ||
|
||
br.FieldViolations = append(br.FieldViolations, violation) | ||
} | ||
|
||
st, e = st.WithDetails(br) | ||
if e != nil { | ||
return err | ||
} | ||
} | ||
} | ||
|
||
return st.Err() | ||
} | ||
|
||
// getGRPCCode returns grpc error code by first value and its existence by second | ||
func getGRPCCode(httpCode int) (codes.Code, bool) { | ||
c, ok := httpToGRPCCodes[httpCode] | ||
return c, ok | ||
} | ||
|
||
func getViolations(details []interface{}) []*errdetails.BadRequest_FieldViolation { | ||
if len(details) == 0 { | ||
return nil | ||
} | ||
|
||
violations := make([]*errdetails.BadRequest_FieldViolation, 0) | ||
for _, detail := range details { | ||
if d, ok := detail.(*errdetails.BadRequest); ok { | ||
violations = append(violations, d.FieldViolations...) | ||
} | ||
} | ||
|
||
return violations | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package ginerrors | ||
|
||
import ( | ||
"database/sql" | ||
"errors" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
const ( | ||
unknownErrMessage = "rpc error: code = InvalidArgument desc = foo" | ||
unknownErrValue = "foo" | ||
recordNotFountMessage = "rpc error: code = NotFound desc = record not found" | ||
unavailableMethodMessage = "rpc error: code = Unavailable desc = method not allowed" | ||
) | ||
|
||
func TestWrapErrWithRPC(t *testing.T) { | ||
cases := []struct { | ||
caseName string | ||
err error | ||
lang string | ||
expected string | ||
}{ | ||
{ | ||
caseName: "unknown err", | ||
err: errors.New(unknownErrValue), | ||
lang: "en", | ||
expected: unknownErrMessage, | ||
}, | ||
{ | ||
caseName: "sql err", | ||
err: sql.ErrNoRows, | ||
lang: "en", | ||
expected: recordNotFountMessage, | ||
}, | ||
{ | ||
caseName: "unavailable method", | ||
err: ErrNoMethod, | ||
lang: "en", | ||
expected: unavailableMethodMessage, | ||
}, | ||
} | ||
for _, cc := range cases { | ||
t.Run(cc.caseName, func(t *testing.T) { | ||
err := WrapErrorWithStatus(cc.err, cc.lang) | ||
assert.Error(t, err) | ||
assert.Equal(t, cc.expected, err.Error()) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters