Home > Enterprise >  Error EntityNotFound from store layer Redis Golang
Error EntityNotFound from store layer Redis Golang

Time:06-04

I have a golang Project that implements redis as it's backend store. In the Get method of the store layer, I want to check if an entity is existing or not. If the entity is existing i will return it else i have two cases :

  • Error EntityNotFound
  • DB Error

I am only able to return the DB Error. How can i return the entity not found error. Like what's the condition for checking in redis.

Help Please

CodePudding user response:

When doing a key lookup the Redis client will return an error if the key does not exist. You can check if that error is equal to redis.Nil, which indicates that the key was not found.

value, err = client.Get(ctx, key)
if errors.Is(err, redis.Nil) {
  return EntityNotFound
} else if err != nil {
  // Some other error
  return err
}
  • Related