In the rules for bonus actions it states:
You choose when to take a bonus action during your turn, unless the bonus action's timing is specified …
I have seen this quote used to justify some gratuitous rulings that I am extremely unsure about. I think there is an assumption that you can't take bonus actions during the resolution of mechanics, but I have seen that many people do not share this opinion.
Take the example of Misty Step. Misty Step is a bonus action spell that lets you teleport 30ft. Can you use Misty Step in the following situations (all examples take place on the player's turn);
- When you are the target of an opportunity attack to avoid the attack?
- Once an opportunity attack has hit you but damage has not been dealt to avoid damage?
- When pushed off a cliff to avoid falling to the bottom?
- When using a Necklace of Fireball on your own position, but teleporting away before the damage is dealt?
- When stepping on to a pressure plate trap but before the trap activates?
- Some other examples you can think of to illustrate the RAW limits to bonus actions
You can only take bonus actions when you could take an action on your turn
The PHB says this on page 189:
Anything includes, well, anything, including timing restrictions. Can you take an action in response to being targeted or hit by an opportunity attack, by triggering a trap, or in reaction to your main action?
I don't think you can. This is an instant response to a trigger of some kind, and that is the very defintion of a reaction:
Because you cannot take an action as an instant response, and you only can take a bonus action when you can take an action, you also cannot take a bonus action.