Fix: Check output file isn't already a directory, handle errors, fix and add tests

Fixes #1034.
I prefer the extra call over having an empty catch block.