hGetContents: close the handle properly on error