Passing error to function instead of just alerting error message