2nd Samuel 6:7 And the anger of the Lord was kindled against Uzzah; and God smote him there for his error; and there he died by the ark of God. 8 And David was displeased, because the Lord had made a breach upon Uzzah. 9 And David was afraid of the Lord that day.
In this instance, the Bible does not give any historical context, leading up to the incident, so that persons could reason logically and conclude that yes, Uzzah received his due. But the scripture is not to be understood through logic, it is to be understood through faith, and thus, what God did in this instance was naturally right, and long overdue.
Even though nothing is said of his prior offences, it is a given that God granted him mercy and grace on many, many occasions, before it finally culminated in his death.
The Bible does not have to always give us the fine print and details; all we have to do is to see God in His position upon the Mercy seat located above law, and all inserts, or omissions in scripture will become as clear as day in the context of His great mercy.
Deuteronomy 7:9 Know therefore that the Lord thy God, he is God, the faithful God, which keepeth covenant and mercy with them that love him and keep his commandments to a thousand generations.
May The Lord add His blessing to the study of His word. God bless!