Skip to content

Commit

Permalink
database: Fix errorlint warnings.
Browse files Browse the repository at this point in the history
  • Loading branch information
dajohi authored Nov 30, 2020
1 parent 6dc037c commit c56c9ea
Show file tree
Hide file tree
Showing 9 changed files with 63 additions and 50 deletions.
11 changes: 7 additions & 4 deletions database/cmd/dbtool/insecureimport.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
// Copyright (c) 2015-2016 The btcsuite developers
// Copyright (c) 2016-2019 The Decred developers
// Copyright (c) 2016-2020 The Decred developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.

package main

import (
"encoding/binary"
"errors"
"fmt"
"io"
"os"
Expand Down Expand Up @@ -70,7 +71,7 @@ func (bi *blockImporter) readBlock() ([]byte, error) {
var net uint32
err := binary.Read(bi.r, binary.LittleEndian, &net)
if err != nil {
if err != io.EOF {
if !errors.Is(err, io.EOF) {
return nil, err
}

Expand Down Expand Up @@ -376,8 +377,10 @@ func (cmd *importCmd) Execute(args []string) error {
resultsChan := importer.Import()
results := <-resultsChan
if results.err != nil {
dbErr, ok := results.err.(database.Error)
if !ok || ok && dbErr.ErrorCode != database.ErrDbNotOpen {
var dbErr database.Error
if !errors.As(results.err, &dbErr) ||
dbErr.ErrorCode != database.ErrDbNotOpen {

shutdownChannel <- results.err
return
}
Expand Down
9 changes: 6 additions & 3 deletions database/cmd/dbtool/main.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
// Copyright (c) 2015-2016 The btcsuite developers
// Copyright (c) 2016-2019 The Decred developers
// Copyright (c) 2016-2020 The Decred developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.

package main

import (
"errors"
"os"
"path/filepath"
"runtime"
Expand Down Expand Up @@ -37,7 +38,8 @@ func loadBlockDB() (database.DB, error) {
if err != nil {
// Return the error if it's not because the database doesn't
// exist.
if dbErr, ok := err.(database.Error); !ok || dbErr.ErrorCode !=
var dbErr database.Error
if !errors.As(err, &dbErr) || dbErr.ErrorCode !=
database.ErrDbDoesNotExist {

return nil, err
Expand Down Expand Up @@ -94,7 +96,8 @@ func realMain() error {
// Parse command line and invoke the Execute function for the specified
// command.
if _, err := parser.Parse(); err != nil {
if e, ok := err.(*flags.Error); ok && e.Type == flags.ErrHelp {
var e *flags.Error
if errors.As(err, &e) && e.Type == flags.ErrHelp {
parser.WriteHelp(os.Stderr)
} else {
log.Error(err)
Expand Down
11 changes: 6 additions & 5 deletions database/driver_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
// Copyright (c) 2015-2016 The btcsuite developers
// Copyright (c) 2016-2019 The Decred developers
// Copyright (c) 2016-2020 The Decred developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.

package database_test

import (
"errors"
"fmt"
"testing"

Expand All @@ -16,8 +17,8 @@ import (
// checkDbError ensures the passed error is a database.Error with an error code
// that matches the passed error code.
func checkDbError(t *testing.T, testName string, gotErr error, wantErrCode database.ErrorCode) bool {
dbErr, ok := gotErr.(database.Error)
if !ok {
var dbErr database.Error
if !errors.As(gotErr, &dbErr) {
t.Errorf("%s: unexpected error type - got %T, want %T",
testName, gotErr, database.Error{})
return false
Expand Down Expand Up @@ -91,7 +92,7 @@ func TestCreateOpenFail(t *testing.T) {
// Ensure creating a database with the new type fails with the expected
// error.
_, err := database.Create(dbType)
if err != openError {
if !errors.Is(err, openError) {
t.Errorf("expected error not received - got: %v, want %v", err,
openError)
return
Expand All @@ -100,7 +101,7 @@ func TestCreateOpenFail(t *testing.T) {
// Ensure opening a database with the new type fails with the expected
// error.
_, err = database.Open(dbType)
if err != openError {
if !errors.Is(err, openError) {
t.Errorf("expected error not received - got: %v, want %v", err,
openError)
return
Expand Down
11 changes: 7 additions & 4 deletions database/error.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
// Copyright (c) 2015-2016 The btcsuite developers
// Copyright (c) 2016 The Decred developers
// Copyright (c) 2016-2020 The Decred developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.

package database

import "fmt"
import (
"errors"
"fmt"
)

// ErrorCode identifies a kind of error.
type ErrorCode int
Expand Down Expand Up @@ -199,6 +202,6 @@ func makeError(c ErrorCode, desc string, err error) Error {

// IsError returns whether err is an Error with a matching error code.
func IsError(err error, code ErrorCode) bool {
e, ok := err.(Error)
return ok && e.ErrorCode == code
var e Error
return errors.As(err, &e) && e.ErrorCode == code
}
7 changes: 4 additions & 3 deletions database/ffldb/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package ffldb
import (
"bytes"
"encoding/binary"
"errors"
"fmt"
"os"
"path/filepath"
Expand Down Expand Up @@ -145,13 +146,13 @@ func convertErr(desc string, ldbErr error) database.Error {
code = database.ErrCorruption

// Database open/create errors.
case ldbErr == leveldb.ErrClosed:
case errors.Is(ldbErr, leveldb.ErrClosed):
code = database.ErrDbNotOpen

// Transaction errors.
case ldbErr == leveldb.ErrSnapshotReleased:
case errors.Is(ldbErr, leveldb.ErrSnapshotReleased):
code = database.ErrTxClosed
case ldbErr == leveldb.ErrIterReleased:
case errors.Is(ldbErr, leveldb.ErrIterReleased):
code = database.ErrTxClosed
}

Expand Down
8 changes: 4 additions & 4 deletions database/ffldb/driver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,20 +183,20 @@ func TestPersistence(t *testing.T) {

bucket1, err := metadataBucket.CreateBucket(bucket1Key)
if err != nil {
return fmt.Errorf("CreateBucket: unexpected error: %v",
return fmt.Errorf("CreateBucket: unexpected error: %w",
err)
}

for k, v := range storeValues {
err := bucket1.Put([]byte(k), []byte(v))
if err != nil {
return fmt.Errorf("Put: unexpected error: %v",
return fmt.Errorf("Put: unexpected error: %w",
err)
}
}

if err := tx.StoreBlock(genesisBlock); err != nil {
return fmt.Errorf("StoreBlock: unexpected error: %v",
return fmt.Errorf("StoreBlock: unexpected error: %w",
err)
}

Expand Down Expand Up @@ -241,7 +241,7 @@ func TestPersistence(t *testing.T) {
genesisBlockBytes, _ := genesisBlock.Bytes()
gotBytes, err := tx.FetchBlock(genesisHash)
if err != nil {
return fmt.Errorf("FetchBlock: unexpected error: %v",
return fmt.Errorf("FetchBlock: unexpected error: %w",
err)
}
if !reflect.DeepEqual(gotBytes, genesisBlockBytes) {
Expand Down
41 changes: 21 additions & 20 deletions database/ffldb/interface_test.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright (c) 2015-2016 The btcsuite developers
// Copyright (c) 2016-2019 The Decred developers
// Copyright (c) 2016-2020 The Decred developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.

Expand All @@ -17,6 +17,7 @@ import (
"bytes"
"compress/bzip2"
"encoding/gob"
"errors"
"fmt"
"os"
"path/filepath"
Expand Down Expand Up @@ -91,8 +92,8 @@ func loadBlocks(t *testing.T, dataFile string, network wire.CurrencyNet) ([]*dcr
// checkDbError ensures the passed error is a database.Error with an error code
// that matches the passed error code.
func checkDbError(t *testing.T, testName string, gotErr error, wantErrCode database.ErrorCode) bool {
dbErr, ok := gotErr.(database.Error)
if !ok {
var dbErr database.Error
if !errors.As(gotErr, &dbErr) {
t.Errorf("%s: unexpected error type - got %T, want %T",
testName, gotErr, database.Error{})
return false
Expand Down Expand Up @@ -407,7 +408,7 @@ func testBucketInterface(tc *testContext, bucket database.Bucket) bool {
err := bucket.ForEach(func(k, v []byte) error {
return forEachError
})
if err != forEachError {
if !errors.Is(err, forEachError) {
tc.t.Errorf("ForEach: inner function error not "+
"returned - got %v, want %v", err, forEachError)
return false
Expand Down Expand Up @@ -470,7 +471,7 @@ func testBucketInterface(tc *testContext, bucket database.Bucket) bool {
err = bucket.ForEachBucket(func(k []byte) error {
return forEachError
})
if err != forEachError {
if !errors.Is(err, forEachError) {
tc.t.Errorf("ForEachBucket: inner function error not "+
"returned - got %v, want %v", err, forEachError)
return false
Expand Down Expand Up @@ -928,7 +929,7 @@ func testMetadataTxInterface(tc *testContext) bool {
return nil
})
if err != nil {
if err != errSubTestFail {
if !errors.Is(err, errSubTestFail) {
tc.t.Errorf("%v", err)
}
return false
Expand All @@ -940,7 +941,7 @@ func testMetadataTxInterface(tc *testContext) bool {
err = tc.db.View(func(tx database.Tx) error {
return viewError
})
if err != viewError {
if !errors.Is(err, viewError) {
tc.t.Errorf("View: inner function error not returned - got "+
"%v, want %v", err, viewError)
return false
Expand Down Expand Up @@ -973,8 +974,8 @@ func testMetadataTxInterface(tc *testContext) bool {
// Return an error to force a rollback.
return forceRollbackError
})
if err != forceRollbackError {
if err == errSubTestFail {
if !errors.Is(err, forceRollbackError) {
if errors.Is(err, errSubTestFail) {
return false
}

Expand All @@ -998,7 +999,7 @@ func testMetadataTxInterface(tc *testContext) bool {
return nil
})
if err != nil {
if err != errSubTestFail {
if !errors.Is(err, errSubTestFail) {
tc.t.Errorf("%v", err)
}
return false
Expand All @@ -1023,7 +1024,7 @@ func testMetadataTxInterface(tc *testContext) bool {
return nil
})
if err != nil {
if err != errSubTestFail {
if !errors.Is(err, errSubTestFail) {
tc.t.Errorf("%v", err)
}
return false
Expand All @@ -1048,7 +1049,7 @@ func testMetadataTxInterface(tc *testContext) bool {
return nil
})
if err != nil {
if err != errSubTestFail {
if !errors.Is(err, errSubTestFail) {
tc.t.Errorf("%v", err)
}
return false
Expand All @@ -1073,7 +1074,7 @@ func testMetadataTxInterface(tc *testContext) bool {
return nil
})
if err != nil {
if err != errSubTestFail {
if !errors.Is(err, errSubTestFail) {
tc.t.Errorf("%v", err)
}
return false
Expand Down Expand Up @@ -1498,7 +1499,7 @@ func testBlockIOTxInterface(tc *testContext) bool {
return nil
})
if err != nil {
if err != errSubTestFail {
if !errors.Is(err, errSubTestFail) {
tc.t.Errorf("%v", err)
}
return false
Expand Down Expand Up @@ -1540,8 +1541,8 @@ func testBlockIOTxInterface(tc *testContext) bool {

return forceRollbackError
})
if err != forceRollbackError {
if err == errSubTestFail {
if !errors.Is(err, forceRollbackError) {
if errors.Is(err, errSubTestFail) {
return false
}

Expand All @@ -1558,7 +1559,7 @@ func testBlockIOTxInterface(tc *testContext) bool {
return nil
})
if err != nil {
if err != errSubTestFail {
if !errors.Is(err, errSubTestFail) {
tc.t.Errorf("%v", err)
}
return false
Expand Down Expand Up @@ -1599,7 +1600,7 @@ func testBlockIOTxInterface(tc *testContext) bool {
return nil
})
if err != nil {
if err != errSubTestFail {
if !errors.Is(err, errSubTestFail) {
tc.t.Errorf("%v", err)
}
return false
Expand All @@ -1616,7 +1617,7 @@ func testBlockIOTxInterface(tc *testContext) bool {
return nil
})
if err != nil {
if err != errSubTestFail {
if !errors.Is(err, errSubTestFail) {
tc.t.Errorf("%v", err)
}
return false
Expand Down Expand Up @@ -1647,7 +1648,7 @@ func testBlockIOTxInterface(tc *testContext) bool {
return nil
})
if err != nil {
if err != errSubTestFail {
if !errors.Is(err, errSubTestFail) {
tc.t.Errorf("%v", err)
}
return false
Expand Down
Loading

0 comments on commit c56c9ea

Please sign in to comment.