delay error object creation to when errors occur

includes the changes in #3686 with additional refactoring.
improves scalar performance by about 15%