Handle errors

This commit is contained in:
Adria Navarro 2024-03-05 22:52:44 +01:00
parent 837395e5e0
commit 192d7deb2a
2 changed files with 14 additions and 0 deletions

View File

@ -334,6 +334,9 @@ class RedisWrapper {
async increment(key: string) {
const result = await this.getClient().incr(addDbPrefix(this._db, key))
if (isNaN(result)) {
throw new Error(`Redis ${key} does not contains a number`)
}
return result
}
}

View File

@ -146,5 +146,16 @@ describe("redis", () => {
expect(results).toHaveLength(100)
expect(results).toEqual(Array.from({ length: 100 }).map((_, i) => i + 1))
})
it.each([
generator.word(),
generator.bool(),
{ [generator.word()]: generator.word() },
])("cannot increment if the store value is not a number", async value => {
const key = structures.uuid()
await redis.store(key, value)
await expect(redis.increment(key)).rejects.toThrowError("")
})
})
})