Issue #1327 regression test and fix

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

err may be `null`