Skip to content

Commit

Permalink
Implement error interface for Error and move ErrorCode to its own file
Browse files Browse the repository at this point in the history
  • Loading branch information
AdamSLevy committed Jan 2, 2019
1 parent ff12d51 commit fc2ce38
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 26 deletions.
39 changes: 13 additions & 26 deletions error.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

package jsonrpc2

import "fmt"

// Error represents a JSON-RPC 2.0 Error object, which is used in the Response
// object.
type Error struct {
Expand All @@ -12,32 +14,6 @@ type Error struct {
Data interface{} `json:"data,omitempty"`
}

// ErrorCode represent the int JSON RPC 2.0 error code.
type ErrorCode int

// Official JSON-RPC 2.0 Spec Error Codes and Messages
const (
LowestReservedErrorCode ErrorCode = -32768
ParseErrorCode ErrorCode = -32700
InvalidRequestCode ErrorCode = -32600
MethodNotFoundCode ErrorCode = -32601
InvalidParamsCode ErrorCode = -32602
InternalErrorCode ErrorCode = -32603
HighestReservedErrorCode ErrorCode = -32000

ParseErrorMessage = "Parse error"
InvalidRequestMessage = "Invalid Request"
MethodNotFoundMessage = "Method not found"
InvalidParamsMessage = "Invalid params"
InternalErrorMessage = "Internal error"
)

// IsReserved returns true if c is within the reserved error code range:
// [LowestReservedErrorCode, HighestReservedErrorCode].
func (c ErrorCode) IsReserved() bool {
return LowestReservedErrorCode <= c && c <= HighestReservedErrorCode
}

// Official JSON-RPC 2.0 Errors
var (
// ParseError is returned to the client if a JSON is not well formed.
Expand Down Expand Up @@ -68,3 +44,14 @@ func NewInvalidParamsError(data interface{}) Error {
err.Data = data
return err
}

// Error implements the error interface.
func (e Error) Error() string {
s := fmt.Sprintf("jsonrpc2.Error{Code:%v, Message:%#v", e.Code, e.Message)
if e.Data != nil {
s += fmt.Sprintf(", Data:%#v}", e.Data)
} else {
s += "}"
}
return s
}
7 changes: 7 additions & 0 deletions error_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,10 @@ func TestErrorCodeIsReserved(t *testing.T) {
c = HighestReservedErrorCode
assert.True(c.IsReserved())
}

func TestError(t *testing.T) {
assert := assert.New(t)
var e error
var err Error
assert.Implements(&e, err)
}
27 changes: 27 additions & 0 deletions errorcode.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package jsonrpc2

// ErrorCode represent the int JSON RPC 2.0 error code.
type ErrorCode int

// Official JSON-RPC 2.0 Spec Error Codes and Messages
const (
LowestReservedErrorCode ErrorCode = -32768
ParseErrorCode ErrorCode = -32700
InvalidRequestCode ErrorCode = -32600
MethodNotFoundCode ErrorCode = -32601
InvalidParamsCode ErrorCode = -32602
InternalErrorCode ErrorCode = -32603
HighestReservedErrorCode ErrorCode = -32000

ParseErrorMessage = "Parse error"
InvalidRequestMessage = "Invalid Request"
MethodNotFoundMessage = "Method not found"
InvalidParamsMessage = "Invalid params"
InternalErrorMessage = "Internal error"
)

// IsReserved returns true if c is within the reserved error code range:
// [LowestReservedErrorCode, HighestReservedErrorCode].
func (c ErrorCode) IsReserved() bool {
return LowestReservedErrorCode <= c && c <= HighestReservedErrorCode
}
File renamed without changes.

0 comments on commit fc2ce38

Please sign in to comment.