Skip to content

Commit

Permalink
jsonrpc2: wireError becomes Error
Browse files Browse the repository at this point in the history
So Code & Data fields are available outside jsonrpc2 package.
  • Loading branch information
maxatome committed Sep 30, 2022
1 parent de9c53c commit 56e1a4e
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 12 deletions.
14 changes: 7 additions & 7 deletions jsonrpc2/messages.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,23 +92,23 @@ func NewResponse(id ID, result interface{}, rerr error) (*Response, error) {

func (msg *Response) marshal(to *wireCombined) {
to.ID = msg.ID.value
to.Error = toWireError(msg.Error)
to.Error = toError(msg.Error)
to.Result = msg.Result
}

func toWireError(err error) *wireError {
func toError(err error) *Error {
if err == nil {
// no error, the response is complete
return nil
}
if err, ok := err.(*wireError); ok {
// already a wire error, just use it
if err, ok := err.(*Error); ok {
// already an Error, just use it
return err
}
result := &wireError{Message: err.Error()}
var wrapped *wireError
result := &Error{Message: err.Error()}
var wrapped *Error
if errors.As(err, &wrapped) {
// if we wrapped a wire error, keep the code from the wrapped error
// if we wrapped an Error, keep the code from the wrapped error
// but the message from the outer error
result.Code = wrapped.Code
}
Expand Down
10 changes: 5 additions & 5 deletions jsonrpc2/wire.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,11 @@ type wireCombined struct {
Method string `json:"method,omitempty"`
Params json.RawMessage `json:"params,omitempty"`
Result json.RawMessage `json:"result,omitempty"`
Error *wireError `json:"error,omitempty"`
Error *Error `json:"error,omitempty"`
}

// wireError represents a structured error in a Response.
type wireError struct {
// Error represents a structured error in a Response.
type Error struct {
// Code is an error code indicating the type of failure.
Code int64 `json:"code"`
// Message is a short description of the error.
Expand All @@ -63,12 +63,12 @@ type wireError struct {
// only be used to build errors for application specific codes as allowed by the
// specification.
func NewError(code int64, message string) error {
return &wireError{
return &Error{
Code: code,
Message: message,
}
}

func (err *wireError) Error() string {
func (err *Error) Error() string {
return err.Message
}

0 comments on commit 56e1a4e

Please sign in to comment.