fix error throwing logic