Skip to content

Commit

Permalink
internal/jsonrpc2_v2: export WireError type
Browse files Browse the repository at this point in the history
This makes it possible for users of the package to us the optional error
data field in error construction and error handling logic.

Updates golang/go#64882

Change-Id: Iaa554f42ff42a0b7355605ba0b49ac67f623da15
Reviewed-on: https://go-review.googlesource.com/c/tools/+/562575
Reviewed-by: Alan Donovan <[email protected]>
Reviewed-by: Than McIntosh <[email protected]>
LUCI-TryBot-Result: Go LUCI <[email protected]>
  • Loading branch information
kortschak authored and adonovan committed Feb 14, 2024
1 parent 1b39a8b commit e1a6261
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 11 deletions.
8 changes: 4 additions & 4 deletions internal/jsonrpc2_v2/messages.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,17 +96,17 @@ func (msg *Response) marshal(to *wireCombined) {
to.Result = msg.Result
}

func toWireError(err error) *wireError {
func toWireError(err error) *WireError {
if err == nil {
// no error, the response is complete
return nil
}
if err, ok := err.(*wireError); ok {
if err, ok := err.(*WireError); ok {
// already a wire error, just use it
return err
}
result := &wireError{Message: err.Error()}
var wrapped *wireError
result := &WireError{Message: err.Error()}
var wrapped *WireError
if errors.As(err, &wrapped) {
// if we wrapped a wire error, keep the code from the wrapped error
// but the message from the outer error
Expand Down
14 changes: 7 additions & 7 deletions internal/jsonrpc2_v2/wire.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,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 *WireError `json:"error,omitempty"`
}

// wireError represents a structured error in a Response.
type wireError struct {
// WireError represents a structured error in a Response.
type WireError 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 @@ -67,18 +67,18 @@ 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 &WireError{
Code: code,
Message: message,
}
}

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

func (err *wireError) Is(other error) bool {
w, ok := other.(*wireError)
func (err *WireError) Is(other error) bool {
w, ok := other.(*WireError)
if !ok {
return false
}
Expand Down

0 comments on commit e1a6261

Please sign in to comment.