Issue #1327 regression test and fix

Hi Travis, I came up with a regression test and a fix, please merge or cherry-pick.

I think line 535 prevents err from being `null` here.