porting over file branchs error handler (its better, should have gone in master in the first place)