Hi sectan.
In many cases, your best move is just to learn the different structures that are commonly used.
At the same time there are some general concepts you can consider as well.
One is that a gerund without a preposition before it can be use as the direct object of a verb.
Examples:
I like swimming.
The writer prefers working in the morning.If the gerund is not the logical direct object of the verb, then the wording will be incorrect.
Example:
As hard as John tried, he failed finding the treasure.Notice that, in this case, "finding the treasure" is not something that one would fail, like a test. So, "finding" is not a logical direct object of "failed," and thus the sentence is incorrect. We need the following instead.
As hard as John tried, he failed to find the treasure.That said, we've come full circle since, if you don't know that "failed to x" is correct, then "failed to find" may not seem any better than "failed finding" to you.
So, you really do need to learn the common structures.