And take into account database errors as well

Signed-off-by: emanuele <emanuele45@gmail.com>