Refactor job error handling; add tests.