diff --git a/server/client.go b/server/client.go index 9981207..2fa70cd 100644 --- a/server/client.go +++ b/server/client.go @@ -186,7 +186,8 @@ func (c *client) onMessageReceived(messageType int, r io.Reader) { grpcClient := grpcPool.(serverGrpc.OverflowApiServerClient) out, err := grpcClient.Exec(context.Background(), si) if err != nil { - c.writeError(req, NewError(E_SERVER, err, err)) + c.writeError(req, NewGrpcError(err)) + return } c.writeResult(req, out.Result) diff --git a/server/protocol.go b/server/protocol.go index 24217c6..f81bd42 100644 --- a/server/protocol.go +++ b/server/protocol.go @@ -3,17 +3,22 @@ package server import ( "encoding/json" "errors" + + grpcCodes "google.golang.org/grpc/codes" + grpcStatus "google.golang.org/grpc/status" ) type ErrorCode int const ( + E_RUNTIME ErrorCode = -33000 E_PARSE ErrorCode = -32700 E_INVALID_REQ ErrorCode = -32600 E_NOT_FOUND_METHOD ErrorCode = -32601 E_INVALID_PARAMS ErrorCode = -32602 E_INTERNAL ErrorCode = -32603 - E_SERVER ErrorCode = -32000 + // -32000 ~ -32099 + E_SERVER ErrorCode = -32000 ) var ErrNullResult = errors.New("result is null") @@ -48,3 +53,34 @@ func NewError(code ErrorCode, err error, data interface{}) *Error { } return e } + +func NewGrpcError(err error) *Error { + resultStatus, ok := grpcStatus.FromError(err) + if !ok { + return nil + } + + var code ErrorCode + + switch resultStatus.Code() { + case grpcCodes.Unknown: + code = E_RUNTIME + case grpcCodes.InvalidArgument: + code = E_INVALID_PARAMS + case grpcCodes.Internal: + code = E_INTERNAL + case grpcCodes.Unimplemented: + code = E_NOT_FOUND_METHOD + case grpcCodes.Unavailable: + code = E_NOT_FOUND_METHOD + default: + + } + + e := &Error{ + Code: code, + Message: resultStatus.Message(), + Data: nil, + } + return e +}