Fix handling of errors from API where code is nil

Fix for #168.