Onan's sin was entirely related to his refusal to perform his levirate duty. Quickly about the other three: Coitus interruptus is not masturbation. It is a (very unreliable) method of birth control. Onan was attempting not to get Tamar pregnant because he did not want to provide an heir for his deceased older brother. It was not "theft of Tamar's child." Rather, it was (once again) refusal to ...
In Genesis 38:7-10 we are told of the deaths of Er and Onan but with little accounting. In Genesis Ch 34 however we have the whole accounting of Simeon and Levi's despicable actions, which were acted upon by their own volition and against their father (Jacob's) wishes. Their sister Dinah had been defiled by one certain prince by the name of Shechem, of the land of the same name, but who later ...
Judah's sons Er & Onan were killed by God for their sins, and yet ...
According to the law of levirate, Onan then marries Tamar, but he also ends up dead, due to the practice of coitus interruptus, in order not to give descendants to his deceased brother. Judah asks Tamar to wait until Shelah is old enough to marry, and then some time passes. Moving forward, Shelah marries another woman, Judah deceives Tamar.
Thus Onan, by practicing birth control with Tamar, was a threat to the seed, that is the Promise, whereas Judah, by sleeping with a prostitute, was a friend of the Promise. Thus one was put to death and the other was honored, even though by human standards of justice Judah's crime was worse than Onan's.