mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-18 09:53:48 +00:00
Simplify if conditions using errors.As
This commit is contained in:
parent
e2ba539edd
commit
d46e1b00ba
30 changed files with 43 additions and 43 deletions
|
|
@ -121,7 +121,7 @@ func loadConfig(file string, cfg *gethConfig) error {
|
|||
err = tomlSettings.NewDecoder(bufio.NewReader(f)).Decode(cfg)
|
||||
// Add file name to errors that have a line number.
|
||||
lineErr := new(toml.LineError)
|
||||
if ok := errors.As(err, &lineErr); ok {
|
||||
if errors.As(err, &lineErr) {
|
||||
err = errors.New(file + ", " + err.Error())
|
||||
}
|
||||
return err
|
||||
|
|
|
|||
|
|
@ -225,7 +225,7 @@ func decodeNibble(in byte) uint64 {
|
|||
|
||||
func mapError(err error) error {
|
||||
numErr := new(strconv.NumError)
|
||||
if ok := errors.As(err, &numErr); ok {
|
||||
if errors.As(err, &numErr) {
|
||||
switch {
|
||||
case errors.Is(numErr.Err, strconv.ErrRange):
|
||||
return ErrUint64Range
|
||||
|
|
@ -235,7 +235,7 @@ func mapError(err error) error {
|
|||
}
|
||||
|
||||
var invalidByteErr hex.InvalidByteError
|
||||
if ok := errors.As(err, &invalidByteErr); ok {
|
||||
if errors.As(err, &invalidByteErr) {
|
||||
return ErrSyntax
|
||||
}
|
||||
if errors.Is(err, hex.ErrLength) {
|
||||
|
|
|
|||
|
|
@ -412,7 +412,7 @@ func checkNumberText(input []byte) (raw []byte, err error) {
|
|||
|
||||
func wrapTypeError(err error, typ reflect.Type) error {
|
||||
decErr := new(decError)
|
||||
if ok := errors.As(err, &decErr); ok {
|
||||
if errors.As(err, &decErr) {
|
||||
return &json.UnmarshalTypeError{Value: err.Error(), Type: typ}
|
||||
}
|
||||
return err
|
||||
|
|
|
|||
|
|
@ -31,7 +31,7 @@ func LoadJSON(file string, val interface{}) error {
|
|||
}
|
||||
if err := json.Unmarshal(content, val); err != nil {
|
||||
syntaxerr := new(json.SyntaxError)
|
||||
if ok := errors.As(err, &syntaxerr); ok {
|
||||
if errors.As(err, &syntaxerr) {
|
||||
line := findLine(content, syntaxerr.Offset)
|
||||
return fmt.Errorf("JSON syntax error at %v:%v: %v", file, line, err)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -170,11 +170,11 @@ func (b *bridge) Send(call jsre.Call) (goja.Value, error) {
|
|||
code := -32603
|
||||
var data interface{}
|
||||
var rcpErr rpc.Error
|
||||
if ok := errors.As(err, &rcpErr); ok {
|
||||
if errors.As(err, &rcpErr) {
|
||||
code = rcpErr.ErrorCode()
|
||||
}
|
||||
var rcpDataErr rpc.DataError
|
||||
if ok := errors.As(err, &rcpDataErr); ok {
|
||||
if errors.As(err, &rcpDataErr) {
|
||||
data = rcpDataErr.ErrorData()
|
||||
}
|
||||
setError(resp, code, err.Error(), data)
|
||||
|
|
|
|||
|
|
@ -149,7 +149,7 @@ func (c *Console) init(preload []string) error {
|
|||
if err := c.jsre.Exec(path); err != nil {
|
||||
failure := err.Error()
|
||||
gojaErr := new(goja.Exception)
|
||||
if ok := errors.As(err, &gojaErr); ok {
|
||||
if errors.As(err, &gojaErr) {
|
||||
failure = gojaErr.String()
|
||||
}
|
||||
return fmt.Errorf("%s: %v", path, failure)
|
||||
|
|
@ -207,7 +207,7 @@ func (c *Console) initExtensions() error {
|
|||
apis, err := c.client.SupportedModules()
|
||||
if err != nil {
|
||||
var rpcErr rpc.Error
|
||||
if ok := errors.As(err, &rpcErr); ok && rpcErr.ErrorCode() == methodNotFound {
|
||||
if errors.As(err, &rpcErr) && rpcErr.ErrorCode() == methodNotFound {
|
||||
log.Warn("Server does not support method rpc_modules, using default API list.")
|
||||
apis = defaultAPIs
|
||||
} else {
|
||||
|
|
|
|||
|
|
@ -1363,7 +1363,7 @@ func runRandTest(rt randTest) bool {
|
|||
func TestRandom(t *testing.T) {
|
||||
if err := quick.Check(runRandTest, nil); err != nil {
|
||||
cerr := new(quick.CheckError)
|
||||
if ok := errors.As(err, &cerr); ok {
|
||||
if errors.As(err, &cerr) {
|
||||
t.Fatalf("random test iteration %d failed: %s", cerr.Count, spew.Sdump(cerr.In))
|
||||
}
|
||||
t.Fatal(err)
|
||||
|
|
|
|||
|
|
@ -671,7 +671,7 @@ func (dl *diskLayer) generate(stats *generatorStats) {
|
|||
if err := generateAccounts(ctx, dl, accMarker); err != nil {
|
||||
// Extract the received interruption signal if exists
|
||||
aerr := new(abortErr)
|
||||
if ok := errors.As(err, &aerr); ok {
|
||||
if errors.As(err, &aerr) {
|
||||
abort = aerr.abort
|
||||
}
|
||||
// Aborted by internal error, wait the signal
|
||||
|
|
|
|||
|
|
@ -438,7 +438,7 @@ func TestStateChanges(t *testing.T) {
|
|||
config := &quick.Config{MaxCount: 1000}
|
||||
err := quick.Check((*stateTest).run, config)
|
||||
cerr := new(quick.CheckError)
|
||||
if ok := errors.As(err, &cerr); ok {
|
||||
if errors.As(err, &cerr) {
|
||||
test := cerr.In[0].(*stateTest)
|
||||
t.Errorf("%v:\n%s", test.err, test)
|
||||
} else if err != nil {
|
||||
|
|
|
|||
|
|
@ -306,7 +306,7 @@ func TestSnapshotRandom(t *testing.T) {
|
|||
config := &quick.Config{MaxCount: 1000}
|
||||
err := quick.Check((*snapshotTest).run, config)
|
||||
cerr := new(quick.CheckError)
|
||||
if ok := errors.As(err, &cerr); ok {
|
||||
if errors.As(err, &cerr) {
|
||||
test := cerr.In[0].(*snapshotTest)
|
||||
t.Errorf("%v:\n%s", test.err, test)
|
||||
} else if err != nil {
|
||||
|
|
|
|||
|
|
@ -192,7 +192,7 @@ func FromECDSAPub(pub *ecdsa.PublicKey) []byte {
|
|||
func HexToECDSA(hexkey string) (*ecdsa.PrivateKey, error) {
|
||||
b, err := hex.DecodeString(hexkey)
|
||||
var byteErr hex.InvalidByteError
|
||||
if ok := errors.As(err, &byteErr); ok {
|
||||
if errors.As(err, &byteErr) {
|
||||
return nil, fmt.Errorf("invalid hex character %q in private key", byte(byteErr))
|
||||
} else if err != nil {
|
||||
return nil, errors.New("invalid hex data for private key")
|
||||
|
|
|
|||
|
|
@ -99,7 +99,7 @@ func RunGit(args ...string) string {
|
|||
cmd.Stdout, cmd.Stderr = &stdout, &stderr
|
||||
if err := cmd.Run(); err != nil {
|
||||
e := new(exec.Error)
|
||||
if ok := errors.As(err, &e); ok && errors.Is(e.Err, exec.ErrNotFound) {
|
||||
if errors.As(err, &e) && errors.Is(e.Err, exec.ErrNotFound) {
|
||||
if !warnedAboutGit {
|
||||
log.Println("Warning: can't find 'git' in PATH")
|
||||
warnedAboutGit = true
|
||||
|
|
|
|||
|
|
@ -62,7 +62,7 @@ func prettyPrint(vm *goja.Runtime, value goja.Value, w io.Writer) {
|
|||
func prettyError(vm *goja.Runtime, err error, w io.Writer) {
|
||||
failure := err.Error()
|
||||
gojaErr := new(goja.Exception)
|
||||
if ok := errors.As(err, &gojaErr); ok {
|
||||
if errors.As(err, &gojaErr) {
|
||||
failure = gojaErr.String()
|
||||
}
|
||||
fmt.Fprint(w, ErrorColor("%s", failure))
|
||||
|
|
|
|||
|
|
@ -34,7 +34,7 @@ var (
|
|||
|
||||
func convertFileLockError(err error) error {
|
||||
var errno syscall.Errno
|
||||
if ok := errors.As(err, &errno); ok && datadirInUseErrnos[uint(errno)] {
|
||||
if errors.As(err, &errno) && datadirInUseErrnos[uint(errno)] {
|
||||
return ErrDatadirUsed
|
||||
}
|
||||
return err
|
||||
|
|
|
|||
|
|
@ -627,7 +627,7 @@ func (t *dialTask) String() string {
|
|||
|
||||
func cleanupDialErr(err error) error {
|
||||
netErr := new(net.OpError)
|
||||
if ok := errors.As(err, &netErr); ok && netErr.Op == "dial" {
|
||||
if errors.As(err, &netErr) && netErr.Op == "dial" {
|
||||
return netErr.Err
|
||||
}
|
||||
return err
|
||||
|
|
|
|||
|
|
@ -48,7 +48,7 @@ type nameError struct {
|
|||
|
||||
func (err nameError) Error() string {
|
||||
ee := new(entryError)
|
||||
if ok := errors.As(err.err, &ee); ok {
|
||||
if errors.As(err.err, &ee) {
|
||||
return fmt.Sprintf("invalid %s entry at %s: %v", ee.typ, err.name, ee.err)
|
||||
}
|
||||
return err.name + ": " + err.err.Error()
|
||||
|
|
|
|||
|
|
@ -54,7 +54,7 @@ func TestIsPacketTooBig(t *testing.T) {
|
|||
n, _, err := listener.ReadFrom(buf)
|
||||
if err != nil {
|
||||
var nerr net.Error
|
||||
if ok := errors.As(err, &nerr); ok && nerr.Timeout() {
|
||||
if errors.As(err, &nerr) && nerr.Timeout() {
|
||||
continue
|
||||
}
|
||||
if !isPacketTooBig(err) {
|
||||
|
|
|
|||
|
|
@ -33,9 +33,9 @@ const _WSAEMSGSIZE = syscall.Errno(10040)
|
|||
// code WSAEMSGSIZE and no data if this happens.
|
||||
func isPacketTooBig(err error) bool {
|
||||
opErr := new(net.OpError)
|
||||
if ok := errors.As(err, &opErr); ok {
|
||||
if errors.As(err, &opErr) {
|
||||
scErr := new(os.SyscallError)
|
||||
if ok := errors.As(opErr.Err, &scErr); ok {
|
||||
if errors.As(opErr.Err, &scErr) {
|
||||
return scErr.Err == _WSAEMSGSIZE
|
||||
}
|
||||
return opErr.Err == _WSAEMSGSIZE
|
||||
|
|
|
|||
|
|
@ -273,7 +273,7 @@ loop:
|
|||
writeStart <- struct{}{}
|
||||
case err = <-readErr:
|
||||
var r DiscReason
|
||||
if ok := errors.As(err, &r); ok {
|
||||
if errors.As(err, &r) {
|
||||
remoteRequested = true
|
||||
reason = r
|
||||
} else {
|
||||
|
|
|
|||
|
|
@ -104,7 +104,7 @@ func (d DiscReason) Error() string {
|
|||
|
||||
func discReasonForError(err error) DiscReason {
|
||||
var reason DiscReason
|
||||
if ok := errors.As(err, &reason); ok {
|
||||
if errors.As(err, &reason) {
|
||||
return reason
|
||||
}
|
||||
if errors.Is(err, errProtocolReturned) {
|
||||
|
|
|
|||
|
|
@ -114,7 +114,7 @@ func (t *rlpxTransport) close(err error) {
|
|||
// We only bother doing this if the underlying connection supports
|
||||
// setting a timeout tough.
|
||||
var reason DiscReason
|
||||
if ok := errors.As(err, &reason); ok && reason != DiscNetworkError {
|
||||
if errors.As(err, &reason) && reason != DiscNetworkError {
|
||||
// We do not use the WriteMsg func since we want a custom deadline
|
||||
deadline := time.Now().Add(discWriteTimeout)
|
||||
if err := t.conn.SetWriteDeadline(deadline); err == nil {
|
||||
|
|
|
|||
|
|
@ -142,7 +142,7 @@ func wrapStreamError(err error, typ reflect.Type) error {
|
|||
|
||||
func addErrorContext(err error, ctx string) error {
|
||||
decErr := new(decodeError)
|
||||
if ok := errors.As(err, &decErr); ok {
|
||||
if errors.As(err, &decErr) {
|
||||
decErr.ctx = append(decErr.ctx, ctx)
|
||||
}
|
||||
return err
|
||||
|
|
@ -950,7 +950,7 @@ func (s *Stream) Decode(val interface{}) error {
|
|||
|
||||
err = decoder(s, rval.Elem())
|
||||
decErr := new(decodeError)
|
||||
if ok := errors.As(err, &decErr); ok && len(decErr.ctx) > 0 {
|
||||
if errors.As(err, &decErr) && len(decErr.ctx) > 0 {
|
||||
// Add decode target type to error so context has more meaning.
|
||||
decErr.ctx = append(decErr.ctx, fmt.Sprint("(", rtyp.Elem(), ")"))
|
||||
}
|
||||
|
|
|
|||
|
|
@ -142,7 +142,7 @@ func structFields(typ reflect.Type) (fields []field, err error) {
|
|||
structFields, structTags, err := rlpstruct.ProcessFields(allStructFields)
|
||||
if err != nil {
|
||||
tagErr := new(rlpstruct.TagError)
|
||||
if ok := errors.As(err, &tagErr); ok {
|
||||
if errors.As(err, &tagErr) {
|
||||
tagErr.StructType = typ.String()
|
||||
return nil, tagErr
|
||||
}
|
||||
|
|
|
|||
|
|
@ -714,7 +714,7 @@ func (c *Client) read(codec ServerCodec) {
|
|||
for {
|
||||
msgs, batch, err := codec.readBatch()
|
||||
jsonErr := new(json.SyntaxError)
|
||||
if ok := errors.As(err, &jsonErr); ok {
|
||||
if errors.As(err, &jsonErr) {
|
||||
msg := errorMessage(&parseError{err.Error()})
|
||||
codec.writeJSON(context.Background(), msg, true)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -115,7 +115,7 @@ func TestClientErrorData(t *testing.T) {
|
|||
// interface, i.e. it has a custom error code. The server returns this error code.
|
||||
expectedCode := testError{}.ErrorCode()
|
||||
var e Error
|
||||
if ok := errors.As(err, &e); !ok {
|
||||
if !errors.As(err, &e) {
|
||||
t.Fatalf("client did not return rpc.Error, got %#v", e)
|
||||
} else if e.ErrorCode() != expectedCode {
|
||||
t.Fatalf("wrong error code %d, want %d", e.ErrorCode(), expectedCode)
|
||||
|
|
@ -123,7 +123,7 @@ func TestClientErrorData(t *testing.T) {
|
|||
|
||||
// Check data.
|
||||
var dataErr DataError
|
||||
if ok := errors.As(err, &dataErr); !ok {
|
||||
if !errors.As(err, &dataErr) {
|
||||
t.Fatalf("client did not return rpc.DataError, got %#v", dataErr)
|
||||
} else if dataErr.ErrorData() != (testError{}.ErrorData()) {
|
||||
t.Fatalf("wrong error data %#v, want %#v", dataErr.ErrorData(), testError{}.ErrorData())
|
||||
|
|
|
|||
|
|
@ -53,7 +53,7 @@ func readJSON(reader io.Reader, value interface{}) error {
|
|||
}
|
||||
if err = json.Unmarshal(data, &value); err != nil {
|
||||
syntaxerr := new(json.SyntaxError)
|
||||
if ok := errors.As(err, &syntaxerr); ok {
|
||||
if errors.As(err, &syntaxerr) {
|
||||
line := findLine(data, syntaxerr.Offset)
|
||||
return fmt.Errorf("JSON syntax error at line %v: %v", line, err)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -272,7 +272,7 @@ func (it *nodeIterator) Error() error {
|
|||
return nil
|
||||
}
|
||||
var seek seekError
|
||||
if ok := errors.As(it.err, &seek); ok {
|
||||
if errors.As(it.err, &seek) {
|
||||
return seek.err
|
||||
}
|
||||
return it.err
|
||||
|
|
@ -287,7 +287,7 @@ func (it *nodeIterator) Next(descend bool) bool {
|
|||
return false
|
||||
}
|
||||
var seek seekError
|
||||
if ok := errors.As(it.err, &seek); ok {
|
||||
if errors.As(it.err, &seek) {
|
||||
if it.err = it.seek(seek.key); it.err != nil {
|
||||
return false
|
||||
}
|
||||
|
|
|
|||
|
|
@ -244,7 +244,7 @@ func wrapError(err error, ctx string) error {
|
|||
return nil
|
||||
}
|
||||
decErr := new(decodeError)
|
||||
if ok := errors.As(err, &decErr); ok {
|
||||
if errors.As(err, &decErr) {
|
||||
decErr.stack = append(decErr.stack, ctx)
|
||||
return decErr
|
||||
}
|
||||
|
|
|
|||
|
|
@ -61,7 +61,7 @@ func TestDecodeFullNodeWrongSizeChild(t *testing.T) {
|
|||
|
||||
_, err := decodeNode([]byte("testdecode"), buf.Bytes())
|
||||
decodeErr := new(decodeError)
|
||||
if ok := errors.As(err, &decodeErr); !ok {
|
||||
if !errors.As(err, &decodeErr) {
|
||||
t.Fatalf("decodeNode returned wrong err: %v", err)
|
||||
}
|
||||
}
|
||||
|
|
@ -81,7 +81,7 @@ func TestDecodeFullNodeWrongNestedFullNode(t *testing.T) {
|
|||
|
||||
_, err := decodeNode([]byte("testdecode"), buf.Bytes())
|
||||
decodeErr := new(decodeError)
|
||||
if ok := errors.As(err, &decodeErr); !ok {
|
||||
if !errors.As(err, &decodeErr) {
|
||||
t.Fatalf("decodeNode returned wrong err: %v", err)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -79,7 +79,7 @@ func testMissingRoot(t *testing.T, scheme string) {
|
|||
t.Error("New returned non-nil trie for invalid root")
|
||||
}
|
||||
missingNodeErr := new(MissingNodeError)
|
||||
if ok := errors.As(err, &missingNodeErr); !ok {
|
||||
if !errors.As(err, &missingNodeErr) {
|
||||
t.Errorf("New returned wrong error: %v", err)
|
||||
}
|
||||
}
|
||||
|
|
@ -150,11 +150,11 @@ func testMissingNode(t *testing.T, memonly bool, scheme string) {
|
|||
|
||||
_, err = trie.Get([]byte("120000"))
|
||||
missingNodeErr := new(MissingNodeError)
|
||||
if ok := errors.As(err, &missingNodeErr); !ok {
|
||||
if !errors.As(err, &missingNodeErr) {
|
||||
t.Errorf("Wrong error: %v", err)
|
||||
}
|
||||
_, err = trie.Get([]byte("120099"))
|
||||
if ok := errors.As(err, &missingNodeErr); !ok {
|
||||
if !errors.As(err, &missingNodeErr) {
|
||||
t.Errorf("Wrong error: %v", err)
|
||||
}
|
||||
_, err = trie.Get([]byte("123456"))
|
||||
|
|
@ -162,11 +162,11 @@ func testMissingNode(t *testing.T, memonly bool, scheme string) {
|
|||
t.Errorf("Unexpected error: %v", err)
|
||||
}
|
||||
err = trie.Update([]byte("120099"), []byte("zxcv"))
|
||||
if ok := errors.As(err, &missingNodeErr); !ok {
|
||||
if !errors.As(err, &missingNodeErr) {
|
||||
t.Errorf("Wrong error: %v", err)
|
||||
}
|
||||
err = trie.Delete([]byte("123456"))
|
||||
if ok := errors.As(err, &missingNodeErr); !ok {
|
||||
if !errors.As(err, &missingNodeErr) {
|
||||
t.Errorf("Wrong error: %v", err)
|
||||
}
|
||||
}
|
||||
|
|
@ -625,7 +625,7 @@ func runRandTest(rt randTest) error {
|
|||
func TestRandom(t *testing.T) {
|
||||
if err := quick.Check(runRandTestBool, nil); err != nil {
|
||||
cerr := new(quick.CheckError)
|
||||
if ok := errors.As(err, &cerr); ok {
|
||||
if errors.As(err, &cerr) {
|
||||
t.Fatalf("random test iteration %d failed: %s", cerr.Count, spew.Sdump(cerr.In))
|
||||
}
|
||||
t.Fatal(err)
|
||||
|
|
|
|||
Loading…
Reference in a new issue