ing
This commit is contained in:
parent
22eb6d7703
commit
824bcf05ef
|
@ -9,7 +9,7 @@ type ClientCodec interface {
|
|||
type ClientResponseCodec interface {
|
||||
Notification() (ClientNotificationCodec, error)
|
||||
Result(result interface{}) error
|
||||
Error() error
|
||||
Error() *Error
|
||||
ID() interface{}
|
||||
}
|
||||
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -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
|
||||
}
|
|
@ -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(),
|
||||
}
|
||||
|
|
|
@ -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"`
|
||||
|
|
Loading…
Reference in New Issue
Block a user