[5.0] Log exceptions and their messages when a job fails.

Recreated this pull request for 5.0 instead of 4.2.

Fixes issue #6203.
Same here.