From 4f62d14421d1574a97d3ef08104ab855e49c2ad5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Miko=C5=82ajczyk?= Date: Fri, 10 Apr 2026 14:02:59 +0200 Subject: [PATCH] Use errors.As instead of direct cast in geth errorMessage --- rpc/json.go | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/rpc/json.go b/rpc/json.go index b0e6092169..52a7f04d80 100644 --- a/rpc/json.go +++ b/rpc/json.go @@ -126,12 +126,12 @@ func errorMessage(err error) *jsonrpcMessage { Code: ErrcodeDefault, Message: err.Error(), }} - ec, ok := err.(Error) - if ok { + var ec Error + if errors.As(err, &ec) { msg.Error.Code = ec.ErrorCode() } - de, ok := err.(DataError) - if ok { + var de DataError + if errors.As(err, &de) { msg.Error.Data = de.ErrorData() } return msg @@ -158,6 +158,19 @@ func (err *jsonError) ErrorData() interface{} { return err.Data } +// Is reports whether target carries the same JSON-RPC error code as err. +// This allows errors.Is(receivedErr, sentinel) to work on the client side +// when receivedErr is a jsonError reconstructed from the wire and sentinel +// is any error that exposes an ErrorCode() int method. +func (err *jsonError) Is(target error) bool { + type errorCoder interface{ ErrorCode() int } + t, ok := target.(errorCoder) + if !ok { + return false + } + return err.Code == t.ErrorCode() +} + // Conn is a subset of the methods of net.Conn which are sufficient for ServerCodec. type Conn interface { io.ReadWriteCloser