Skip to content

Commit

Permalink
Improve error handling - remove hardcoded values (hashicorp#370)
Browse files Browse the repository at this point in the history
* Add macOS and VSCode speciific to .gitignore

* Replace hardcoded error handling and fix few typos

* Leave 'not found' error message
  • Loading branch information
programmer04 authored and catsby committed Oct 16, 2019
1 parent 7d1d8f1 commit ba08237
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 8 deletions.
8 changes: 8 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,11 @@ _testmain.go

*.exe
*.test

# MacOS
.DS_Store
.AppleDouble
.LSOverride

# IDE specific
.vscode/*
8 changes: 4 additions & 4 deletions api.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ var (
ErrEnqueueTimeout = errors.New("timed out enqueuing operation")

// ErrNothingNewToSnapshot is returned when trying to create a snapshot
// but there's nothing new commited to the FSM since we started.
// but there's nothing new committed to the FSM since we started.
ErrNothingNewToSnapshot = errors.New("nothing new to snapshot")

// ErrUnsupportedProtocol is returned when an operation is attempted
Expand Down Expand Up @@ -392,7 +392,7 @@ func HasExistingState(logs LogStore, stable StableStore, snaps SnapshotStore) (b
return true, nil
}
} else {
if err.Error() != "not found" {
if err != ErrKeyNotFound {
return false, fmt.Errorf("failed to read current term: %v", err)
}
}
Expand Down Expand Up @@ -446,7 +446,7 @@ func NewRaft(conf *Config, fsm FSM, logs LogStore, stable StableStore, snaps Sna

// Try to restore the current term.
currentTerm, err := stable.GetUint64(keyCurrentTerm)
if err != nil && err.Error() != "not found" {
if err != nil && err != ErrKeyNotFound {
return nil, fmt.Errorf("failed to load current term: %v", err)
}

Expand Down Expand Up @@ -683,7 +683,7 @@ func (r *Raft) ApplyLog(log Log, timeout time.Duration) ApplyFuture {
}
}

// Barrier is used to issue a command that blocks until all preceeding
// Barrier is used to issue a command that blocks until all preceding
// operations have been applied to the FSM. It can be used to ensure the
// FSM reflects all queued writes. An optional timeout can be provided to
// limit the amount of time we wait for the command to be started. This
Expand Down
7 changes: 6 additions & 1 deletion inmem_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ import (
"sync"
)

var (
// ErrKeyNotFound is returned when a key does not exist in collection.
ErrKeyNotFound = errors.New("not found")
)

// InmemStore implements the LogStore and StableStore interface.
// It should NOT EVER be used for production. It is used only for
// unit tests. Use the MDBStore implementation instead.
Expand Down Expand Up @@ -109,7 +114,7 @@ func (i *InmemStore) Get(key []byte) ([]byte, error) {
defer i.l.RUnlock()
val := i.kv[string(key)]
if val == nil {
return nil, errors.New("not found")
return nil, ErrKeyNotFound
}
return val, nil
}
Expand Down
6 changes: 3 additions & 3 deletions raft.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ func (r *Raft) runCandidate() {
// Make sure the leadership transfer flag is reset after each run. Having this
// flag will set the field LeadershipTransfer in a RequestVoteRequst to true,
// which will make other servers vote even though they have a leader already.
// It is important to reset that flag, because this priviledge could be abused
// It is important to reset that flag, because this privilege could be abused
// otherwise.
defer func() { r.candidateFromLeadershipTransfer = false }()

Expand Down Expand Up @@ -1445,12 +1445,12 @@ func (r *Raft) requestVote(rpc RPC, req *RequestVoteRequest) {

// Check if we have voted yet
lastVoteTerm, err := r.stable.GetUint64(keyLastVoteTerm)
if err != nil && err.Error() != "not found" {
if err != nil && err != ErrKeyNotFound {
r.logger.Error("failed to get last vote term", "error", err)
return
}
lastVoteCandBytes, err := r.stable.Get(keyLastVoteCand)
if err != nil && err.Error() != "not found" {
if err != nil && err != ErrKeyNotFound {
r.logger.Error("failed to get last vote candidate", "error", err)
return
}
Expand Down

0 comments on commit ba08237

Please sign in to comment.