From ba082378c3436b5fc9af38c40587f2d9ee59cccf Mon Sep 17 00:00:00 2001 From: Jakub Warczarek Date: Wed, 16 Oct 2019 18:40:15 +0200 Subject: [PATCH] Improve error handling - remove hardcoded values (#370) * Add macOS and VSCode speciific to .gitignore * Replace hardcoded error handling and fix few typos * Leave 'not found' error message --- .gitignore | 8 ++++++++ api.go | 8 ++++---- inmem_store.go | 7 ++++++- raft.go | 6 +++--- 4 files changed, 21 insertions(+), 8 deletions(-) diff --git a/.gitignore b/.gitignore index 836562412fe8..59216e7e1201 100644 --- a/.gitignore +++ b/.gitignore @@ -21,3 +21,11 @@ _testmain.go *.exe *.test + +# MacOS +.DS_Store +.AppleDouble +.LSOverride + +# IDE specific +.vscode/* diff --git a/api.go b/api.go index 17ca6556ee0d..5f696088d75c 100644 --- a/api.go +++ b/api.go @@ -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 @@ -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) } } @@ -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) } @@ -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 diff --git a/inmem_store.go b/inmem_store.go index 6285610f9ade..d495e72c72c4 100644 --- a/inmem_store.go +++ b/inmem_store.go @@ -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. @@ -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 } diff --git a/raft.go b/raft.go index af60c75914a1..bdac6e149bcb 100644 --- a/raft.go +++ b/raft.go @@ -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 }() @@ -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 }