All sin deserves death and hell, so God is doing nothing wrong when He puts Onan to death for refusing to obey His command to further the line of his brother Er, who, we might note, was also put to death by God for his wickedness.
Furthermore, Onan's sin would have been considered much more serious in Israel's culture than what we might think of his sin today.
Some argue that God was wrong to put Onan to death for merely "wasting his semen on the ground." Here is the relevant passage:
- 6 And Judah took a wife for Er his firstborn, and her name was Tamar. 7 But Er, Judah's firstborn, was wicked in the sight of the Lord, and the Lord put him to death. 8 Then Judah said to Onan, “Go in to your brother's wife and perform the duty of a brother-in-law to her, and raise up offspring for your brother.” 9 But Onan knew that the offspring would not be his. So whenever he went in to his brother's wife he would waste the semen on the ground, so as not to give offspring to his brother. 10 And what he did was wicked in the sight of the Lord, and he put him to death also.
In Romans 6:23
So, God was fully justified in putting Onan to death for refusing to obey His command to help further the line of his brother Er. God is not wrong at all to do this because all sin is serious and all sin is fully deserving of death.
The fact that most humans are not put to death immediately for their sin is a sign of God's mercy and grace. God holds off on giving most humans the punishment they deserve to give them time to be saved through repentance and faith. However, God does nothing wrong when He chooses to punish a person for sin before that person naturally dies.
Something else to keep in mind is that Israel's culture was different than ours in that having offspring was extremely important to the Israelites.
In Israel's culture, if a man died without any children, the next of kin sometimes had the responsibility to "go in" to the wife and produce children, who would be regarded as children of the husband who had died. These children would be able to care for their mother and produce offpsring of their own, furthering the line of the deceased husband.
So, it would have been a serious issue for a man to refuse to help further the line of his deceased brother. It was much more serious back then than it sounds now.
Some people have trouble accepting possible explanations to alleged contradictions in the Bible, especially ones that they consider a stretch. For example, Dan Barker, president of the Freedom From Religion Foundation, says this:
I have received numerous replies from Christians who think that these contradictions are either trivial or easily explained. Yet not a single "explanation" has been convincing.
The fact is that there are plausible explanations for every alleged contradiction in the Bible. That there is a plausible explanation for an alleged contradiction does not mean that it is definitely the correct explanation for the alleged contradiction.
However, as long as a possible explanation has been suggested, then it has been objectively demonstrated that there is no necessary contradiction regarding the Bible verses and passages brought up.
When people like Dan Barker say that they don't find a particular explanation for a contradiction "convincing," then that is merely their opinion. A plausible explanation has been suggested that eliminates the necessary alleged contradiction. They simply don't like it, which is not at all a relevant argument against the explanation.
To read more about solutions to Bible contradictions and difficulties, check out Norman Geisler's The Big Book of Bible Difficulties: Clear and Concise Answers from Genesis to Revelation. While we do not agree with some of Geisler's theology, particularly concerning his view of predestination, this book is still an excellent resource. It is thorough and filled with research.
Another book to check out is Tim Chaffey's Demolishing Supposed Bible Contradictions: Exploring Forty Alleged Contraditions, which also answers many alleged contradictions in the Bible.