Improve error handling in ImageDatabase