Improve error handling in dbExists function