Test failed event? | Call function on test fail?

Hello,

Is there any way to handle failed test cases? I would like to call a function when the test fails. After the function executes test case can close.

Some of the stuff that I would like to do in function: Save browser JS log, take screenshot(when it isn’t done automatically), save values of hidden fields and offscreen elements etc.

Is this possible?

Best regards,
Ismar

Similar issue is here