Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

jsonrpc2: wireError becomes Error #38

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}