moved error handler to catch function