Error improvements for lint.

Rename errors to Err*.
Lowercase error text.

Add golint for top level package

ok @davecgh
This commit is contained in:
John C. Vernaleo 2014-09-16 14:43:09 -04:00
parent 372bbe5c49
commit a7ac93f5b6
7 changed files with 29 additions and 28 deletions

View file

@ -11,6 +11,7 @@ install:
script: script:
- export PATH=$PATH:$HOME/gopath/bin - export PATH=$PATH:$HOME/gopath/bin
- go vet ./... - go vet ./...
- fgt golint .
- fgt golint memdb - fgt golint memdb
- fgt golint ldb - fgt golint ldb
- go test -v - go test -v

16
db.go
View file

@ -13,12 +13,12 @@ import (
// Errors that the various database functions may return. // Errors that the various database functions may return.
var ( var (
PrevShaMissing = errors.New("Previous sha missing from database") ErrPrevShaMissing = errors.New("previous sha missing from database")
TxShaMissing = errors.New("Requested transaction does not exist") ErrTxShaMissing = errors.New("requested transaction does not exist")
BlockShaMissing = errors.New("Requested block does not exist") ErrBlockShaMissing = errors.New("requested block does not exist")
DuplicateSha = errors.New("Duplicate insert attempted") ErrDuplicateSha = errors.New("duplicate insert attempted")
DbDoesNotExist = errors.New("Non-existent database") ErrDbDoesNotExist = errors.New("non-existent database")
DbUnknownType = errors.New("Non-existent database type") ErrDbUnknownType = errors.New("non-existent database type")
) )
// AllShas is a special value that can be used as the final sha when requesting // AllShas is a special value that can be used as the final sha when requesting
@ -156,7 +156,7 @@ func CreateDB(dbtype string, args ...interface{}) (pbdb Db, err error) {
return drv.CreateDB(args...) return drv.CreateDB(args...)
} }
} }
return nil, DbUnknownType return nil, ErrDbUnknownType
} }
// OpenDB opens an existing database. // OpenDB opens an existing database.
@ -166,7 +166,7 @@ func OpenDB(dbtype string, args ...interface{}) (pbdb Db, err error) {
return drv.OpenDB(args...) return drv.OpenDB(args...)
} }
} }
return nil, DbUnknownType return nil, ErrDbUnknownType
} }
// SupportedDBs returns a slice of strings that represent the database drivers // SupportedDBs returns a slice of strings that represent the database drivers

View file

@ -77,7 +77,7 @@ func (db *LevelDb) getBlkLoc(sha *btcwire.ShaHash) (int64, error) {
data, err := db.lDb.Get(key, db.ro) data, err := db.lDb.Get(key, db.ro)
if err != nil { if err != nil {
if err == leveldb.ErrNotFound { if err == leveldb.ErrNotFound {
err = btcdb.BlockShaMissing err = btcdb.ErrBlockShaMissing
} }
return 0, err return 0, err
} }
@ -206,7 +206,7 @@ func (db *LevelDb) blkExistsSha(sha *btcwire.ShaHash) (bool, error) {
switch err { switch err {
case nil: case nil:
return true, nil return true, nil
case leveldb.ErrNotFound, btcdb.BlockShaMissing: case leveldb.ErrNotFound, btcdb.ErrBlockShaMissing:
return false, nil return false, nil
} }
return false, err return false, err

View file

@ -17,7 +17,7 @@ func FetchSha(db btcdb.Db, sha *btcwire.ShaHash) (buf []byte, pver uint32,
blkid int64, err error) { blkid int64, err error) {
sqldb, ok := db.(*LevelDb) sqldb, ok := db.(*LevelDb)
if !ok { if !ok {
err = fmt.Errorf("Invalid data type") err = fmt.Errorf("invalid data type")
return return
} }
buf, blkid, err = sqldb.fetchSha(sha) buf, blkid, err = sqldb.fetchSha(sha)

View file

@ -175,7 +175,7 @@ func openDB(dbpath string, create bool) (pbdb btcdb.Db, err error) {
} else { } else {
_, err = os.Stat(dbpath) _, err = os.Stat(dbpath)
if err != nil { if err != nil {
err = btcdb.DbDoesNotExist err = btcdb.ErrDbDoesNotExist
return return
} }
} }

View file

@ -99,7 +99,7 @@ func (db *LevelDb) getTxFullySpent(txsha *btcwire.ShaHash) ([]*spentTx, error) {
key := shaSpentTxToKey(txsha) key := shaSpentTxToKey(txsha)
buf, err := db.lDb.Get(key, db.ro) buf, err := db.lDb.Get(key, db.ro)
if err == leveldb.ErrNotFound { if err == leveldb.ErrNotFound {
return badTxList, btcdb.TxShaMissing return badTxList, btcdb.ErrTxShaMissing
} else if err != nil { } else if err != nil {
return badTxList, err return badTxList, err
} }
@ -186,7 +186,7 @@ func (db *LevelDb) FetchTxByShaList(txShaList []*btcwire.ShaHash) []*btcdb.TxLis
btxspent[idx] = (txspent[byteidx] & (byte(1) << byteoff)) != 0 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, // if the unspent pool did not have the tx,
// look in the fully spent pool (only last instance // look in the fully spent pool (only last instance
@ -244,7 +244,7 @@ func (db *LevelDb) fetchTxDataBySha(txsha *btcwire.ShaHash) (rtx *btcwire.MsgTx,
blkHeight, txOff, txLen, txspent, err = db.getTxData(txsha) blkHeight, txOff, txLen, txspent, err = db.getTxData(txsha)
if err != nil { if err != nil {
if err == leveldb.ErrNotFound { if err == leveldb.ErrNotFound {
err = btcdb.TxShaMissing err = btcdb.ErrTxShaMissing
} }
return return
} }
@ -260,7 +260,7 @@ func (db *LevelDb) fetchTxDataByLoc(blkHeight int64, txOff int, txLen int, txspe
blksha, blkbuf, err = db.getBlkByHeight(blkHeight) blksha, blkbuf, err = db.getBlkByHeight(blkHeight)
if err != nil { if err != nil {
if err == leveldb.ErrNotFound { if err == leveldb.ErrNotFound {
err = btcdb.TxShaMissing err = btcdb.ErrTxShaMissing
} }
return return
} }
@ -269,7 +269,7 @@ func (db *LevelDb) fetchTxDataByLoc(blkHeight int64, txOff int, txLen int, txspe
// txsha, blksha, blkHeight, txOff, txLen) // txsha, blksha, blkHeight, txOff, txLen)
if len(blkbuf) < txOff+txLen { if len(blkbuf) < txOff+txLen {
err = btcdb.TxShaMissing err = btcdb.ErrTxShaMissing
return return
} }
rbuf := bytes.NewReader(blkbuf[txOff : txOff+txLen]) rbuf := bytes.NewReader(blkbuf[txOff : txOff+txLen])
@ -297,7 +297,7 @@ func (db *LevelDb) FetchTxBySha(txsha *btcwire.ShaHash) ([]*btcdb.TxListReply, e
if txerr == nil { if txerr == nil {
replylen++ replylen++
} else { } else {
if txerr != btcdb.TxShaMissing { if txerr != btcdb.ErrTxShaMissing {
return []*btcdb.TxListReply{}, txerr return []*btcdb.TxListReply{}, txerr
} }
} }
@ -305,7 +305,7 @@ func (db *LevelDb) FetchTxBySha(txsha *btcwire.ShaHash) ([]*btcdb.TxListReply, e
sTxList, fSerr := db.getTxFullySpent(txsha) sTxList, fSerr := db.getTxFullySpent(txsha)
if fSerr != nil { if fSerr != nil {
if fSerr != btcdb.TxShaMissing { if fSerr != btcdb.ErrTxShaMissing {
return []*btcdb.TxListReply{}, fSerr return []*btcdb.TxListReply{}, fSerr
} }
} else { } else {

View file

@ -17,7 +17,7 @@ import (
// Errors that the various database functions may return. // Errors that the various database functions may return.
var ( var (
ErrDbClosed = errors.New("Database is closed") ErrDbClosed = errors.New("database is closed")
) )
var ( var (
@ -383,7 +383,7 @@ func (db *MemDb) FetchTxBySha(txHash *btcwire.ShaHash) ([]*btcdb.TxListReply, er
if !exists { if !exists {
log.Warnf("FetchTxBySha: requested hash of %s does not exist", log.Warnf("FetchTxBySha: requested hash of %s does not exist",
txHash) txHash)
return nil, btcdb.TxShaMissing return nil, btcdb.ErrTxShaMissing
} }
txHashCopy := *txHash txHashCopy := *txHash
@ -431,7 +431,7 @@ func (db *MemDb) fetchTxByShaList(txShaList []*btcwire.ShaHash, includeSpent boo
// information if the transaction exists. // information if the transaction exists.
reply := btcdb.TxListReply{ reply := btcdb.TxListReply{
Sha: txShaList[i], Sha: txShaList[i],
Err: btcdb.TxShaMissing, Err: btcdb.ErrTxShaMissing,
} }
replyList = append(replyList, &reply) replyList = append(replyList, &reply)
@ -548,7 +548,7 @@ func (db *MemDb) InsertBlock(block *btcutil.Block) (int64, error) {
msgBlock := block.MsgBlock() msgBlock := block.MsgBlock()
if _, exists := db.blocksBySha[msgBlock.Header.PrevBlock]; !exists { if _, exists := db.blocksBySha[msgBlock.Header.PrevBlock]; !exists {
if len(db.blocks) > 0 { if len(db.blocks) > 0 {
return 0, btcdb.PrevShaMissing return 0, btcdb.ErrPrevShaMissing
} }
} }
@ -599,7 +599,7 @@ func (db *MemDb) InsertBlock(block *btcutil.Block) (int64, error) {
log.Warnf("InsertBlock: requested hash "+ log.Warnf("InsertBlock: requested hash "+
" of %s does not exist in-flight", " of %s does not exist in-flight",
tx.Sha()) tx.Sha())
return 0, btcdb.TxShaMissing return 0, btcdb.ErrTxShaMissing
} }
} else { } else {
originTxns, exists := db.txns[prevOut.Hash] originTxns, exists := db.txns[prevOut.Hash]
@ -607,14 +607,14 @@ func (db *MemDb) InsertBlock(block *btcutil.Block) (int64, error) {
log.Warnf("InsertBlock: requested hash "+ log.Warnf("InsertBlock: requested hash "+
"of %s by %s does not exist", "of %s by %s does not exist",
prevOut.Hash, tx.Sha()) prevOut.Hash, tx.Sha())
return 0, btcdb.TxShaMissing return 0, btcdb.ErrTxShaMissing
} }
originTxD := originTxns[len(originTxns)-1] originTxD := originTxns[len(originTxns)-1]
if prevOut.Index > uint32(len(originTxD.spentBuf)) { if prevOut.Index > uint32(len(originTxD.spentBuf)) {
log.Warnf("InsertBlock: requested hash "+ log.Warnf("InsertBlock: requested hash "+
"of %s with index %d does not "+ "of %s with index %d does not "+
"exist", tx.Sha(), prevOut.Index) "exist", tx.Sha(), prevOut.Index)
return 0, btcdb.TxShaMissing return 0, btcdb.ErrTxShaMissing
} }
} }
} }
@ -624,7 +624,7 @@ func (db *MemDb) InsertBlock(block *btcutil.Block) (int64, error) {
inFlightIndex < i { inFlightIndex < i {
log.Warnf("Block contains duplicate transaction %s", log.Warnf("Block contains duplicate transaction %s",
tx.Sha()) tx.Sha())
return 0, btcdb.DuplicateSha return 0, btcdb.ErrDuplicateSha
} }
// Prevent duplicate transactions unless the old one is fully // Prevent duplicate transactions unless the old one is fully
@ -634,7 +634,7 @@ func (db *MemDb) InsertBlock(block *btcutil.Block) (int64, error) {
if !isFullySpent(txD) { if !isFullySpent(txD) {
log.Warnf("Attempt to insert duplicate "+ log.Warnf("Attempt to insert duplicate "+
"transaction %s", tx.Sha()) "transaction %s", tx.Sha())
return 0, btcdb.DuplicateSha return 0, btcdb.ErrDuplicateSha
} }
} }
} }