This commit is contained in:
crusader 2018-03-27 20:23:43 +09:00
parent 22eb6d7703
commit 824bcf05ef
7 changed files with 50 additions and 41 deletions

View File

@ -9,7 +9,7 @@ type ClientCodec interface {
type ClientResponseCodec interface { type ClientResponseCodec interface {
Notification() (ClientNotificationCodec, error) Notification() (ClientNotificationCodec, error)
Result(result interface{}) error Result(result interface{}) error
Error() error Error() *Error
ID() interface{} ID() interface{}
} }

View File

@ -1,5 +1,10 @@
package protocol package protocol
import (
"errors"
"fmt"
)
type ErrorCode int type ErrorCode int
const ( const (
@ -10,3 +15,21 @@ const (
E_INTERNAL ErrorCode = -32603 E_INTERNAL ErrorCode = -32603
E_SERVER ErrorCode = -32000 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)
}

View File

@ -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. // 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. // JSON params structured object. Unmarshal to the args object.
if err := cuej.SetValueWithJSONStringArray(*cnc.noti.Params, args); nil != err { if err := cuej.SetValueWithJSONStringArray(*cnc.noti.Params, args); nil != err {
cnc.err = &Error{ cnc.err = &crp.Error{
Code: crp.E_INVALID_REQ, Code: crp.E_INVALID_REQ,
Message: err.Error(), Message: err.Error(),
Data: cnc.noti.Params, Data: cnc.noti.Params,
@ -54,7 +54,7 @@ func (cnc *ClientNotificationCodec) Params() ([]string, error) {
var values []string var values []string
if err := json.Unmarshal(*cnc.noti.Params, &values); err != nil { if err := json.Unmarshal(*cnc.noti.Params, &values); err != nil {
cnc.err = &Error{ cnc.err = &crp.Error{
Code: crp.E_INVALID_REQ, Code: crp.E_INVALID_REQ,
Message: err.Error(), Message: err.Error(),
Data: cnc.noti.Params, Data: cnc.noti.Params,

View File

@ -15,7 +15,7 @@ import (
type clientResponse struct { type clientResponse struct {
Version string `json:"jsonrpc"` Version string `json:"jsonrpc"`
Result *json.RawMessage `json:"result,omitempty"` Result *json.RawMessage `json:"result,omitempty"`
Error error `json:"error,omitempty"` Error *json.RawMessage `json:"error,omitempty"`
ID interface{} `json:"id,omitempty"` ID interface{} `json:"id,omitempty"`
} }
@ -32,7 +32,7 @@ func (crc *ClientResponseCodec) ID() interface{} {
func (crc *ClientResponseCodec) Result(result interface{}) error { func (crc *ClientResponseCodec) Result(result interface{}) error {
if nil == crc.err && nil != crc.res.Result { if nil == crc.err && nil != crc.res.Result {
if err := json.Unmarshal(*crc.res.Result, result); nil != err { if err := json.Unmarshal(*crc.res.Result, result); nil != err {
crc.err = &Error{ crc.err = &crp.Error{
Code: crp.E_PARSE, Code: crp.E_PARSE,
Message: err.Error(), Message: err.Error(),
Data: crc.res.Result, Data: crc.res.Result,
@ -43,8 +43,17 @@ func (crc *ClientResponseCodec) Result(result interface{}) error {
return crc.err return crc.err
} }
func (crc *ClientResponseCodec) Error() error { func (crc *ClientResponseCodec) Error() *crp.Error {
return crc.res.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) { func (crc *ClientResponseCodec) Notification() (protocol.ClientNotificationCodec, error) {
@ -68,14 +77,14 @@ func newClientResponseCodec(buf []byte) (protocol.ClientResponseCodec, error) {
err := json.Unmarshal(buf, res) err := json.Unmarshal(buf, res)
if err != nil { if err != nil {
err = &Error{ err = &crp.Error{
Code: crp.E_PARSE, Code: crp.E_PARSE,
Message: err.Error(), Message: err.Error(),
Data: res, Data: res,
} }
} }
if res.Version != Version { if res.Version != Version {
err = &Error{ err = &crp.Error{
Code: crp.E_INVALID_REQ, Code: crp.E_INVALID_REQ,
Message: "jsonrpc must be " + Version, Message: "jsonrpc must be " + Version,
Data: res, Data: res,

View File

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

View File

@ -40,7 +40,7 @@ func newServerRequestCodec(buf []byte) (protocol.ServerRequestCodec, error) {
err := json.Unmarshal(buf, req) err := json.Unmarshal(buf, req)
if err != nil { if err != nil {
err = &Error{ err = &crp.Error{
Code: crp.E_PARSE, Code: crp.E_PARSE,
Message: err.Error(), Message: err.Error(),
Data: req, Data: req,
@ -48,7 +48,7 @@ func newServerRequestCodec(buf []byte) (protocol.ServerRequestCodec, error) {
} }
if req.Version != Version { if req.Version != Version {
err = &Error{ err = &crp.Error{
Code: crp.E_INVALID_REQ, Code: crp.E_INVALID_REQ,
Message: "jsonrpc must be " + Version, Message: "jsonrpc must be " + Version,
Data: req, 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. // 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. // JSON params structured object. Unmarshal to the args object.
if err := cuej.SetValueWithJSONStringArray(*src.req.Params, args); nil != err { if err := cuej.SetValueWithJSONStringArray(*src.req.Params, args); nil != err {
src.err = &Error{ src.err = &crp.Error{
Code: crp.E_INVALID_REQ, Code: crp.E_INVALID_REQ,
Message: err.Error(), Message: err.Error(),
Data: src.req.Params, Data: src.req.Params,
@ -110,7 +110,7 @@ func (src *ServerRequestCodec) Params() ([]string, error) {
var values []string var values []string
if err := json.Unmarshal(*src.req.Params, &values); err != nil { if err := json.Unmarshal(*src.req.Params, &values); err != nil {
src.err = &Error{ src.err = &crp.Error{
Code: crp.E_INVALID_REQ, Code: crp.E_INVALID_REQ,
Message: err.Error(), Message: err.Error(),
Data: src.req.Params, Data: src.req.Params,
@ -128,9 +128,9 @@ func (src *ServerRequestCodec) NewResponse(reply interface{}) ([]byte, error) {
return src.newServerResponse(res) return src.newServerResponse(res)
} }
func (src *ServerRequestCodec) NewError(status int, err error) ([]byte, error) { func (src *ServerRequestCodec) NewError(status int, err error) ([]byte, error) {
jsonErr, ok := err.(*Error) jsonErr, ok := err.(*crp.Error)
if !ok { if !ok {
jsonErr = &Error{ jsonErr = &crp.Error{
Code: crp.E_SERVER, Code: crp.E_SERVER,
Message: err.Error(), Message: err.Error(),
} }

View File

@ -2,6 +2,8 @@ package json
import ( import (
"encoding/json" "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 // An Error object if there was an error invoking the method. It must be
// null if there was no error. // null if there was no error.
// As per spec the member will be omitted 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. // This must be the same id as the request it is responding to.
ID *json.RawMessage `json:"id,omitempty"` ID *json.RawMessage `json:"id,omitempty"`