'Run scripts to report error when Jenkins build failed

When Jenkins job is marked as failure, how to run a script so that a report could be sent?

Post build actions do the trick but can not catch errors.

In addition, how to catch errors like the following?

21:37:14 Cloning into 'xxx'...
21:37:14 fatal: Remote branch release not found in upstream origin
21:37:14 fatal: The remote end hung up unexpectedly
21:37:14 [!] run git clone -b release xxx failed.


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source