Return custom error if a key does not exist
continuous-integration/drone/pr Build is failing Details

This commit is contained in:
kolaente 2020-10-10 12:35:55 +02:00
parent b7d54151a4
commit 7de86c5eb6
Signed by: konrad
GPG Key ID: F40E70337AB24C9B
3 changed files with 43 additions and 2 deletions

View File

@ -0,0 +1,27 @@
// Vikunja is a to-do list application to facilitate your life.
// Copyright 2018-2020 Vikunja and contributors. All rights reserved.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package error
import "fmt"
type ErrValueNotFoundForKey struct {
Key string
}
func (e *ErrValueNotFoundForKey) Error() string {
return fmt.Sprintf("could not find value for key %s", e.Key)
}

View File

@ -17,7 +17,10 @@
package memory
import "sync"
import (
e "code.vikunja.io/api/pkg/modules/keyvalue/error"
"sync"
)
// Storage is the memory implementation of a storage backend
type Storage struct {
@ -44,7 +47,14 @@ func (s *Storage) Put(key string, value interface{}) (err error) {
func (s *Storage) Get(key string) (value interface{}, err error) {
s.mutex.Lock()
defer s.mutex.Unlock()
return s.store[key], nil
var exists bool
value, exists = s.store[key]
if !exists {
return nil, &e.ErrValueNotFoundForKey{Key: key}
}
return
}
// Del removes a saved value from a memory storage

View File

@ -18,6 +18,7 @@
package redis
import (
e "code.vikunja.io/api/pkg/modules/keyvalue/error"
"code.vikunja.io/api/pkg/red"
"encoding/json"
"github.com/go-redis/redis/v7"
@ -51,6 +52,9 @@ func (s *Storage) Put(key string, value interface{}) (err error) {
func (s *Storage) Get(key string) (value interface{}, err error) {
b, err := s.client.Get(key).Bytes()
if err != nil {
if err == redis.Nil {
return nil, &e.ErrValueNotFoundForKey{Key: key}
}
return nil, err
}