From 824bcf05ef2e3a613dd1dad445f0b8c1007e5b06 Mon Sep 17 00:00:00 2001 From: crusader Date: Tue, 27 Mar 2018 20:23:43 +0900 Subject: [PATCH] ing --- protocol/client_codec.go | 2 +- protocol/error.go | 23 +++++++++++++++++++++++ protocol/json/client_notification.go | 4 ++-- protocol/json/client_response.go | 21 +++++++++++++++------ protocol/json/error.go | 25 ------------------------- protocol/json/server_request.go | 12 ++++++------ protocol/json/server_response.go | 4 +++- 7 files changed, 50 insertions(+), 41 deletions(-) delete mode 100644 protocol/json/error.go diff --git a/protocol/client_codec.go b/protocol/client_codec.go index 43dfd48..92d9fd1 100644 --- a/protocol/client_codec.go +++ b/protocol/client_codec.go @@ -9,7 +9,7 @@ type ClientCodec interface { type ClientResponseCodec interface { Notification() (ClientNotificationCodec, error) Result(result interface{}) error - Error() error + Error() *Error ID() interface{} } diff --git a/protocol/error.go b/protocol/error.go index 25e8197..8ece1da 100644 --- a/protocol/error.go +++ b/protocol/error.go @@ -1,5 +1,10 @@ package protocol +import ( + "errors" + "fmt" +) + type ErrorCode int const ( @@ -10,3 +15,21 @@ const ( E_INTERNAL ErrorCode = -32603 E_SERVER ErrorCode = -32000 ) + +var ErrNullResult = errors.New("result is null") + +type Error struct { + // A Number that indicates the error type that occurred. + Code ErrorCode `json:"code"` /* required */ + + // A String providing a short description of the error. + // The message SHOULD be limited to a concise single sentence. + Message string `json:"message"` /* required */ + + // A Primitive or Structured value that contains additional information about the error. + Data interface{} `json:"data"` /* optional */ +} + +func (e Error) Error() string { + return fmt.Sprintf("Code[%d] Message[%s] Data[%v]", e.Code, e.Message, e.Data) +} diff --git a/protocol/json/client_notification.go b/protocol/json/client_notification.go index 1229427..9d272a1 100644 --- a/protocol/json/client_notification.go +++ b/protocol/json/client_notification.go @@ -37,7 +37,7 @@ func (cnc *ClientNotificationCodec) ReadParams(args []interface{}) error { // Note: if scr.request.Params is nil it's not an error, it's an optional member. // JSON params structured object. Unmarshal to the args object. if err := cuej.SetValueWithJSONStringArray(*cnc.noti.Params, args); nil != err { - cnc.err = &Error{ + cnc.err = &crp.Error{ Code: crp.E_INVALID_REQ, Message: err.Error(), Data: cnc.noti.Params, @@ -54,7 +54,7 @@ func (cnc *ClientNotificationCodec) Params() ([]string, error) { var values []string if err := json.Unmarshal(*cnc.noti.Params, &values); err != nil { - cnc.err = &Error{ + cnc.err = &crp.Error{ Code: crp.E_INVALID_REQ, Message: err.Error(), Data: cnc.noti.Params, diff --git a/protocol/json/client_response.go b/protocol/json/client_response.go index f5423ea..03f5ba4 100644 --- a/protocol/json/client_response.go +++ b/protocol/json/client_response.go @@ -15,7 +15,7 @@ import ( type clientResponse struct { Version string `json:"jsonrpc"` Result *json.RawMessage `json:"result,omitempty"` - Error error `json:"error,omitempty"` + Error *json.RawMessage `json:"error,omitempty"` ID interface{} `json:"id,omitempty"` } @@ -32,7 +32,7 @@ func (crc *ClientResponseCodec) ID() interface{} { func (crc *ClientResponseCodec) Result(result interface{}) error { if nil == crc.err && nil != crc.res.Result { if err := json.Unmarshal(*crc.res.Result, result); nil != err { - crc.err = &Error{ + crc.err = &crp.Error{ Code: crp.E_PARSE, Message: err.Error(), Data: crc.res.Result, @@ -43,8 +43,17 @@ func (crc *ClientResponseCodec) Result(result interface{}) error { return crc.err } -func (crc *ClientResponseCodec) Error() error { - return crc.res.Error +func (crc *ClientResponseCodec) Error() *crp.Error { + protocolError := &crp.Error{} + err := json.Unmarshal(*crc.res.Error, protocolError) + if nil != err { + return &crp.Error{ + Code: crp.E_PARSE, + Message: err.Error(), + Data: crc.res.Error, + } + } + return protocolError } func (crc *ClientResponseCodec) Notification() (protocol.ClientNotificationCodec, error) { @@ -68,14 +77,14 @@ func newClientResponseCodec(buf []byte) (protocol.ClientResponseCodec, error) { err := json.Unmarshal(buf, res) if err != nil { - err = &Error{ + err = &crp.Error{ Code: crp.E_PARSE, Message: err.Error(), Data: res, } } if res.Version != Version { - err = &Error{ + err = &crp.Error{ Code: crp.E_INVALID_REQ, Message: "jsonrpc must be " + Version, Data: res, diff --git a/protocol/json/error.go b/protocol/json/error.go deleted file mode 100644 index 8c2353c..0000000 --- a/protocol/json/error.go +++ /dev/null @@ -1,25 +0,0 @@ -package json - -import ( - "errors" - - crp "git.loafle.net/commons_go/rpc/protocol" -) - -var ErrNullResult = errors.New("result is null") - -type Error struct { - // A Number that indicates the error type that occurred. - Code crp.ErrorCode `json:"code"` /* required */ - - // A String providing a short description of the error. - // The message SHOULD be limited to a concise single sentence. - Message string `json:"message"` /* required */ - - // A Primitive or Structured value that contains additional information about the error. - Data interface{} `json:"data"` /* optional */ -} - -func (e *Error) Error() string { - return e.Message -} diff --git a/protocol/json/server_request.go b/protocol/json/server_request.go index 3362642..be143ea 100644 --- a/protocol/json/server_request.go +++ b/protocol/json/server_request.go @@ -40,7 +40,7 @@ func newServerRequestCodec(buf []byte) (protocol.ServerRequestCodec, error) { err := json.Unmarshal(buf, req) if err != nil { - err = &Error{ + err = &crp.Error{ Code: crp.E_PARSE, Message: err.Error(), Data: req, @@ -48,7 +48,7 @@ func newServerRequestCodec(buf []byte) (protocol.ServerRequestCodec, error) { } if req.Version != Version { - err = &Error{ + err = &crp.Error{ Code: crp.E_INVALID_REQ, Message: "jsonrpc must be " + Version, Data: req, @@ -93,7 +93,7 @@ func (src *ServerRequestCodec) ReadParams(args []interface{}) error { // Note: if scr.request.Params is nil it's not an error, it's an optional member. // JSON params structured object. Unmarshal to the args object. if err := cuej.SetValueWithJSONStringArray(*src.req.Params, args); nil != err { - src.err = &Error{ + src.err = &crp.Error{ Code: crp.E_INVALID_REQ, Message: err.Error(), Data: src.req.Params, @@ -110,7 +110,7 @@ func (src *ServerRequestCodec) Params() ([]string, error) { var values []string if err := json.Unmarshal(*src.req.Params, &values); err != nil { - src.err = &Error{ + src.err = &crp.Error{ Code: crp.E_INVALID_REQ, Message: err.Error(), Data: src.req.Params, @@ -128,9 +128,9 @@ func (src *ServerRequestCodec) NewResponse(reply interface{}) ([]byte, error) { return src.newServerResponse(res) } func (src *ServerRequestCodec) NewError(status int, err error) ([]byte, error) { - jsonErr, ok := err.(*Error) + jsonErr, ok := err.(*crp.Error) if !ok { - jsonErr = &Error{ + jsonErr = &crp.Error{ Code: crp.E_SERVER, Message: err.Error(), } diff --git a/protocol/json/server_response.go b/protocol/json/server_response.go index 4038b55..f6a4138 100644 --- a/protocol/json/server_response.go +++ b/protocol/json/server_response.go @@ -2,6 +2,8 @@ package json import ( "encoding/json" + + crp "git.loafle.net/commons_go/rpc/protocol" ) // ---------------------------------------------------------------------------- @@ -20,7 +22,7 @@ type serverResponse struct { // An Error object if there was an error invoking the method. It must be // null if there was no error. // As per spec the member will be omitted if there was no error. - Error *Error `json:"error,omitempty"` + Error *crp.Error `json:"error,omitempty"` // This must be the same id as the request it is responding to. ID *json.RawMessage `json:"id,omitempty"`