Skip to content

Commit

Permalink
internal/jsonrpc2: 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.

Closes golang/go#64882

Change-Id: I749c52c5ffc2d83922aeafc746ee13ea657d6551
Reviewed-on: https://go-review.googlesource.com/c/tools/+/562515
LUCI-TryBot-Result: Go LUCI <[email protected]>
Reviewed-by: Bryan Mills <[email protected]>
Reviewed-by: Alan Donovan <[email protected]>
  • Loading branch information
kortschak authored and adonovan committed Feb 7, 2024
1 parent acf07b3 commit 76ef6b6
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 10 deletions.
8 changes: 4 additions & 4 deletions internal/jsonrpc2/messages.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,17 +157,17 @@ func (r *Response) MarshalJSON() ([]byte, error) {
return data, nil
}

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
12 changes: 6 additions & 6 deletions internal/jsonrpc2/wire.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ type wireResponse struct {
// Result is the response value, and is required on success.
Result *json.RawMessage `json:"result,omitempty"`
// Error is a structured error response if the call fails.
Error *wireError `json:"error,omitempty"`
Error *WireError `json:"error,omitempty"`
// ID must be set and is the identifier of the Request this is a response to.
ID *ID `json:"id,omitempty"`
}
Expand All @@ -70,11 +70,11 @@ type wireCombined struct {
Method string `json:"method"`
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 @@ -96,13 +96,13 @@ type ID struct {
}

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
}

Expand Down

0 comments on commit 76ef6b6

Please sign in to comment.