internal/jsonrpc2_v2: export WireError type

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 <adonovan@google.com>
Reviewed-by: Than McIntosh <thanm@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
This commit is contained in:
Dan Kortschak 2024-02-08 11:38:17 +10:30 • committed by Alan Donovan
commit 2f861619f9
2 changed files with 11 additions and 11 deletions

View file

@ -96,17 +96,17 @@ func (msg *Response) marshal(to *wireCombined) {
to.Result = msg.Result to.Result = msg.Result
} }
func toWireError(err error) *wireError { func toWireError(err error) *WireError {
if err == nil { if err == nil {
// no error, the response is complete // no error, the response is complete
return nil return nil
} }
if err, ok := err.(*wireError); ok { if err, ok := err.(*WireError); ok {
// already a wire error, just use it // already a wire error, just use it
return err return err
} }
result := &wireError{Message: err.Error()} result := &WireError{Message: err.Error()}
var wrapped *wireError var wrapped *WireError
if errors.As(err, &wrapped) { if errors.As(err, &wrapped) {
// if we wrapped a wire error, keep the code from the wrapped error // if we wrapped a wire error, keep the code from the wrapped error
// but the message from the outer error // but the message from the outer error

View file

@ -49,11 +49,11 @@ type wireCombined struct {
Method string `json:"method,omitempty"` Method string `json:"method,omitempty"`
Params json.RawMessage `json:"params,omitempty"` Params json.RawMessage `json:"params,omitempty"`
Result json.RawMessage `json:"result,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. // WireError represents a structured error in a Response.
type wireError struct { type WireError struct {
// Code is an error code indicating the type of failure. // Code is an error code indicating the type of failure.
Code int64 `json:"code"` Code int64 `json:"code"`
// Message is a short description of the error. // Message is a short description of the error.
@ -67,18 +67,18 @@ type wireError struct {
// only be used to build errors for application specific codes as allowed by the // only be used to build errors for application specific codes as allowed by the
// specification. // specification.
func NewError(code int64, message string) error { func NewError(code int64, message string) error {
return &wireError{ return &WireError{
Code: code, Code: code,
Message: message, Message: message,
} }
} }
func (err *wireError) Error() string { func (err *WireError) Error() string {
return err.Message return err.Message
} }
func (err *wireError) Is(other error) bool { func (err *WireError) Is(other error) bool {
w, ok := other.(*wireError) w, ok := other.(*WireError)
if !ok { if !ok {
return false return false
} }