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
}
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

View file

@ -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.
@ -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
}