Error handling

This commit is contained in:
crusader 2017-08-14 19:07:13 +09:00
parent 671e711f19
commit 927e99c530
2 changed files with 39 additions and 2 deletions

View File

@ -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)

View File

@ -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
}