Skip to content

Commit

Permalink
Error improvements for lint.
Browse files Browse the repository at this point in the history
Rename errors to Err*.
Lowercase error text.

Add golint for top level package

ok @davecgh
  • Loading branch information
jcvernaleo committed Sep 16, 2014
1 parent 372bbe5 commit a7ac93f
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 28 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ install:
script:
- export PATH=$PATH:$HOME/gopath/bin
- go vet ./...
- fgt golint .
- fgt golint memdb
- fgt golint ldb
- go test -v
16 changes: 8 additions & 8 deletions db.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ import (

// Errors that the various database functions may return.
var (
PrevShaMissing = errors.New("Previous sha missing from database")
TxShaMissing = errors.New("Requested transaction does not exist")
BlockShaMissing = errors.New("Requested block does not exist")
DuplicateSha = errors.New("Duplicate insert attempted")
DbDoesNotExist = errors.New("Non-existent database")
DbUnknownType = errors.New("Non-existent database type")
ErrPrevShaMissing = errors.New("previous sha missing from database")
ErrTxShaMissing = errors.New("requested transaction does not exist")
ErrBlockShaMissing = errors.New("requested block does not exist")
ErrDuplicateSha = errors.New("duplicate insert attempted")
ErrDbDoesNotExist = errors.New("non-existent database")
ErrDbUnknownType = errors.New("non-existent database type")
)

// AllShas is a special value that can be used as the final sha when requesting
Expand Down Expand Up @@ -156,7 +156,7 @@ func CreateDB(dbtype string, args ...interface{}) (pbdb Db, err error) {
return drv.CreateDB(args...)
}
}
return nil, DbUnknownType
return nil, ErrDbUnknownType
}

// OpenDB opens an existing database.
Expand All @@ -166,7 +166,7 @@ func OpenDB(dbtype string, args ...interface{}) (pbdb Db, err error) {
return drv.OpenDB(args...)
}
}
return nil, DbUnknownType
return nil, ErrDbUnknownType
}

// SupportedDBs returns a slice of strings that represent the database drivers
Expand Down
4 changes: 2 additions & 2 deletions ldb/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func (db *LevelDb) getBlkLoc(sha *btcwire.ShaHash) (int64, error) {
data, err := db.lDb.Get(key, db.ro)
if err != nil {
if err == leveldb.ErrNotFound {
err = btcdb.BlockShaMissing
err = btcdb.ErrBlockShaMissing
}
return 0, err
}
Expand Down Expand Up @@ -206,7 +206,7 @@ func (db *LevelDb) blkExistsSha(sha *btcwire.ShaHash) (bool, error) {
switch err {
case nil:
return true, nil
case leveldb.ErrNotFound, btcdb.BlockShaMissing:
case leveldb.ErrNotFound, btcdb.ErrBlockShaMissing:
return false, nil
}
return false, err
Expand Down
2 changes: 1 addition & 1 deletion ldb/internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func FetchSha(db btcdb.Db, sha *btcwire.ShaHash) (buf []byte, pver uint32,
blkid int64, err error) {
sqldb, ok := db.(*LevelDb)
if !ok {
err = fmt.Errorf("Invalid data type")
err = fmt.Errorf("invalid data type")
return
}
buf, blkid, err = sqldb.fetchSha(sha)
Expand Down
2 changes: 1 addition & 1 deletion ldb/leveldb.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ func openDB(dbpath string, create bool) (pbdb btcdb.Db, err error) {
} else {
_, err = os.Stat(dbpath)
if err != nil {
err = btcdb.DbDoesNotExist
err = btcdb.ErrDbDoesNotExist
return
}
}
Expand Down
14 changes: 7 additions & 7 deletions ldb/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func (db *LevelDb) getTxFullySpent(txsha *btcwire.ShaHash) ([]*spentTx, error) {
key := shaSpentTxToKey(txsha)
buf, err := db.lDb.Get(key, db.ro)
if err == leveldb.ErrNotFound {
return badTxList, btcdb.TxShaMissing
return badTxList, btcdb.ErrTxShaMissing
} else if err != nil {
return badTxList, err
}
Expand Down Expand Up @@ -186,7 +186,7 @@ func (db *LevelDb) FetchTxByShaList(txShaList []*btcwire.ShaHash) []*btcdb.TxLis
btxspent[idx] = (txspent[byteidx] & (byte(1) << byteoff)) != 0
}
}
if err == btcdb.TxShaMissing {
if err == btcdb.ErrTxShaMissing {
// if the unspent pool did not have the tx,
// look in the fully spent pool (only last instance

Expand Down Expand Up @@ -244,7 +244,7 @@ func (db *LevelDb) fetchTxDataBySha(txsha *btcwire.ShaHash) (rtx *btcwire.MsgTx,
blkHeight, txOff, txLen, txspent, err = db.getTxData(txsha)
if err != nil {
if err == leveldb.ErrNotFound {
err = btcdb.TxShaMissing
err = btcdb.ErrTxShaMissing
}
return
}
Expand All @@ -260,7 +260,7 @@ func (db *LevelDb) fetchTxDataByLoc(blkHeight int64, txOff int, txLen int, txspe
blksha, blkbuf, err = db.getBlkByHeight(blkHeight)
if err != nil {
if err == leveldb.ErrNotFound {
err = btcdb.TxShaMissing
err = btcdb.ErrTxShaMissing
}
return
}
Expand All @@ -269,7 +269,7 @@ func (db *LevelDb) fetchTxDataByLoc(blkHeight int64, txOff int, txLen int, txspe
// txsha, blksha, blkHeight, txOff, txLen)

if len(blkbuf) < txOff+txLen {
err = btcdb.TxShaMissing
err = btcdb.ErrTxShaMissing
return
}
rbuf := bytes.NewReader(blkbuf[txOff : txOff+txLen])
Expand Down Expand Up @@ -297,15 +297,15 @@ func (db *LevelDb) FetchTxBySha(txsha *btcwire.ShaHash) ([]*btcdb.TxListReply, e
if txerr == nil {
replylen++
} else {
if txerr != btcdb.TxShaMissing {
if txerr != btcdb.ErrTxShaMissing {
return []*btcdb.TxListReply{}, txerr
}
}

sTxList, fSerr := db.getTxFullySpent(txsha)

if fSerr != nil {
if fSerr != btcdb.TxShaMissing {
if fSerr != btcdb.ErrTxShaMissing {
return []*btcdb.TxListReply{}, fSerr
}
} else {
Expand Down
18 changes: 9 additions & 9 deletions memdb/memdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (

// Errors that the various database functions may return.
var (
ErrDbClosed = errors.New("Database is closed")
ErrDbClosed = errors.New("database is closed")
)

var (
Expand Down Expand Up @@ -383,7 +383,7 @@ func (db *MemDb) FetchTxBySha(txHash *btcwire.ShaHash) ([]*btcdb.TxListReply, er
if !exists {
log.Warnf("FetchTxBySha: requested hash of %s does not exist",
txHash)
return nil, btcdb.TxShaMissing
return nil, btcdb.ErrTxShaMissing
}

txHashCopy := *txHash
Expand Down Expand Up @@ -431,7 +431,7 @@ func (db *MemDb) fetchTxByShaList(txShaList []*btcwire.ShaHash, includeSpent boo
// information if the transaction exists.
reply := btcdb.TxListReply{
Sha: txShaList[i],
Err: btcdb.TxShaMissing,
Err: btcdb.ErrTxShaMissing,
}
replyList = append(replyList, &reply)

Expand Down Expand Up @@ -548,7 +548,7 @@ func (db *MemDb) InsertBlock(block *btcutil.Block) (int64, error) {
msgBlock := block.MsgBlock()
if _, exists := db.blocksBySha[msgBlock.Header.PrevBlock]; !exists {
if len(db.blocks) > 0 {
return 0, btcdb.PrevShaMissing
return 0, btcdb.ErrPrevShaMissing
}
}

Expand Down Expand Up @@ -599,22 +599,22 @@ func (db *MemDb) InsertBlock(block *btcutil.Block) (int64, error) {
log.Warnf("InsertBlock: requested hash "+
" of %s does not exist in-flight",
tx.Sha())
return 0, btcdb.TxShaMissing
return 0, btcdb.ErrTxShaMissing
}
} else {
originTxns, exists := db.txns[prevOut.Hash]
if !exists {
log.Warnf("InsertBlock: requested hash "+
"of %s by %s does not exist",
prevOut.Hash, tx.Sha())
return 0, btcdb.TxShaMissing
return 0, btcdb.ErrTxShaMissing
}
originTxD := originTxns[len(originTxns)-1]
if prevOut.Index > uint32(len(originTxD.spentBuf)) {
log.Warnf("InsertBlock: requested hash "+
"of %s with index %d does not "+
"exist", tx.Sha(), prevOut.Index)
return 0, btcdb.TxShaMissing
return 0, btcdb.ErrTxShaMissing
}
}
}
Expand All @@ -624,7 +624,7 @@ func (db *MemDb) InsertBlock(block *btcutil.Block) (int64, error) {
inFlightIndex < i {
log.Warnf("Block contains duplicate transaction %s",
tx.Sha())
return 0, btcdb.DuplicateSha
return 0, btcdb.ErrDuplicateSha
}

// Prevent duplicate transactions unless the old one is fully
Expand All @@ -634,7 +634,7 @@ func (db *MemDb) InsertBlock(block *btcutil.Block) (int64, error) {
if !isFullySpent(txD) {
log.Warnf("Attempt to insert duplicate "+
"transaction %s", tx.Sha())
return 0, btcdb.DuplicateSha
return 0, btcdb.ErrDuplicateSha
}
}
}
Expand Down

0 comments on commit a7ac93f

Please sign in to comment.